Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collect to map skipping null key/values

Let's say I have some stream and want to collect to map like this

stream.collect(Collectors.toMap(this::func1, this::func2));

But I want to skip null keys/values. Of course, I can do like this

stream.filter(t -> func1(t) != null)
    .filter(t -> func2(t) != null)
    .collect(Collectors.toMap(this::func1, this::func2));

But is there more beautiful/effective solution?

like image 565
Joel Avatar asked Apr 03 '18 15:04

Joel


People also ask

Can we use null as a key for a map collection?

Null doesn't have any hashcode, but is tolerated as key in the HashMap (there are other Map implementations which don't allow Null as key).

Can collectors toMap return null?

toMap throws a NullPointerException if one of the values is null .


3 Answers

If you want to avoid evaluating the functions func1 and func2 twice, you have to store the results. E.g.

stream.map(t -> new AbstractMap.SimpleImmutableEntry<>(func1(t), func2(t))
      .filter(e -> e.getKey()!=null && e.getValue()!=null)
      .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

This doesn’t make the code shorter and even the efficiency depends on the circumstances. This change pays off, if the costs of evaluating the func1 and func2 are high enough to compensate the creation of temporary objects. In principle, the temporary object could get optimized away, but this isn’t guaranteed.

Starting with Java 9, you can replace new AbstractMap.SimpleImmutableEntry<>(…) with Map.entry(…). Since this entry type disallows null right from the start, it would need filtering before constructing the entry:

stream.flatMap(t -> {
          Type1 value1 = func1(t);
          Type2 value2 = func2(t);
          return value1!=null && value2!=null? Stream.of(Map.entry(value1, value2)): null;
      })
      .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Alternatively, you may use a pair type of one of the libraries you’re already using (the Java API itself doesn’t offer such a type).

like image 186
Holger Avatar answered Oct 16 '22 18:10

Holger


Another way to avoid evaluating the functions twice. Use a pair class of your choice. Not as concise as Holger's but it's a little less dense which can be easier to read.

stream.map(A::doFuncs)
    .flatMap(Optional::stream)
    .collect(Collectors.toMap(Pair::getKey, Pair::getValue));

private static Optional<Pair<Bar, Baz>> doFuncs(Foo foo)
{
    final Bar bar = func1(foo);
    final Baz baz = func2(foo);
    if (bar == null || baz == null) return Optional.empty();
    return Optional.of(new Pair<>(bar, baz));
}

(Choose proper names - I didn't know what types you were using)

like image 44
Michael Avatar answered Oct 16 '22 18:10

Michael


One option is to do as in the other answers, i.e. use a Pair type, or an implementation of Map.Entry. Another approach used in functional programming would be to memoize the functions. According to Wikipedia:

memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

So you could do it by caching the results of the functions in maps:

public static <K, V> Function<K, V> memoize(Function<K, V> f) {
    Map<K, V> map = new HashMap<>();
    return k -> map.computeIfAbsent(k, f);
}

Then, use the memoized functions in the stream:

Function<E, K> memoizedFunc1 = memoize(this::func1);
Function<E, V> memoizedFunc2 = memoize(this::func2);

stream.filter(t -> memoizedFunc1.apply(t) != null)
    .filter(t -> memoizedFunc2.apply(t) != null)
    .collect(Collectors.toMap(memoizedFunc1, memoizedFunc2));

Here E stands for the type of the elements of the stream, K stands for the type returned by func1 (which is the type of the keys of the map) and V stands for the type returned by func2 (which is the type of the values of the map).

like image 3
fps Avatar answered Oct 16 '22 18:10

fps