Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use apache commons collection implementing a Predicate for it.</p> <p><a href="http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html" rel="noreferrer">http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html</a></p> <p>Sample:</p> <pre><code>package snippet; import java.util.Arrays; import java.util.Collection; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; public class TestCollection { public static class User { private String name; public User(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [name=" + name + "]"; } } public static void main(String[] args) { Collection&lt;User&gt; users = Arrays.asList(new User("User Name 1"), new User("User Name 2"), new User("Another User")); Predicate predicate = new Predicate() { public boolean evaluate(Object object) { return ((User) object).getName().startsWith("User"); } }; Collection filtered = CollectionUtils.select(users, predicate); System.out.println(filtered); } } </code></pre> <p>A few sample can be found here: <a href="http://apachecommonstipsandtricks.blogspot.de/2009/01/examples-of-functors-transformers.html" rel="noreferrer">http://apachecommonstipsandtricks.blogspot.de/2009/01/examples-of-functors-transformers.html</a></p> <p>And if you need something more generic, like inspect a value of a specific field or property you could do something like:</p> <pre><code>public static class MyPredicate implements Predicate { private Object expected; private String propertyName; public MyPredicate(String propertyName, Object expected) { super(); this.propertyName = propertyName; this.expected = expected; } public boolean evaluate(Object object) { try { return expected.equals(PropertyUtils.getProperty(object, propertyName)); } catch (Exception e) { return false; } } } </code></pre> <p>That could compare a specific property to a expected value, and the use would be something like:</p> <pre><code>Collection filtered = CollectionUtils.select(users, new MyPredicate("name", "User Name 2")); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload