Java ArrayList contains() method with example

Java ArrayList contains() method 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 (o==null ? e==null : o.equals(e)).

Method Signature

public boolean contains(Object o)

Specified by:

contains method in interface Collection

Specified by:

contains method in interface List

Overrides

Overrides:contains in class AbstractCollection

Parameters:

o – element whose presence in this list is to be tested

Returns:

true if this list contains the specified element

ArrayList contains() method implementation

ArrayList contains(Object o) method internally refer index(Object o) method.

public boolean contains(Object o) {
        return indexOf(o) >= 0;
    }

Java ArrayList contains() method example

package com.ehowtonow.java.util.arraylist;

import java.util.ArrayList;

public class ContainsExample {

	public static void main(String[] args) {
		// create empty array list with String type
		ArrayList<String> stringList = new ArrayList<String>();

		// add() method to add String element in the list
		stringList.add("Welcome");
		stringList.add("To");
		stringList.add("Java");
		stringList.add("ArrayList");

		// create empty array list with Integer type
		ArrayList<Integer> numericList = new ArrayList<Integer>();

		// add() method to add Integer element in the list
		numericList.add(10);
		numericList.add(15);
		numericList.add(20);
		numericList.add(25);
		numericList.add(30);

		boolean listContains = stringList.contains("java");

		System.out.println("List contains java :  " + listContains);

		listContains = stringList.contains("Java");

		System.out.println("List contains Java :  " + listContains);

		listContains = numericList.contains(10);

		System.out.println("List contains 10 :  " + listContains);

		listContains = numericList.contains(11);

		System.out.println("List contains 11 :  " + listContains);

		for (Integer integer : numericList) {
			if (numericList.contains("25")) {
				System.out.println("If list contains 25 then do something");
			}
		}

	}

}

Output

List contains java : false
List contains Java : true
List contains 10 : true
List contains 11 : false


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java ArrayList contains() method with example
  2. Java ArrayList removeAll() method with example
  3. Java ArrayList isEmpty() method with example
  4. Java ArrayList addAll(Collection< ? extends E> c) method with example
  5. Java ArrayList get() method with example
  6. Java ArrayList remove(Object o) method with example
  7. Java ArrayList add(E e) method with example
  8. Java ArrayList remove() method with example
  9. Java ArrayList clear() method with example
  10. Java ArrayList indexOf() method with example
  11. Java ArrayList addAll(int index, Collection c) method with example
  12. Java ArrayList forEach() method with example
  13. Java ArrayList clone() method with example
  14. Java ArrayList add(int index, E element) method with example
  15. Java ArrayList ensureCapacity() method with example

Be the first to comment

Leave a Reply