We have learnt about Generic class in Java. We can also use them to code for Graph in Java. The Graph Class is implemented using HashMap in Java. As we know HashMap contains a key and a value, we represent nodes as keys and their adjancency list in values in the graph.
Like C++, we use <> to specify parameter types in generic class creation. To create objects of generic class, we use following syntax.
// To create an instance of generic class BaseType <Type> obj = new BaseType <Type>() Note: In Parameter type, we cannot use primitives like 'int','char' or 'double'.
Below is the implementation of the above approach:
Graph: 0: 1 4 1: 0 2 3 4 2: 1 3 3: 1 2 4 4: 0 1 3 The graph has 5 vertex The graph has 7 edges. The graph has an edge between 3 and 4. The graph does not contain 5 as a vertex.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Height of a generic tree from parent array
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Implementing Checksum Using Java
- Implementing a Linked List in Java using Class
- Java Applet | Implementing Flood Fill algorithm
- Implementing Byte Stuffing using Java
- Implementing Water Supply Problem using Breadth First Search
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Implementing Forward Iterator in BST
- Implementing Backward Iterator in BST
- Implementing Rich getting Richer phenomenon using Barabasi Albert Model in Python
- Implementing upper_bound() and lower_bound() for Ordered Set in C++
- Implementing Backward and Forward buttons of Browser
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Detect cycle in the graph using degrees of nodes of graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Convert undirected connected graph to strongly connected directed graph
- Java Program to Detect Cycle in a Directed Graph
- Graph Representation using Java ArrayList
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.