SetMap extends HashMap<T, Set<V>> { public static <T, V> SetMap<T, V> getSetMap( Set<V> values, Function<V, T> keyMapper ) { SetMap<T, V> map = new SetMap<>(); for ( V value : values ) { T key = keyMapper.apply( value ); map.putValue( key, value ); } return map; } SetMap();  SetMap( SetMap<T, V> setMap ); Set<V> putValue( T key, V value ); Set<V> putValues( T key, Set<V> values ); void putValues( SetMap<T, V> setMap ); static SetMap<T, V> getSetMap( Set<V> values, Function<V, T> keyMapper );  }
@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 ); Set<DataElementGroup> groups = Sets.newHashSet( groupA, groupB, groupC, groupD, groupE, groupF ); SetMap<DataElementGroupSet, DataElementGroup> map = SetMap.getSetMap( groups, group -> group.getGroupSets().iterator().next() ); assertEquals( Sets.newHashSet( groupA, groupD, groupF ), map.get( groupSetA ) ); assertEquals( Sets.newHashSet( groupB, groupE ), map.get( groupSetB ) ); assertEquals( Sets.newHashSet( groupC ), map.get( groupSetC ) ); assertNull( map.get( groupSetZ ) ); }