Check if a Java ArrayList contains a given item or notJava 8Object Oriented ProgrammingProgramming Show The java.util.ArrayList.contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present. A program that demonstrates this is given as follows − 1. ArrayList contains() syntaxThe contains() method is pretty simple. It simply checks the index of element in the list. If the index is greater than '0' than element is present in the list. public boolean contains(Object o) { return indexOf(o) >= 0; }Arraylist.contains() in Java
ArrayList contains() method in Java is used for checking if the specified element exists in the given list or not. Syntax: public boolean contains(Object) object-element to be searched forParameters: Returns: Code#1: Demonstrate the working of the method contains() in integer
Output: The list contains 2 The list does not contains 5 Code#2: Demonstrate the working of the method contains() in string
Output: The list contains geeks The list does not contains coding Practical Application: Reference: Article Tags : Java Java - util package Java-ArrayList Java-Collections java-list Practice Tags : Java Java-Collections ArrayList contains() ExampleLet’s look at the program with an Integer type. import java.util.ArrayList; public class Demo1 { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(12); numbers.add(43); numbers.add(54); numbers.add(23); numbers.add(89); numbers.add(76); System.out.println("ArrayList elements : " + numbers); System.out.println("Does the list have 54 : " + numbers.contains(54)); System.out.println("Does the list have 45 : " + numbers.contains(45)); } }Output: ArrayList elements : [12, 43, 54, 23, 89, 76] Does the list have 54 : true Does the list have 45 : falseArrayList contains(Object o) method in javaLet’s learn ArrayList contains(Object o) method in java. ArrayList contains(Object o) method in javacontains(Object o) method of ArrayList class returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that Objects.equals(o, e). Syntax: public boolean contains(Object o) Parameters: o element whose presence in this list is to be tested. Returns: true if this list contains the specified element. Now let’s see example on ArrayList contains(Object o) method. import java.util.ArrayList; public class ArrayListContainsMethodExample { public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(5); names.add("virat"); names.add("dhoni"); names.add("rohit"); names.add("vijay"); names.add("ajay"); System.out.println("Names: " + names); System.out.print("Does list contains name dhoni?: "); System.out.println(names.contains("dhoni")); System.out.print("Does list contains name Sudhakar?: "); System.out.println(names.contains("Sudhakar")); } } Output: Names: [virat, dhoni, rohit, vijay, ajay] Also read – java overview Posted in: JavaTagged: ArrayList contains(Object o) method in java ArrayList contains() MethodBelow is the syntax of the contains() method, defined in ArrayList class: public boolean contains(Object o)This method takes one object as its parameter. It checks if this object is in the ArrayList or not.It returns one boolean value. If the ArrayList contains at least one element, then it returns true. Else it returns false. It tries to find out at least one element vsuch that (o == null ? v == null : o.equals(v)). i.e. if ois null,it returns trueonly if one element isnull in the ArrayList. If ois not null, it returns trueonly if at least one element equal to v. |