I have the following multiMap:
1st: [3=>[4,5]]
2nd: [2=>[3]]
3rd: [0=>[2]]
4th: [5=>[1]]
5th: [4=>[1]]
I would like to get the sequence out of it such as:
0->2->3->[4,5]->1
Is it possible?
This is what I have tried, but doesn't seem to work correctly:
// the starting point is given from the start.
Collection<String> col = new HashSet<String>();
col.add("0");
buildPath2(myMultimap, col, result2);
static void buildPath2(Multimap<String, String> path, Collection<String> token, List<Collection<String>> result2) {
if (token != null) {
result2.add(token);
Collection<String> c = null;
for (String elem: token){
c = path.get(elem);
}
buildPath2(path, c, result2);
}
}
You've got two basic problems that you'll need to resolve.
result2
somehow.for
loop, c
will be whatever you get from looking up the last element of token
in the map. You probably wanted it to be a Set
containing what you get from looking up all the elements of token
in the map. To deal with issue 2, you should probably change
Collection<String> c = null;
for (String elem: token){
c = path.get(elem);
}
to
Collection<String> c = new HashSet<>();
for (String elem : token) {
c.addAll(path.get(elem));
}