C
C
click_f2016-10-14 15:46:43
Java
click_f, 2016-10-14 15:46:43

How to do .collect() in lambda expression?

NavigableMap<Integer, Items>  filter_result = map.entrySet().stream()
                .filter( item -> item.getValue() < max)
                .limit(size)
                .collect(<???>);

How to get a NavigableMap as an output, given that Items is a custom type, and map is also a NavigableMap?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Lopatin, 2016-10-14
@click_f

Collector.of(
    TreeMap::new,
    (map, e) -> map.put(e.getKey, e.getValue()),
    (left, right) -> {left.putAll(right); return left;}
    EnumSet.of(Collector.Characteristics.UNORDERED, Collector.Characteristics.IDENTITY_FINISH))

something like this.
You can also write your own Collector.
But it can be simpler like this:
NavigableMap<Integer, Items> result = new TreeMap<>();

...forEach(e -> result.put(e.getKey(), e.getValue())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question