List contains() method in Java with ExamplesThe contains() method of List interface in Java is used for checking if the specified element exists in the given list or not. Show Syntax: public boolean contains(Object obj) object-element to be searched forParameters: This method accepts a single parameter obj whose presence in this list is to be tested. Return Value: It returns true if the specified element is found in the list else it returns false. Below programs illustrate the contains() method in List: Program 1: Demonstrate the working of the method contains() in List of integer.
Output:
The list contains 2
The list does not contains 5
Program 2: Demonstrate the working of the method contains() in List of string.
Output:
The list contains geeks
The list does not contains coding
Practical Application: In search operations, we can check if a given element exists in a list or not. Reference: https://docs.oracle.com/javase/7/docs/api/java/util/List.html#contains(java.lang.Object)
Article Tags :
Java
Java-Collections Java-Functions java-list Practice Tags :
Java Java-Collections What is the ArrayList.contains() method in Java?The ArrayList.contains() method in Java is used to check whether or not a list contains a specific element. To check if an element is in an array, we first need to convert the array into an ArrayList using the asList() method and then apply the same contains() method to it. 1. OverviewFinding an element in a list is a very common task we come across as developers. In this quick tutorial, we'll cover different ways we can do this with Java. Java ArrayList.contains() MethodLast update on February 26 2020 08:07:31 (UTC/GMT +8 hours)public boolean contains(Object o)The contains() method is used to determines whether an element exists in an ArrayList object. Returns true if this list contains the specified element. Package: java.util Java Platform: Java SE 8 Syntax: contains(Object o)Parameters:
Return Value: Return Value Type: boolean Pictorial presentation of ArrayList.contains() Method Example: ArrayList.contains Method The following example checks whether a particular student is present in a list. import java.util.*; public class test { public static void main(String[] args) { // ArrayList with Capacity 4 ArrayList<String> StudentList = new ArrayList<String>(4); //Added 4 elements StudentList.add("David"); StudentList.add("Tom"); StudentList.add("Rohit"); StudentList.add("Paul"); System.out.println("Students in the list are : "); System.out.println(StudentList); System.out.print("Is list contains the student Tom?"); System.out.println(StudentList.contains("Tom")); System.out.print("Is list contains the student Sudhir?"); System.out.println(StudentList.contains("Sudhir")); } }Output: F:\java>javac test.java F:\java>java test Students in the list are : [David, Tom, Rohit, Paul] Is list contains the student Tom?true Is list contains the student Sudhir?falseJava Code Editor: Previous:isEmpty Method Share this Tutorial / Exercise on : Facebook and Twitter
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; } |