Java Collection Tutorial - Java LinkedList.contains(Object o)








Syntax

LinkedList.contains(Object o) has the following syntax.

public boolean contains(Object o)

Example

In the following code shows how to use LinkedList.contains(Object o) method.

//from   w  ww . j  av a2 s.c o  m
import java.util.LinkedList;

public class Main {

   public static void main(String[] args) {

      // create a LinkedList
      LinkedList<String> list = new LinkedList<String>();

      // add some elements
      list.add("Hello");
      list.add("from java2s.com");
      list.add("10");

      // print the list
      System.out.println("LinkedList:" + list);

      // check if the list contains "Coffee"
      System.out.println("List contains 'Coffee':" + list.contains("Coffee"));

      // check if the list contains "10"
      System.out.println("List contains '10':" + list.contains("10"));
   }
}

The code above generates the following result.