Reputation: 502
static HashMap<String, HashMap<String, List<Integer>>> BIGMAP= new HashMap<String, HashMap<String, List<Integer>>>();
This is a Hashmap if i were to print it out this is what i get:
SortedSet<String> keys = new TreeSet<String>(BIGMAP.keySet());
for (String key : keys) {
HashMap<String, List<Integer>> value = kmap.get(key);
System.out.println(key + " : " + value);
}
a.txt : {a=[20], to=[10]}
b.txt : {a=[4, 20], by=[16], is=[2], to=[13, 22]}
c.txt : {a=[15], as=[16, 17], in=[34], do=[9], to=[14]}
So i am trying to iterate through this "BIGMAP" the above for each key a.txt(String) there is a Value which is another map the structure of HashMap<String, List<Integer>>
...
OUTPUT:
a [15],[20],[4, 20],
as [16, 17],, ,, ,
by , ,, ,[16],
...so on...
...so on...
...so on...
EXPECTED/WANTED OUTPUT
a 20, 4:20 ,15
as , , 16:17
by , 16 ,
the printing format should be like above but obviously it should print in order from each textfile(string) which is a key in the outer hashmap
for example a 20, 4:20 ,15
"a" is word FOR That word , 20
is from the value where key isa.txt
and 4:20
is from b.txt
and 15
is from c.txt
As you can see i have something up there but its not doing exactly what i need. The colons are necessary to where there multiple numbers. it can ever sometimes be 4:20:50:92
Upvotes: 0
Views: 354
Reputation: 50716
This will do the job in Java 8. Not my best work, but tested successfully with your sample values.
BIGMAP.values()
.stream()
.flatMap(m -> m.keySet().stream())
.distinct()
.sorted()
.map(k -> k
+ " "
+ BIGMAP.entrySet()
.stream()
.sorted(comparing(Entry::getKey))
.map(e -> e.getValue()
.getOrDefault(k, Collections.emptyList())
.stream()
.map(String::valueOf)
.collect(joining(":")))
.collect(joining(", ")))
.forEachOrdered(System.out::println);
Produces the following output:
a 20, 4:20, 15 as , , 16:17 by , 16, do , , 9 in , , 34 is , 2, to 10, 13:22, 14
Upvotes: 1