SHARE
TWEET

contains vs anyMatch

DulcetAirman Aug 24th, 2018 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ch.claude_martin;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class SomeClass {
  6.   String someField;
  7.  
  8.   SomeClass(String value) {
  9.     this.someField = value;
  10.   }
  11.  
  12.   public static void main(String[] args) {
  13.     ArrayList<SomeClass> list = new ArrayList<>();
  14.     list.add(new SomeClass("Bob"));
  15.     list.add(new SomeClass("Jane"));
  16.     list.add(new SomeClass("Sepp"));
  17.     list.add(new SomeClass("Renée"));
  18.  
  19.     System.out.println(list.contains(new SomeClass("Bob"))); // false
  20.     System.out.println(list.stream().anyMatch(o -> "Bob".equals(o.someField))); // true
  21.  
  22.   }
  23. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top