-1

This is my hash map:

HashMap<Customer, HashSet<Car>> carsBoughtByCustomers = ...

How can I get a new HashMap that will contain for each customer the amount of cars, i.e. the size of the HashSet?

I want to do this without loops, but using streams only.

My attempt:

HashMap<Customer, Integer> result = (HashMap<Customer, Integer>)
    carsBoughtByCustomers.entrySet().stream()
        .map(e -> e.getValue().size());
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46

1 Answers1

1

You need to apply a terminal operation in order to obtain a result from the stream, collect() in this case, which expects a Collector.

map() - is an intermediate operation, it produces another stream, i.e. it's meant to transform the stream-pipeline but not to generate a result. Therefore, a stream-statement like this myMap.entrySet().stream().map() results to a Stream, and is not assignable to a variable of type Map.

Map<Customer,Integer> result = carsBoughtByCustomers.entrySet().stream()
            .collect(Collectors.toMap(
                Map.Entry::getKey,
                entry -> entry.getValue().size()
            ));

Sidenotes:

  • Writing code against interfaces like Map and Set, not concrete types like HashMap and HashSet, makes your code more flexible. See: What does it mean to "program to an interface"?
  • A stream without a terminal operation is perfectly valid, i.e. it would compile (if there are no errors is the code), but would not be executed.
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46