Skip to content

Instantly share code, notes, and snippets.

@itoshige
Created March 20, 2015 07:38
Show Gist options
  • Save itoshige/15b7d2cbd1cef822187f to your computer and use it in GitHub Desktop.
Save itoshige/15b7d2cbd1cef822187f to your computer and use it in GitHub Desktop.
Java ListをPairに分割する
public interface Target<T> {
boolean apply(T o);
}
/**
* ListをPredicateで指定した条件で分割する
*
* @param collection
* @param predicate
* @return
*/
public static <T> Pair<List<T>, List<T>> separate(List<T> list, Target<T> Target) {
List<T> valid = new ArrayList<T>();
List<T> invalid = new ArrayList<T>();
if ((list != null) && (Target != null)) {
for (T value : list) {
if (Target.apply(value)) {
valid.add(value);
} else {
invalid.add(value);
}
}
}
return new Pair<List<T>, List<T>>(valid, invalid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment