Generic Method in Java with Map

来源:互联网 发布:电磁场仿真软件 编辑:程序博客网 时间:2024/06/06 17:05

There is a chance to use generic and map in Java today and get the notes as the following:

The generic type should be declared ahead of the name of the method, not as in C# after the name.

    public static <K,V> boolean printMap(Map<K, V> m){        Set<Map.Entry<K,V>> es = m.entrySet();        for(Map.Entry<K,V> ent : es){            System.out.println("Entry: "+ent.getKey()+" - "+ent.getValue());        }        return true;    }
It is used to printout the contents of the map as the type map does not support `for' enumeration.