Why is this simple generic Java function not compiling?

While f1compiling, f2it won’t be very similar , and I just can’t explain why. (Tested on Intellij 9 and Eclipse 3.6)

And indeed, I thought with this question.

import java.util.*;
public class Demo {

    public  List<? extends Set<Integer>> f1(){
        final List<HashSet<Integer>> list = null;
        return list;         
    } 

    public  List<List<? extends Set<Integer>>> f2(){
        final List<List<HashSet<Integer>>> list = null;
        return list;         
    } 

}
+5
source share
3 answers

List<List<HashSet<Integer>>>not assigned List<List<? extends Set<Integer>>>for the same reason List<HashSet<Integer>>cannot be assigned List<Set<Integer>>.

You can get it for compilation by changing this:

public  List<List<? extends Set<Integer>>> f2(){

in it:

public  List<? extends List<? extends Set<Integer>>> f2(){

, , , (.. "List<HashSet<Integer>> List<Set<Integer>>" ), , Java .

, Circle Shape, List<Circle> List<Shape>. , List<Circle> add(Shape), Square, , , Square List<Circle>.

, , . List<? extends Shape> , E, , E . , E get(int), add(E) . List<? extends Shape> List<Shape>, List<Circle>, List<? extends Circle> .. (? super : , , )

, , :

  • List<HashSet<Integer>> List<? extends Set<Integer>>
  • generics , (, List<...>) , sub/super-type. List<List<HashSet<Integer>>> List<List<? extends Set<Integer>>>
  • List<...> List<? extends ...>, . ( , , , 80% , .)

, trashgod BalusC , , , . List<List<Set<Integer>>> . , , , . : List<ImmutableSet<Integer>> List<Set<Integer>>, ImmutableSet<Integer> List<Set<Integer>>, List<ImmutableSet<Integer>>, List<Set<Integer>>.

+11

" . , , ." - , Java, 5, 28.

+6

, . , . :

public List<List<Set<Integer>>> f2() {
    List<List<Set<Integer>>> list = new ArrayList<List<Set<Integer>>>();
    List<Set<Integer>> nestedList = new ArrayList<Set<Integer>>();
    list.add(nestedList);
    Set<Integer> set = new HashSet<Integer>();
    nestedList.add(set);
    return list;
}

. ? extends SomeInterface .


: :

public List<Map<Integer, Set<Integer>>> getOutcomes() {
    Map<HashSet<Integer>, Integer> map = new HashMap<HashSet<Integer>, Integer>();
    List<Map<Integer, Set<Integer>>> outcomes = new ArrayList<Map<Integer, Set<Integer>>>();
    for (Map.Entry<HashSet<Integer>, Integer> entry : map.entrySet()) {
        outcomes.add(asMap(entry.getValue(), entry.getKey())); 
        // add() gives compiler error: The method add(Map<Integer,Set<Integer>>)
        // in the type List<Map<Integer,Set<Integer>>> is not applicable for 
        // the arguments (Map<Integer,HashSet<Integer>>)
    }
    return outcomes;
}

public <K, V> Map<K, V> asMap(K k, V v) {
    Map<K, V> result = new HashMap<K, V>();
    result.put(k, v);
    return result;
}

, (Set ) (HashSet ) . :

public List<Map<Integer, Set<Integer>>> getOutcomes() {
    Map<Set<Integer>, Integer> map = new HashMap<Set<Integer>, Integer>();
    List<Map<Integer, Set<Integer>>> outcomes = new ArrayList<Map<Integer, Set<Integer>>>();
    for (Map.Entry<Set<Integer>, Integer> entry : map.entrySet()) {
        outcomes.add(asMap(entry.getValue(), entry.getKey()));
        // add() now compiles fine.
    }
    return outcomes;
}

For future problems, try asking how to solve a specific problem, and not how to reach a specific solution (which, in turn, may not be the right solution).

+2
source

All Articles