-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEATURE: add tools for merging of maps #4017
base: master
Are you sure you want to change the base?
Conversation
7578055
to
94c3bd2
Compare
@hosuaby I'm not a member of the Guava team or even a Googler, just so that you're aware, but have you considered merging your maps together like the following? // If you're able to turn your Map<String, Set<String>>s into SetMultimap<String, String>s beforehand:
var merged =
ImmutableSetMultimap.builder()
.putAll(multimapTags1)
.putAll(multimapTags2)
.build();
// Otherwise:
var merged =
Stream.of(tags1, tags2)
.flatMap(tags -> tags.entries().stream())
.collect(toImmutableSetMultimap(Entry::getKey, Entry::getValue)); |
Oops, sorry, I didn't realise that one of my examples above prints the wrong value! Here's my updated code snippet: // If you're able to turn your Map<String, Set<String>>s into SetMultimap<String, String>s beforehand:
var merged =
ImmutableSetMultimap.builder()
.putAll(multiTags1)
.putAll(multiTags2)
.build();
// Otherwise:
var merged =
Stream.of(tags1, tags2)
.flatMap(tags -> tags.entrySet().stream())
.collect(flatteningToImmutableSetMultimap(Entry::getKey, entry -> entry.getValue().stream())); |
94c3bd2
to
f0194d5
Compare
Hello, @jbduncan It's all the question of simplicity. In your example, the user must "prepare" the maps by converting them into The second point, in your example, We are looking to merge maps of But, thank you for your suggestion of If you have, any questions, suggestions, objections do not hesitate to comment. I try my best to make those tools the most generic and convenient to as much use-cases as possible. |
@hosuaby Okay, thanks for explaining! If or when the Guava team get around to reviewing this, the first thing that they'll probably say is they'll need to internally discuss the API first, as mentioned in the contribution docs and the project philosophy. And even then there's no guarantee it will end up in Guava, because they may think that existing APIs, like in my example above, are good enough. Just so that you're warned. :) |
The biggest question would come from the resolution of conflicts. You are conveniently speaking about I believe that you'd be best using |
@ogregoire That's what public void testGithub() {
Map<String, Collection<String>> tags1 = ImmutableMap.of(
"guava", newHashSet("java", "collections"));
Map<String, Collection<String>> tags2 = ImmutableMap.of(
"guava", newHashSet("optionals", "java", "library"));
Map<String, Collection<String>> asSet = Maps.merge(tags1, tags2, (c1, c2) -> newHashSet(Iterables.concat(c1, c2)));
assertThat(asSet.get("guava")).hasSize(4);
assertThat(asSet.get("guava")).containsExactly("java", "collections", "optionals", "library");
Map<String, Collection<String>> asList = Maps.merge(tags1, tags2, (c1, c2) -> newArrayList(Iterables.concat(c1, c2)));
assertThat(asList.get("guava")).hasSize(5);
assertThat(asList.get("guava")).containsExactly("java", "collections", "optionals", "java", "library");
} |
The present PR adds methods for merging of maps.
Merging of maps is an operation that combines content of two or more maps into a new map while handles entries with duplicate key. Example:
The present PR add new methods to class
Maps
:Map merge(firstMap, secondMap, mergeFunction)
- merges two mapsMap mergeAll(mergeFunction, firstMap, Map... otherMaps)
- merges multiple maps, using signature with varagrsMap mergeAll(mergeFunction, Iterable maps)
- merges multiple mapsCollector mergeCollector(Collector<V, ?, V> downstream)
- creates a collector able to merge stream of mapsReal life use-case:
Assuming we have multiple data sources that returns data in form of maps. We would like to aggregate those data by merging those maps. Unit tests give us an example using measurements provided by different sources.