1

Where mappedRoles = Maps.uniqueIndex... can be seen Netbeans produces the following error:

no suitable method found for uniqueIndex(List<ReducedEntity>,<anonymous java.util.function.Function<ReducedEntity,Integer>>)
    method Maps.<K#1,V#1>uniqueIndex(Iterable<V#1>,com.google.common.base.Function<? super V#1,K#1>) is not applicable
      (cannot infer type-variable(s) K#1,V#1
        (argument mismatch; <anonymous java.util.function.Function<ReducedEntity,Integer>> cannot be converted to com.google.common.base.Function<? super V#1,K#1>...

Demo of issue:

package com.mycompany.testground;

import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class Main {
    public static void main(String[] args) {
        List<ReducedEntity> list = new ArrayList<>();
        Map<Integer, ReducedEntity> mappedRoles;
        mappedRoles = Maps.uniqueIndex(list, new Function<ReducedEntity, Integer>() {
            @Override
            public Integer apply(ReducedEntity from) {
                return from.getId();
            }
        });
    }
}

Reduced Entity Class for completeness (but probably unnecessary)

package com.mycompany.testground;

public class ReducedEntity {
    protected Integer id;
    protected String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Created the above after following Jons answer on this page Java: how to convert a List<?> to a Map<String,?> and am unable to yet see why this does not work as shown.

Community
  • 1
  • 1
Quaternion
  • 10,380
  • 6
  • 51
  • 102

1 Answers1

3

You're using java.util.function.Function. The Maps.uniqueIndex() method expects a com.google.common.base.Function. Fix your imports.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • +1 and why sometimes imports aren't superfluous... but netbeans only suggests this import so it is likely someone else will do the same ridiculous thing. – Quaternion Aug 24 '14 at 16:53
  • 2
    @Quaternion: If you're able to import `java.util.function.Function`, you're compiling with JDK8. In which case, can you use Java 8 features? If you replace the whole `Function` with a method reference like `ReducedEntity::getId`, you won't even have to worry about which type of `Function` the method takes. – ColinD Aug 25 '14 at 15:20
  • I can use Java8 features. I'll give it a try, the above example was after a dozen other attempt but I'd assigned the method reference to a variable before using it... and the type was not in agreement. I'll try what you recommend. – Quaternion Aug 25 '14 at 21:16
  • I had to revert the code because Glassfish does not support lambda functions yet =( but the issue is resolved in the next point release (4.1),yet to be released – Quaternion Sep 04 '14 at 22:29