- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java LinkedList contains() Method
Description
The Java LinkedList contains(Object) method returns true if this list contains the specified element. The object should have implemented equals() method in order to make this operation successful.
Declaration
Following is the declaration for java.util.LinkedList.contains() method
public boolean contains(Object o)
Parameters
o − The element whose presence in this list is to be tested.
Return Value
This method returns true if this list contains the specified element.
Exception
NA
Checking Existence of an Element in a LinkedList of Integers Example
The following example shows the usage of Java LinkedList contains() method. We're using Integers. We'll be adding few elements and then checking if certain element is present or not.
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<Integer> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add(20); linkedList.add(30); linkedList.add(10); linkedList.add(18); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); // linkedList contains element 10 if (linkedList.contains(10)) { System.out.println("element 10 is present in the linkedList"); } else { System.out.println("element 10 is not present in the linkedList"); } // linkedList does not contain element 25 if (linkedList.contains(25)) { System.out.println("element 25 is present in the linkedList"); } else { System.out.println("element 25 is not present in the linkedList"); } } }
Output
Let us compile and run the above program, this will produce the following result −
LinkedList = [20, 30, 10, 18] element 10 is present in the linkedList element 25 is not present in the linkedList
Checking Existence of an Element in a LinkedList of Strings Example
The following example shows the usage of Java LinkedList contains() method with Strings. We'll be adding few elements and then checking if certain element is present or not.
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<String> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add("Welcome"); linkedList.add("to"); linkedList.add("tutorialspoint"); linkedList.add(".com"); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); // linkedList contains element tutorialspoint if (linkedList.contains("tutorialspoint")) { System.out.println("element tutorialspoint is present in the linkedList"); } else { System.out.println("element tutorialspoint is not present in the linkedList"); } // linkedList does not contain element html if (linkedList.contains("html")) { System.out.println("element html is present in the linkedList"); } else { System.out.println("element html is not present in the linkedList"); } } }
Output
Let us compile and run the above program, this will produce the following result −
LinkedList = [Welcome, to, tutorialspoint, .com] element tutorialspoint is present in the deque element html is not present in the deque
Checking Existence of an Element in a LinkedList of Objects Example
The following example shows the usage of Java LinkedList contains() method with Student objects. We'll be adding few elements and then checking if certain element is present or not.
package com.tutorialspoint; import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { // create an empty linkedList LinkedList<Student> linkedList = new LinkedList<>(); // use add() method to add elements in the linkedList linkedList.add(new Student(1, "Julie")); linkedList.add(new Student(2, "Robert")); linkedList.add(new Student(3, "Adam")); // let us print all the elements available in linkedList System.out.println("LinkedList = " + linkedList); // linkedList contains element Robert if (linkedList.contains(new Student(2, "Robert"))) { System.out.println("Student Robert is present in the linkedList"); } else { System.out.println("Student Robert is not present in the linkedList"); } // linkedList does not contain element Jane if (linkedList.contains(new Student(4, "Jane"))) { System.out.println("Student Jane is present in the linkedList"); } else { System.out.println("Student Jane is not present in the linkedList"); } } } class Student { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } }
Output
Let us compile and run the above program, this will produce the following result −
LinkedList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] Student Robert is present in the deque Student Jane is not present in the deque
To Continue Learning Please Login
Login with Google