Skip to content

Instantly share code, notes, and snippets.

@tomaszalusky
Created February 7, 2017 14:07
Show Gist options
  • Save tomaszalusky/d598015a33f4a4f24b08989cceda2906 to your computer and use it in GitHub Desktop.
Save tomaszalusky/d598015a33f4a4f24b08989cceda2906 to your computer and use it in GitHub Desktop.
playing with collectors, requires guava 21.0
import com.google.common.collect.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.stream.Collectors.*;
public class Mapping {
static BiMap<Character,ImmutableSet<String>> indexUsingImmutableSet(Map<String,Character> input) {
BiMap<Character,ImmutableSet<String>> result = input.entrySet().stream()
.collect(groupingBy(Map.Entry::getValue, HashBiMap::create,
mapping(Map.Entry::getKey,
toImmutableSet())));
return result;
}
static BiMap<Character,Set<String>> indexUsingMutableSet(Map<String,Character> input) {
BiMap<Character,Set<String>> result = input.entrySet().stream()
.collect(groupingBy(Map.Entry::getValue, HashBiMap::create,
mapping(Map.Entry::getKey,
toSet())));
return result;
}
static BiMap<Character,Set<String>> indexUsingIntermediateCollector(Map<String,Character> input) {
BiMap<Character,Set<String>> result = input.entrySet().stream()
.collect(groupingBy(Map.Entry::getValue, HashBiMap::create,
collectingAndThen(
mapping(Map.Entry<String,Character>::getKey,
toSet()),
HashSet<String>::new)));
return result;
}
static BiMap<Character,Set<String>> indexUsingMapThenBimap(Map<String,Character> input) {
BiMap<Character,Set<String>> result = input.entrySet().stream()
.collect(collectingAndThen(
groupingBy(Map.Entry::getValue,
mapping(Map.Entry::getKey,
toSet())),
HashBiMap::create));
return result;
}
public static void main(String[] args) {
Map<String,Character> before = ImmutableMap.of("cleaning",'A',"cooking",'A',"washing",'C',"ironing",'C');
Map<String,Character> after = ImmutableMap.of("cleaning",'B',"cooking",'B',"washing",'D',"ironing",'E');
BiMap<Character,? extends Set<String>> indexUsingImmutableSetBefore = indexUsingImmutableSet(before);
BiMap<Character,? extends Set<String>> indexUsingImmutableSetAfter = indexUsingImmutableSet(after);
System.out.println(Sets.intersection(indexUsingImmutableSetBefore.values(), indexUsingImmutableSetAfter.values()));
BiMap<Character,? extends Set<String>> indexUsingMutableSetBefore = indexUsingMutableSet(before);
BiMap<Character,? extends Set<String>> indexUsingMutableSetAfter = indexUsingMutableSet(after);
System.out.println(Sets.intersection(indexUsingMutableSetBefore.values(), indexUsingMutableSetAfter.values()));
BiMap<Character,? extends Set<String>> indexUsingIntermediateCollectorBefore = indexUsingIntermediateCollector(before);
BiMap<Character,? extends Set<String>> indexUsingIntermediateCollectorAfter = indexUsingIntermediateCollector(after);
System.out.println(Sets.intersection(indexUsingIntermediateCollectorBefore.values(), indexUsingIntermediateCollectorAfter.values()));
BiMap<Character,? extends Set<String>> indexUsingMapThenBimapBefore = indexUsingMapThenBimap(before);
BiMap<Character,? extends Set<String>> indexUsingMapThenBimapAfter = indexUsingMapThenBimap(after);
System.out.println(Sets.intersection(indexUsingMapThenBimapBefore.values(), indexUsingMapThenBimapAfter.values()));
/* prints:
[[cleaning, cooking]]
[]
[[cleaning, cooking]]
[[cleaning, cooking]]
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment