Last active
March 27, 2017 02:47
-
-
Save lonniev/327144b5654f7bbeb157d91900f4cf2e to your computer and use it in GitHub Desktop.
Given one collection, split it into several subsets based on either a Function or a Function based on a Predicate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// partition a collection of properties into the two sets | |
// of those that satisfy a predicate and those that do not | |
final ImmutableListMultimap<Boolean, Entry<String, Object>> propertiesPair = | |
Multimaps.index(domainModelDTO.getProperties().entrySet(), | |
Functions.forPredicate(predicateMethod())); | |
final FluentIterable<Entry<String, Object>> truePropertiesList = | |
FluentIterable.from( propertiesPair.get( true ) ); | |
final FluentIterable<Entry<String, Object>> falsePropertiesList = | |
FluentIterable.from( propertiesPair.get( false ) ); | |
// more than two subsets can be obtained by using a key-generating Function<V, K> instead | |
// of this simple Boolean-generating Predicate funtion | |
// The separate FluentIterables can be turned into their own Maps with | |
// Map<String, Object> map = ImmutableMaps.copyOf( truePropertiesList ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment