Java ArrayList contains example shows how to check if ArrayList contains an element in Java. The example also shows how to check if ArrayList contains string or ArrayList contains a custom object.
How to check if ArrayList contains an element?
To check if ArrayList contains an element, use the contains
method.
1 |
public boolean contains(Object o) |
This method returns true if ArrayList contains specified element, false otherwise.
ArrayList contains example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaArrayListContainsExample { public static void main(String[] args) { //create new ArrayList ArrayList<Integer> aListNumbers = new ArrayList<Integer>(); //add some elements to it aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); aListNumbers.add(4); //print ArrayList elements System.out.println( aListNumbers ); /* * To check if ArrayList contains element, use * contains method. */ boolean doesContain = aListNumbers.contains(2); if(doesContain) System.out.println("ArrayList contains 2"); else System.out.println("ArrayList does not contain 2"); } } |
Output
1 2 |
[1, 2, 3, 4] ArrayList contains 2 |
How to check if ArrayList contains a custom class object?
When comparing objects, the contains
method returns true if and only if the ArrayList contains an element e
such that (o == null ? e == null : o.equals(e)).
The ArrayList contains
method uses the equals
method to check if it contains the specified object. If ArrayList contains custom class objects, the class must implement the equals
method in order for the contains
method to work.
Consider below given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaArrayListContainsExample { public static void main(String[] args) { ArrayList<Language> aListLanguage = new ArrayList<Language>(); aListLanguage.add(new Language("Java")); aListLanguage.add(new Language("C")); aListLanguage.add(new Language("C++")); aListLanguage.add(new Language("Cobol")); System.out.println(aListLanguage.contains(new Language("Java"))); } } class Language{ private String name; public Language(){} public Language(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Output
1 |
false |
As you can see from the output, even if the ArrayList contains “Java”, the contains
method returned false. That is because the Language class does not implement the equals
method which is used by the contains
method to compare the objects. Let’s implement the equals
method in the Language class and re-run the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class JavaArrayListContainsExample { public static void main(String[] args) { ArrayList<Language> aListLanguage = new ArrayList<Language>(); aListLanguage.add(new Language("Java")); aListLanguage.add(new Language("C")); aListLanguage.add(new Language("C++")); aListLanguage.add(new Language("Cobol")); System.out.println(aListLanguage.contains(new Language("Java"))); } } class Language{ private String name; public Language(){} public Language(String name){ this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Language other = (Language) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } |
Output
1 |
true |
Note: it is always recommended to implement the hashCode
method along with the equals
method.
This example is a part of the Java ArrayList tutorial with examples.
Please let me know your views in the comments section below.