ListMap extends HashMap<T, List<V>> { public static <T, V> ListMap<T, V> getListMap( List<V> values, Function<V, T> keyMapper ) { ListMap<T, V> map = new ListMap<>(); for ( V value : values ) { T key = keyMapper.apply( value ); map.putValue( key, value ); } return map; } ListMap();  ListMap( ListMap<T, V> listMap ); List<V> putValue( T key, V value ); List<V> putValues( T key, Collection<V> values ); void putValueMap( Map<T, V> map ); void putAll( ListMap<T, V> map ); Collection<V> allValues(); Set<V> uniqueValues( ); boolean containsValue( T key, V value ); static ListMap<T, V> getListMap( List<V> values, Function<V, T> keyMapper ); static ListMap<T, U> getListMap( List<V> values, Function<V, T> keyMapper, Function<V, U> valueMapper ); static ListMap<T, V> union( ListMap<T, V> a, ListMap<T, V> b );  }
@Test public void testGetListMapValueMapper() { DataElementGroupSet groupSetA = new DataElementGroupSet( "GroupSetA" ); DataElementGroupSet groupSetB = new DataElementGroupSet( "GroupSetB" ); DataElementGroupSet groupSetC = new DataElementGroupSet( "GroupSetC" ); DataElementGroupSet groupSetZ = new DataElementGroupSet( "GroupSetZ" ); DataElementGroup groupA = new DataElementGroup( "GroupA" ); DataElementGroup groupB = new DataElementGroup( "GroupB" ); DataElementGroup groupC = new DataElementGroup( "GroupC" ); DataElementGroup groupD = new DataElementGroup( "GroupD" ); DataElementGroup groupE = new DataElementGroup( "GroupE" ); DataElementGroup groupF = new DataElementGroup( "GroupF" ); groupA.getGroupSets().add( groupSetA ); groupB.getGroupSets().add( groupSetB ); groupC.getGroupSets().add( groupSetC ); groupD.getGroupSets().add( groupSetA ); groupE.getGroupSets().add( groupSetB ); groupF.getGroupSets().add( groupSetA ); List<DataElementGroup> groups = Lists.newArrayList( groupA, groupB, groupC, groupD, groupE, groupF ); ListMap<DataElementGroupSet, DataElementGroup> map = ListMap.getListMap( groups, group -> group.getGroupSets().iterator().next() ); assertEquals( Lists.newArrayList( groupA, groupD, groupF ), map.get( groupSetA ) ); assertEquals( Lists.newArrayList( groupB, groupE ), map.get( groupSetB ) ); assertEquals( Lists.newArrayList( groupC ), map.get( groupSetC ) ); assertNull( map.get( groupSetZ ) ); }
@Test public void testGetListMapKeyValueMapper() { DataElementGroupSet groupSetA = new DataElementGroupSet( "GroupSetA" ); DataElementGroupSet groupSetB = new DataElementGroupSet( "GroupSetB" ); DataElementGroupSet groupSetC = new DataElementGroupSet( "GroupSetC" ); DataElementGroupSet groupSetZ = new DataElementGroupSet( "GroupSetZ" ); DataElementGroup groupA = new DataElementGroup( "GroupA" ); DataElementGroup groupB = new DataElementGroup( "GroupB" ); DataElementGroup groupC = new DataElementGroup( "GroupC" ); DataElementGroup groupD = new DataElementGroup( "GroupD" ); DataElementGroup groupE = new DataElementGroup( "GroupE" ); DataElementGroup groupF = new DataElementGroup( "GroupF" ); groupA.getGroupSets().add( groupSetA ); groupB.getGroupSets().add( groupSetB ); groupC.getGroupSets().add( groupSetC ); groupD.getGroupSets().add( groupSetA ); groupE.getGroupSets().add( groupSetB ); groupF.getGroupSets().add( groupSetA ); List<DataElementGroup> groups = Lists.newArrayList( groupA, groupB, groupC, groupD, groupE, groupF ); ListMap<DataElementGroupSet, Long> map = ListMap.getListMap( groups, group -> group.getGroupSets().iterator().next(), group -> group.getId() ); assertEquals( Lists.newArrayList( groupA.getId(), groupD.getId(), groupF.getId() ), map.get( groupSetA ) ); assertEquals( Lists.newArrayList( groupB.getId(), groupE.getId() ), map.get( groupSetB ) ); assertEquals( Lists.newArrayList( groupC.getId() ), map.get( groupSetC ) ); assertNull( map.get( groupSetZ ) ); }