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.
- Implementing Checksum Using Java
- Implementing Byte Stuffing using Java
- Implementing a Linked List in Java using Class
- Java Applet | Implementing Flood Fill algorithm
- Height of a generic tree from parent array
- Graph Representation using Java ArrayList
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Detect cycle in the graph using degrees of nodes of graph
- Java Program to Detect Cycle in a Directed Graph
- Implementing Forward Iterator in BST
- Implementing Backward Iterator in BST
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Sum of dependencies in a graph
- Transpose graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.