Im new to java, I used a hashmap initially and did a forEach over that, it worked fine:
Map<String, Integer> testmap = new HashMap<>();
IntStream.range(0, 100).forEach(n -> {
testmap.put("teststring-" + Integer.toString(n), 1);
});
String x = testmap.entrySet().stream().filter(..);
However, now I have a ImmutableHashMap which I want to do the same above steps, how would I do that? I tried doing
ImmutableMap.Builder<String, Integer> testmap = ImmutableMap.builder();
IntStream.range(0, 100).forEach(n -> {
testmap.put("teststring-" + Integer.toString(n), 1);
});
testmap.build();
String x = testmap.entrySet().stream().filter(...); // throws an error while compile
cannot find symbol
[javac] String testmap = testmap.entrySet().stream()
[javac] ^
[javac] symbol: method entrySet()
[javac] location: variable streams of type Builder<String,Integer>
Can anyone point out what i'm doing wrong here? thanks much for all your help!
testmap.build()does? Might you need to assign the return value to something...?Streamis pretty horrible. Your first case can be rewritten in a single line usingCollectors.toMap. Your second case can be rewritten to use a customCollector. I would strongly suggest you do some more reading about Java 8 before plunging into its depths.testmapwhich lets you thinktestmapis a Map as it used to be in your first code snipped. Always think careful when choosing identifier names!.Integer.toString(n); you can just use"teststring-"+nto get the desired result.