@Test public void testGroupBy() { List<Pair<Integer,Integer>> list = new ArrayList<>(); for(int i = 0; i < 10; i++) { for(int j = 0; j < 5; j++) { list.add(Pair.of(i, j)); } } Map<Integer, List<Integer>> integerIterableMap = FunctionalUtils.groupByKey(list); assertEquals(10,integerIterableMap.keySet().size()); assertEquals(5,integerIterableMap.get(0).size()); }
public static <K,V> Map<K,List<V>> groupByKey(List<Pair<K,V>> listInput) { Map<K,List<V>> ret = new HashMap<>(); for(Pair<K,V> pair : listInput) { List<V> currList = ret.get(pair.getFirst()); if(currList == null) { currList = new ArrayList<>(); ret.put(pair.getFirst(),currList); } currList.add(pair.getSecond()); } return ret; }
FunctionalUtils { public static <K,V> Map<K,List<V>> groupByKey(List<Pair<K,V>> listInput) { Map<K,List<V>> ret = new HashMap<>(); for(Pair<K,V> pair : listInput) { List<V> currList = ret.get(pair.getFirst()); if(currList == null) { currList = new ArrayList<>(); ret.put(pair.getFirst(),currList); } currList.add(pair.getSecond()); } return ret; } }
FunctionalUtils { public static <K,V> Map<K,List<V>> groupByKey(List<Pair<K,V>> listInput) { Map<K,List<V>> ret = new HashMap<>(); for(Pair<K,V> pair : listInput) { List<V> currList = ret.get(pair.getFirst()); if(currList == null) { currList = new ArrayList<>(); ret.put(pair.getFirst(),currList); } currList.add(pair.getSecond()); } return ret; }  }
FunctionalUtils { public static <K,V> Map<K,List<V>> groupByKey(List<Pair<K,V>> listInput) { Map<K,List<V>> ret = new HashMap<>(); for(Pair<K,V> pair : listInput) { List<V> currList = ret.get(pair.getFirst()); if(currList == null) { currList = new ArrayList<>(); ret.put(pair.getFirst(),currList); } currList.add(pair.getSecond()); } return ret; }  static Map<K,Pair<List<V>,List<V>>> cogroup(List<Pair<K,V>> left,List<Pair<K,V>> right); static Map<K,List<V>> groupByKey(List<Pair<K,V>> listInput); static List<Pair<K,V>> mapToPair(Map<K,V> map); }
FunctionalUtils { public static <K,V> Map<K,List<V>> groupByKey(List<Pair<K,V>> listInput) { Map<K,List<V>> ret = new HashMap<>(); for(Pair<K,V> pair : listInput) { List<V> currList = ret.get(pair.getFirst()); if(currList == null) { currList = new ArrayList<>(); ret.put(pair.getFirst(),currList); } currList.add(pair.getSecond()); } return ret; }  static Map<K,Pair<List<V>,List<V>>> cogroup(List<Pair<K,V>> left,List<Pair<K,V>> right); static Map<K,List<V>> groupByKey(List<Pair<K,V>> listInput); static List<Pair<K,V>> mapToPair(Map<K,V> map);  }