Skip to content

Instantly share code, notes, and snippets.

@lonniev
Last active March 27, 2017 02:47
Show Gist options
  • Save lonniev/327144b5654f7bbeb157d91900f4cf2e to your computer and use it in GitHub Desktop.
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.
// 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