1

can you help me to build a condition. This is the example:

ArrayList<Integer> list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);

I have 2 predicates.

ArrayList<Predicate<Integer>> predicates = new ArrayList<>();
predicates.add(x -> x == 4);
predicates.add(x -> x == 1);

I want to find first element that match first predicate. (In this example that would be 4)

How can this be done?

CappY
  • 1,510
  • 1
  • 17
  • 32
  • And what is the issue? Why are `predicate2` and `predicate3` relevant if only `predicate1` is to be used? Are they in a list? You can still refer to the linked question for how to get the first element satisfying a predicate. – Tunaki Oct 14 '16 at 17:37
  • @Tunaki all predicates will be used. I want first element that matches first condition. I will edit my post so they will be in array. – CappY Oct 14 '16 at 17:39
  • Something like: `Optional result = list.stream().filter(predicate1).findFirst();` – Alexey Oct 14 '16 at 17:46
  • Well, refer to the linked question then, it also asks about finding the first element of a list matching a predicate ([to get the first element of the list](http://stackoverflow.com/questions/2503363/how-can-i-get-a-first-element-from-a-sorted-list)). – Tunaki Oct 14 '16 at 17:48
  • @Tunaki But I want first element that matches first Predicate. In My case I want 4, not 1. ( 1 is the first element, but I want 4 to be found ) Maybe I should foreach over predicates and test 1 by 1. – CappY Oct 14 '16 at 17:51
  • Exactly. The first element that matches the predicate, this is what the linked question also asks, please refer to it. Take a look at what [Alexey commented as well](http://stackoverflow.com/questions/40048976/java-predicate-match-against-first-predicate#comment67374277_40048976). – Tunaki Oct 14 '16 at 17:52
  • Well, you keep repeating that you want the first element matching the *first predicate* and repeatedly fail to explain the relevance of that list of predicates. Get the first predicate like you get the first *anything* from a `List`: `get(0)`, i.e. `Predicate theOnlyRelevantPredicate = predicates.get(0);` Then, proceed like there never was a list of multiple predicates… – Holger Oct 14 '16 at 18:00

0 Answers0