commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thomas Neidhart (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (COLLECTIONS-511) CollectionUtils.bisect(...), this would be a combination of Select and SelectRejected
Date Tue, 06 May 2014 22:28:23 GMT

    [ https://issues.apache.org/jira/browse/COLLECTIONS-511?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13991253#comment-13991253
] 

Thomas Neidhart commented on COLLECTIONS-511:
---------------------------------------------

Scala has a partition method that does exactly this.

split would also be good imho, but I do not like selectAndReject.

> CollectionUtils.bisect(...), this would be a combination of Select and SelectRejected
> -------------------------------------------------------------------------------------
>
>                 Key: COLLECTIONS-511
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-511
>             Project: Commons Collections
>          Issue Type: New Feature
>          Components: Collection
>            Reporter: Nathan Blomquist
>            Priority: Trivial
>
> I recently needed a way to use a Predicate to select things from a list, but I also wanted
to know which ones failed the predicate test.
> I wanted the following, but with one iteration instead of two:
> {code}
> List originalList = (...)
> Predicate p = (...)
> Collection selected = CollectionUtils.select(originalList, p);
> Collection rejected = CollectionUtils.selectRejected(originalList, p);
> // handle the selected cases (save them or whatnot)
> // now throw an error message or handle the rejected cases
> {code}
> This is what I came up with based on the CollectionUtils.select(...) method:
> {code:java}
> 	public static <O, R extends Collection<? super O>> void bisect(
> 			final Iterable<? extends O> inputCollection,
> 			final Predicate<? super O> predicate, 
> 			final R selectedCollection,
> 			final R rejectedCollection) {
> 		if (inputCollection != null && predicate != null) {
> 			for (final O item : inputCollection) {
> 				if (predicate.evaluate(item)) {
> 					selectedCollection.add(item);
> 				}else{
> 					rejectedCollection.add(item);
> 				}
> 			}
> 		}
> 	}
> 	
> 	public static void main(String[] args){
> 		// this will test the bisection code
> 		List<String> original = Arrays.asList(
> 				"testString1",
> 				"testString2",
> 				"testString3",
> 				"String1",
> 				"String2",
> 				"String3",
> 				"testString4",
> 				"String4",
> 				"testString5"
> 				);
> 		
> 		List<String> selected = new ArrayList<String>();
> 		List<String> rejected = new ArrayList<String>();
> 		
> 		Predicate<String> beginsWithTestPredicate =
> 		new Predicate<String>() {
> 			public boolean evaluate(String object) {
> 				return object.startsWith("test");
> 			}
> 		};
> 		
> 		bisect(original, beginsWithTestPredicate, selected, rejected);
> 		
> 		System.out.println("Size of selected (should be 5):" 
> 				+ selected.size());
> 		System.out.println("Size of rejected (should be 4):"
> 				+ rejected.size());
> 	}
> {code}
> This will of course throw a NullPointerException if either output collection is null.
 This seems appropriate since we need to return two outputs anyway.
> Not sure if *bisect* is the best name, but this method will split the original into two
pieces. https://www.google.com/#q=define+bisect



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Mime
View raw message