田躲躲 田躲躲 - 7 months ago 8
Java Question

How Can I create method which use Java generics put the maps key to list?

public <T> List<T> map2list(Map<T, T> map){

List<T> list = new ArrayList<T>(map.keySet());
return list;

}


My Method like this... But it does not work.

Answer

Maybe your map is empty, because the example below works:

public class Utils {

    public <T, K> List<T> map2list(Map<T, K> map) {
        List<T> list = new ArrayList<T>(map.keySet());
        return list;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("test1", 1);
        map.put("test2", 2);
        map.put("test3", 3);
        Utils t = new Utils();
        List<String> result = t.map2list(map);
        for (String key : result) {
            System.out.println(key);
        }
    }

}

printing the following:

test2
test3
test1

Comments