Diana G
Diana G

Reputation: 261

Transforming a Stream in a HashMap with condition

I am really new to java 8.I am trying to create a HashMap from a stream that has showNames in it. My issues is that the names can be repetitive and they can appear multiple times, which means they have to be mapped on the same key, I have tried with filter after mapping but I am not sure what condition to put. x->x.equals(x)? how the stream showNames looks like:

LOTR, Lucifer, Breaking Bad, LOTR, Exorcist, The Godfather, The Godfather, Lucifer, etc

The hash map should have as keys the name of the show and as value the times the string appears in the stream

Upvotes: 1

Views: 120

Answers (1)

Eugene
Eugene

Reputation: 120848

What you want is to group actually:

Map<String, Long> result = yourList.stream()
              .collect(Collectors.groupingBy(
                 Function.identity(), 
                 Collectors.counting()))

Upvotes: 5

Related Questions