泛型在生成key-value的灵活使用

来源:互联网 发布:r语言 空间数据可视化 编辑:程序博客网 时间:2024/05/17 09:34
public interface KeyGenerator<K,V> {        public  K generateKey (V obj );    }    public static <K, V> Map<K, V> convert(List<V> list, KeyGenerator<K, V> kg) {            Map<K, V> map = new HashMap<K, V>();            if (list != null) {                for (int i = 0; i < list.size(); i++) {                    V value = list.get(i);                    K key = kg.generateKey(value);                    map.put(key, value);                }            }            return map;        } 
原创粉丝点击