1

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!

5
  • 4
    What do you think testmap.build() does? Might you need to assign the return value to something...? Commented Mar 27, 2017 at 20:50
  • 3
    In any case, mutating external variables from inside a Stream is pretty horrible. Your first case can be rewritten in a single line using Collectors.toMap. Your second case can be rewritten to use a custom Collector. I would strongly suggest you do some more reading about Java 8 before plunging into its depths. Commented Mar 27, 2017 at 20:51
  • 2
    Check the docs: google.github.io/guava/releases/snapshot/api/docs/com/google/… and look at your own code. You don't actually have an Immutablemap anywhere but a builder to create one. Commented Mar 27, 2017 at 20:53
  • 1
    You are confused by your bad choice of names. You named your ImmutableMap.Builder testmap which lets you think testmap is a Map as it used to be in your first code snipped. Always think careful when choosing identifier names!. Commented Mar 27, 2017 at 21:09
  • By the way, there is no need for Integer.toString(n); you can just use "teststring-"+n to get the desired result. Commented Mar 28, 2017 at 13:12

1 Answer 1

5

You have to build map from builder. Update your code to:

ImmutableMap.Builder<String, Integer> testmapBuilder = ImmutableMap.builder();
IntStream.range(0, 100).forEach(n -> testmapBuilder.put("teststring-" + Integer.toString(n), 1));
ImmutableMap<String, Integer> testmap = testmapBuilder.build();
//your code...

Or update to this one

Map<String, String> testmap = IntStream.range(0, 100)
        .mapToObj(Integer::toString)
        .collect(collectingAndThen(toMap(n -> "teststring-" + n, o -> "1"), ImmutableMap::copyOf));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.