bdrx
bdrx

Reputation: 943

Convert List to Map and filter null keys

Using java 8 streams I want to convert a list into a map like described in the solution to Java 8 List<V> into Map<K, V>. However, I want to filter to remove entries with certain keys (for instance if the key is null) without doing the conversion of the value to a key twice.

For example I could do the filtering prior to collect such as

Map<String, Choice> result =
    choices.stream().filter((choice) -> choice.getName() != null).collect(Collectors.toMap(Choice::getName,
                                 Function.<Choice>identity());

In my case the logic to get the key is more complex than simply getting a field property, and I would like to avoid doing the logic first in the filter and again in the keyMapper function of Collectors.toMap

How can I convert the list to a map using a custom keyMapper function and filter certain values based on the new key?

Upvotes: 3

Views: 7927

Answers (3)

nosid
nosid

Reputation: 50034

If you want to calculate the key only once, you can use the stream method map to convert the stream to a stream of tuples, filter the tuples based on the key, and finally create the map from the tuples:

Map<String, Choice> result = choices.stream()
    .map(c -> new AbstractMap.SimpleEntry<String, Choice>(c.getName(), c))
    .filter(e -> e.getKey() != null)
    .collect(toMap(e -> e.getKey(), e -> e.getValue()));

Upvotes: 9

Lukasz Wiktor
Lukasz Wiktor

Reputation: 20422

If you accept doing it in 2 steps you could first collect the map and then remove unwanted keys:

Map<String, Choice> result = choices.stream()
    .collect(Collectors.toMap(c -> c.getName(), c -> c);
result.keySet().removeIf(k -> k == null);

Upvotes: 1

Dane White
Dane White

Reputation: 3542

Here's a custom collector for what you want:

public class FilteredKeyCollector<T, K, V> implements Collector<T, Map<K, V>, Map<K, V>> {

    private final Function<? super T,? extends K> keyMapper;
    private final Function<? super T,? extends V> valueMapper;
    private final Predicate<K> keyFilter;
    private final EnumSet<Collector.Characteristics> characteristics;

    private FilteredKeyCollector(Function<? super T,? extends K> keyMapper, Function<? super T,? extends V> valueMapper, Predicate<K> keyFilter) {

        this.keyMapper = keyMapper;
        this.valueMapper = valueMapper;
        this.keyFilter = keyFilter;
        this.characteristics = EnumSet.of(Collector.Characteristics.IDENTITY_FINISH);
    }

    @Override
    public Supplier<Map<K, V>> supplier() {
        return HashMap<K, V>::new;
    }

    @Override
    public BiConsumer<Map<K, V>, T> accumulator() {
        return (map, t) -> {
            K key = keyMapper.apply(t);
            if (keyFilter.test(key)) {
                map.put(key, valueMapper.apply(t));
            }
        };
    }

    @Override
    public BinaryOperator<Map<K, V>> combiner() {
        return (map1, map2) -> {
            map1.putAll(map2);
            return map1;
        };
    }

    @Override
    public Function<Map<K, V>, Map<K, V>> finisher() {
        return m -> m;
    }

    @Override
    public Set<Collector.Characteristics> characteristics() {
        return characteristics;
    }
}

And using it:

Map<String, Choice> result = choices.stream()
    .collect(new FilteredKeyCollector<>(
                Choice::getName,    // key mapper
                c -> c,             // value mapper
                k -> k != null));   // key filter

Upvotes: 2

Related Questions