Sudhakar Chavali
Sudhakar Chavali

Reputation: 829

Getting subset of elements from array having same value

Is there any collection API method that extracts the same elements from array or collection?

for example SomeClass.getElements("Test") should return subset of an array that holds elements with value "Test".

Reason for asking this question is because I want to avoid my own traverse and condition checks.

Regards,
Sudhakar

Upvotes: 1

Views: 236

Answers (2)

Adam
Adam

Reputation: 44929

If you use the Apache collections api you can do:

Predicate predicate = PredicateUtils.equalPredicate("Test");
Collection result = CollectionUtils.find(someCollection, predicate);

Upvotes: 1

cmonkey
cmonkey

Reputation: 4296

As mentioned in the comments, there are (presently) no easy ways to do this. The best you could do is something akin to http://docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html , where you make an interface that accepts elements while iterating over it.

Alternatively, you can do something like: What is the best way to filter a Java Collection? , which drags you into the land of functional programming.

Upvotes: 2

Related Questions