Transform and filter a Java Map with streams

Yes, you can map each entry to another temporary entry that will hold the key and the parsed integer value. Then you can filter each entry based on their value. Map<String, Integer> output = input.entrySet() .stream() .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), Integer.valueOf(e.getValue()))) .filter(e -> e.getValue() % 2 == 0) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue )); Note that I … Read more

Java 8 toMap IllegalStateException Duplicate Key

The pramodh’s answer is good if you want to map your value to 1. But in case you don’t want to always map to a constant, the use of the “merge-function” might help: Map<Integer, Integer> map1 = Files.lines(Paths.get(inputFile)) .map(line::trim()) .map(Integer::valueOf) .collect(Collectors.toMap(x -> x, x -> 1, (x1, x2) -> x1)); The above code is almost … Read more

Collectors.groupingBy doesn’t accept null keys

I had the same kind of problem. This failed, because groupingBy performs Objects.requireNonNull on the value returned from the classifier: Map<Long, List<ClaimEvent>> map = events.stream() .filter(event -> eventTypeIds.contains(event.getClaimEventTypeId())) .collect(groupingBy(ClaimEvent::getSubprocessId)); Using Optional, this works: Map<Optional<Long>, List<ClaimEvent>> map = events.stream() .filter(event -> eventTypeIds.contains(event.getClaimEventTypeId())) .collect(groupingBy(event -> Optional.ofNullable(event.getSubprocessId())));

Splitting List into sublists along elements

Although there are several answers already, and an accepted answer, there are still a couple points missing from this topic. First, the consensus seems to be that solving this problem using streams is merely an exercise, and that the conventional for-loop approach is preferable. Second, the answers given thus far have overlooked an approach using … Read more

Hashmap with Streams in Java 8 Streams to collect value of Map

If you are sure you are going to get at most a single element that passed the filter (which is guaranteed by your filter), you can use findFirst : Optional<List> o = id1.entrySet() .stream() .filter( e -> e.getKey() == 1) .map(Map.Entry::getValue) .findFirst(); In the general case, if the filter may match multiple Lists, you can … Read more

Java 8 Collectors.toMap SortedMap

I don’t think you can get much better than this: .collect(Collectors.toMap(keyMapper, valueMapper, (v1,v2) ->{ throw new RuntimeException(String.format(“Duplicate key for values %s and %s”, v1, v2));}, TreeMap::new)); where the throw lambda is the same as throwingMerger() but I can’t directly call that since it’s package private (you can of course always make your own static method … Read more

What kind of List does Collectors.toList() return?

So, what concrete type (subclass) of List is being used here? Are there any guarantees? If you look at the documentation of Collectors#toList(), it states that – “There are no guarantees on the type, mutability, serializability, or thread-safety of the List returned”. If you want a particular implementation to be returned, you can use Collectors#toCollection(Supplier) … Read more

Is there a Collector that collects to an order-preserving Set?

You can use toCollection and provide the concrete instance of the set you want. For example if you want to keep insertion order: Set<MyClass> set = myStream.collect(Collectors.toCollection(LinkedHashSet::new)); For example: public class Test { public static final void main(String[] args) { List<String> list = Arrays.asList(“b”, “c”, “a”); Set<String> linkedSet = list.stream().collect(Collectors.toCollection(LinkedHashSet::new)); Set<String> collectorToSet = list.stream().collect(Collectors.toSet()); System.out.println(linkedSet); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)