contains(Object o) method of List Interface in java.

contains(Object o) method of List Interface in java.


Posted in : Core Java Posted on : January 3, 2011 at 8:09 PM Comments : [ 0 ]

In this method you can search an element from the list.

contains(Object o) method of List Interface in java.

In the following example, we will show you the method of searching an element from a list using contains(Object o) method of List Interface in java.

Syntax

public boolean contains(Object o)

This method returns either True or False.

In this method you can search an element from the list. It searches the element from a specified list and returns True if the element is found and returns False if the element is not found.

parameteric description  :

Object o : It takes an argument as Object type element what do you want to search.

boolean : It returns ' true ' if the specified element is found otherwise ' false '.

Example of contains(Object o ) method

In this example we will show you how does a contains(Object o) method work in List Interface. This example will help you to understand how elements can be searched into the list. Through this example we will show you adding elements to the list, count the number of list, and searching a specified element from the list.

Example:-

package Devmanuals.com;
import java.util.LinkedList;
public class ListContains {
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.add("Ram");
ll.add("Shyam");
ll.add(null);
ll.add(55);
System.out.println("Size of list : "+ll.size());
// Check for element in the list:
boolean bl=ll.contains(55);
System.out.println("Search result : " +bl);
if (bl==true)
System.out.println("The given element is found in the list");
else
System.out.println("The given element is not in list");
}
}

Output:-

Size of list : 4

Search result : true

The given element is found in the list

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics