12

I have a List<Object> and I want to return the first value that it finds true which matches a predicate.

I found that I can use CollectionUtils.find(collection,predicate) (Apache commons). Assuming that the Object contains a integer variable called : value , how do i specify in the predicate that the value can be 1,2,3,4,5 and to discard those that dont match. Is it possible to do 'contains'.

Also not using java 8 so unable to do stream.

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
Rory Lester
  • 2,858
  • 11
  • 49
  • 66

3 Answers3

33

To return the first element in the list which matches the given predicate:

MyObject res = CollectionUtils.find(myList, new Predicate<MyObject>() {
    @Override
    public boolean evaluate(MyObject o) {
        return o.getValue() >= 1 && o.getValue() <= 5;
    }
});

To filter the list so that it only contains elements matching the predicate:

CollectionUtils.filter(myList, new Predicate<MyObject>() {
    @Override
    public boolean evaluate(MyObject o) {
        return o.getValue() >= 1 && o.getValue() <= 5;
    }
});

You can notice that the Predicate<MyObject> is the same.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • I thought th OP wanted to filter the list (`discard those that dont match`). You are just finding the first object matching the predicate. – Dici Sep 01 '14 at 13:16
  • 4
    Worth noting that the generic `Predicate` is only present in version 4 – blgt Sep 01 '14 at 13:16
  • No you are right, I am using the find because I want to return the first value that it matches. Thank you – Rory Lester Sep 01 '14 at 13:17
  • 1
    What should the predicate be? since i am getting The type Predicate is not generic; it cannot be parameterized with arguments . – Rory Lester Sep 01 '14 at 13:39
  • 1
    So you need to cast the ``Object`` to its actual type, the one that contains the ``value`` variable – Jean Logeart Sep 01 '14 at 13:41
  • Yeah have tried that but still doesnt work, same error. – Rory Lester Sep 01 '14 at 13:48
  • It is the same issue : Predicate is not generic; it cannot be parameterized with arguments . Seems to be because I am using collections, it is using the predicate from that instead of the default predicate that does allow parameters. Is there a way around this without passing a parameter? – Rory Lester Sep 01 '14 at 14:01
  • ``MyObject`` must be replaced by the type of the elements contains in your list, the type that contains the ``value`` variable. – Jean Logeart Sep 01 '14 at 14:03
5

In Java 8 you can write

Optional<Integer> found = list.stream().filter(i -> i >= 1 && i <= 5).findAny();

Before Java 7 the simplest solution is to use a loop.

Integer found = null;
for(integer i : list)
   if (i >= 1 && i <= 5) {
        found = i;
        break;
   }

This would be the cleanest and fastest way as Java 7 doesn't have support for lambdas.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

You can use Collections.removeIf (I'm assuming you are using JDK 8). You can also use a Stream :

list = list.stream().filter(predicate).collect(Collectors.toList());

Using Apach Commons Collections, you can use CollectionUtils.filter.

Dici
  • 25,226
  • 7
  • 41
  • 82