dictionary - Java nested lists in map returns an error when passing a list of lists to Map values -
i've class called zipclass holds 2 static methods: - ziptolist() put 2 lists of same size lists of lists; , - ziptomaplist() put list of values keys , lists of lists values keys.
first method works fine:
public static <t> list<list<t>> ziptolist(list<t> list1,list<t> list2) { list<t> tuple = new arraylist<>(); //a list pair of values from same columns (fields) list<list<t>> tuples = new arraylist<>(); // list of lists if(list1.size() == list2.size()) //check size of lists same { for(int idx = 0; idx < list1.size(); idx ++ ) { //feed tuple pair of values tuple.add(list1.get(idx)); tuple.add(list2.get(idx)); //feed list of lists list of paired values tuples.add(copylist(tuple)); //clear pair next iteration tuple.clear(); } } else system.out.println("lists must of same size"); return tuples; }
my second method:
public static <k,v extends list<?>> map<k,list<?>> ziptomaplist(list<k> keys,list<list<?>> values) { map<k,list<?>> datamap = new hashmap<>(); for(int idx = 0; idx < keys.size(); idx ++ ) { datamap.put(keys.get(idx), values.get(idx)); } return datamap; }
i've 2 lists hold integer values: values1 , values2 , i've list of keys holds string values: keys when try print content of datamap: system.out.println(ziptomaplist(keys,ziptolist(values1,values2))); there error message:
the method ziptomaplist(list<k>, list<list<?>>) in type zipclass not applicable arguments (list<string>, list<list<integer>>)
ziptolist() method works fine can't understand happening in second method, guess it's with:
<k,v extends list<?>>
so "k" generic reference type keys. v extends list , presumes hold type of list , inner relates type list. after googling realize illegal way can't understand why. can please explain why? , way overcome problem?
many thanks!
since not using generic type parameter v
anywhere in ziptomaplist
method, suggest using element type of list
:
public static <k,v> map<k,list<v>> ziptomaplist(list<k> keys,list<list<v>> values) { map<k,list<v>> datamap = new hashmap<>(); for(int idx = 0; idx < keys.size(); idx ++ ) { datamap.put(keys.get(idx), values.get(idx)); } return datamap; }
Comments
Post a Comment