Java List Contains Method Example

In this example we will discuss the java.util.List.contains method and see how to use it.

The method signature in the Java interface List is :

boolean contains(Object o).

As per the Java documentation, it returns true if and only if this list contains at least one element’s equals method returns true for object which we are searching for.

Lets see an example:


package com.javacodegeeks.example;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

 * Created by anirudh on 26/08/14.
public class ListContainsExample {

    public static void main(String[] args) {

        //make a sample Array List
        List<String> list = new ArrayList<String>();

        System.out.println("The list contains one " + list.contains("one"));
        System.out.println("The list contains two " + list.contains("two"));
        System.out.println("The list contains four " + list.contains("four"));

        //make a sample Linked List
        List<Integer> integerList = new LinkedList<Integer>();

        System.out.println("The list contains 1 " + integerList.contains(1));
        System.out.println("The list contains 2 " + integerList.contains(2));
        System.out.println("The list contains 4 " + integerList.contains(4));


Output :

The list contains one true
The list contains two true
The list contains four false
The list contains 1 true
The list contains 2 true
The list contains 4 false

So, in this example first we made an ArrayList object of String, populated it and by using the Contains method we checked for the occurrence of few specific objects.

The objects which were present in the List returned true and others returned false. Here contains method used String’s equals method to compare the 2 objects for equality.

Similarly, for a LinkedList, we saw that the same behaviour was exhibited.

Comparing Custom Objects

Suppose we have custom objects; then to use contains method properly, we would then need to override the objects equals method as per the equality condition.

Lets see an example:

In the below class User, 2 User objects will be equal if their first name, last name and email addresses are equal.
So we need to override the equals method accordingly.

package com.javacodegeeks.example;

public class User {

	private String firstName;
	private String lastName;
	private String email;

	public User(String firstName, String lastName, String email) {
		this.firstName = firstName;
		this.lastName = lastName; = email;

	//setters and getters
	 * Two users are equal if their firstName, lastName and email address is same.
	public boolean equals(Object obj) {
		return (this.firstName.equals(((User) obj).firstName)
				&& this.lastName.equals(((User) obj).lastName) &&
					.equals(((User) obj).email));


Now, lets see the use of contains method for a list of Users in the example below :

  //Make an array list of custom objects
        List<User> userList = new ArrayList<User>();
        userList.add(new User("ani","bha",""));
        userList.add(new User("ani2","bha2",""));
        userList.add(new User("ani5","bha5",""));
        //create a custom object 
        User user1 = new User("ani", "bha", "");
        System.out.println("The list contains user with name ani bha and email id: : "+userList.contains(user1));
        User user2 = new User("aa","bb","");
        System.out.println("The list contains user with name ani bha and email id: : "+userList.contains(user2));

Its Output would be :

The list contains user with name ani bha and email id: : true
The list contains user with name ani bha and email id: : false

In the above example we created an arrayList of users and checked the ArrayList if it contains the specified User object, with same firstName, lastName and email Address.

Download the Eclipse project of this tutorial:

You can download the full source code of this example here :

Anirudh Bhatnagar

Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Aniket Malik
Aniket Malik
3 years ago

May God bless you

Back to top button