We can build on top of this: Graph Representation - Adjacency List and Map. We can store source as key in map and pair of destination and weight as adjacency list. We are using int array of size 2 here, as java doesnt still have a good pair class.
Code
Java
public class Graph {
private Map<Integer, List<int[]>> adjList = new LinkedHashMap<>();
private int V;
private final boolean isDirected;
public Graph(int n, boolean isDirected) {
V = n;
for (int i = 0; i < n; i++) {
adjMap.put(i, new LinkedList<>());
}
this.isDirected = isDirected;
}
public boolean addEdge(int i, int j, int w) {
adjMap.putIfAbsent(i, new LinkedList<>());
adjMap.get(i).add(new int[]{j, w});
if (!isDirected) {
adjMap.putIfAbsent(j, new LinkedList<>());
adjMap.get(j).add(new int[]{i, w});
}
return true;
}
}