I have a map TreeMap<Integer, Set<Integer>> adjacencyLists
and an integer set TreeSet<Integer> specialNodes
.
The map represents adjacency lists of a graph.
I want to pick keys from adjacencyLists
and find if there is a common adjacent of them in specialNodes
.
Is there a way to do this efficiently?
Example:
adjacencyLists
is as follows:
[1, [2 3 4 5]]
[2, [1 5]]
[3, [1 4 5]]
[4, [1 3]]
[5, [1 2 3]]
and specalNodes
is as follows:
[1 3 4 5]
In this example, 4
and 5
are present in the values of first and third entries of adjacencyLists
.
Hence, writing a function findCommon(1,3)
should give me [4 5]
Similarly, findCommon(1,5)
should return null
because '2' is the only element that is common and is not in specialNodes
.