List

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:

ListContainsExample

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>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("five");

        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>();
        integerList.add(1);
        integerList.add(2);
        integerList.add(3);
        integerList.add(5);

        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:

User.java

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) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
	}

	//setters and getters
         
	/**
	 * Two users are equal if their firstName, lastName and email address is same.
	 */
	@Override
	public boolean equals(Object obj) {
		return (this.firstName.equals(((User) obj).firstName)
				&& this.lastName.equals(((User) obj).lastName) && this.email
					.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","abcd@gg.com"));
        userList.add(new User("ani2","bha2","abcd2@gg.com"));
        userList.add(new User("ani5","bha5","abcd5@gg.com"));
        
        //create a custom object 
        User user1 = new User("ani", "bha", "abcd@gg.com");
        System.out.println("The list contains user with name ani bha and email id: abcd@gg.com : "+userList.contains(user1));
        
        User user2 = new User("aa","bb","aa@bb.com");
        System.out.println("The list contains user with name ani bha and email id: aa@bb.com : "+userList.contains(user2));
   ....

Its Output would be :

The list contains user with name ani bha and email id: abcd@gg.com : true
The list contains user with name ani bha and email id: aa@bb.com : 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:

Download
You can download the full source code of this example here : JavaListContainsMethodExample.zip

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.
Subscribe
Notify of
guest

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

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

Thanks…
May God bless you

Back to top button