Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Stream flatMap and group by code compiler error

// given a set of Item objects, group them by the managers of creator and owners
Map<String, List<Item>> managersItems = 
    itemSet.parallelStream().flatMap(item -> {
        // get the list of the creator and owners
        List<String> users = new ArrayList();
        users.add(item.getCreator());
        users.addAll(item.getOwners());
        return Stream.of(users.toArray(new String[] {})).map(user -> {
            LdapUserInfo ldapUser = LdapUserInfoFactory.create(user);
            String manager = ldapUser.getManager();
            return new AbstractMap.SimpleImmutableEntry<String, Item(manager, item);
        });
    }).collect(
        Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

This code compiles fine in Eclipse Mars, but gets the following eror in Eclipse Luna:

Type mismatch: cannot convert from Map<Object,List<Object>> to Map<String,List<WeblabInfo>>

If I do not assign the returned to a Map with Map<String, List<Item>> managersItem = in Eclipse Luna, the error is at Map.Entry::getKey and Map.Entry::getValue statement with message:

The type Map.Entry does not define getKey(Object) that is applicable here".

What did I do wrong?

like image 851
derrdji Avatar asked Feb 18 '26 19:02

derrdji


1 Answers

You didn't do anything wrong. Eclipse compiler has problems with type inference that causes these issues. If Luna compatibility is important, you will have to add explicit types to lambda expressions. Try, for example, Map.Entry::<String,Item>getKey

On another note, it's not necessary to convert a List to array to stream it. You can directly call users.stream(). But even creating the List isn't necessary. You can use Stream.concat(Stream.of(item.getCreator()), item.getOwners().stream()) instead (granted, it's a bit unwieldy).

Finally (and most importantly), avoid using parallelStream with blocking code, such as looking up data in an external system. Parallel streams are designed to handle CPU-bound tasks.

like image 112
Misha Avatar answered Feb 21 '26 14:02

Misha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!