List根据指定key分组

public interface Key {

K get(V v);

}

public class Group {


public static Map> create(List list, Key key) {
Map> m = new HashMap>();
create(m, list, key);
return m;
}

public static void create(Map> m, List list, Key key) {
        m.clear();
for (V o : list) {
K k = key.get(o);
List l = (List) m.get(k);
if (l == null) {
l = new ArrayList();
m.put(k, l);
}
l.add(o);
}
}
}

你可能感兴趣的:(java,map,分组,list)