To do this, you need to override the implementation of contains() . I give you a simple example.
Custom class ArrayList
public class MyArrayList extends ArrayList<String> { private static final long serialVersionUID = 2178228925760279677L; @Override public boolean contains(Object o) { return indexOf(o) >= 0; } @Override public int indexOf(Object o) { int size = this.size(); if (o == null) { for (int i = 0; i < size ; i++) { if (this.get(i) == null) { return i; } } } else { for (int i = 0; i < size ; i++) { if (this.get(i).contains(String.valueOf(o))) { return i; } } } return -1; } }
How to use
MyArrayList arrayList = new MyArrayList(); arrayList.add("http://www.google.com"); arrayList.add("https://www.stackoverflow.com"); arrayList.add("http://pankajchunchun.wordpress.com"); if (arrayList.contains("google")) { System.out.println("ArrayList Contains google word"); } if (arrayList.contains("igoogle")) { System.out.println("ArrayList Contains igoogle word"); } else { System.out.println("ArrayList does not Contains igoogle word"); }
Below is the sample code above
ArrayList Contains google word ArrayList does not Contains igoogle word
For more information, see ArrayList Source Code .
source share