In the previous article, we have discussed about Java LinkedList clone() Method with Examples
In this article we are going to see the use of Java LinkedList contains() method along with suitable examples.
Java LinkedList contains() Method with Examples
This java.util.LinkedList.contains()
method is used to check if the specified element in the LinkedList is present or not. It returns true if that specific element is present else it returns false.
Syntax:
LinkedListName.contains()
Where,
LinkedListName
refers to the name of your LinkedList.
Let’s see different examples to understand it more clearly.
Example-1: Java LinkedList contains() Method – Example with String Type LinkedList
Approach:
- Create a new LinkedList of type String.
- Add string elements into the LinkedList using the add() method.
- Display the LinkedList elements.
- Now, using the contains( ) method you can check if the desired element is present or not in the LinkedList.
- Print the result as true/false.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a LinkedList of string datatype LinkedList<String> l = new LinkedList<String>(); // Adding some elements to the LinkedList l.add("Hello"); l.add("this"); l.add("is"); l.add("an"); l.add("example of contains() method"); // Prints the LinkedList elements System.out.println("The elements of LinkedList are: "+l); // checking specific elements in the LinkedList System.out.println("Is Hello present in the LinkedList: "+l.contains("Hello")); System.out.println("Is hello present in the LinkedList: "+l.contains("hello")); } }
Output: The elements of LinkedList are: [Hello, this, is, an, example of contains() method] Is Hello present in the LinkedList: true Is hello present in the LinkedList: false
Example-2: Java LinkedList contains() Method – Example with Integer Type LinkedList
Approach:
- Create a new LinkedList of type Integer.
- Add Integer elements into the LinkedList using the add() method.
- Display the LinkedList elements.
- Now, using the contains( ) method we can check if the desired element is present or not in the LinkedList.
- Print the result as true/false.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a LinkedList of Integer datatype LinkedList<Integer> l = new LinkedList<Integer>(); // Adding some elements to the LinkedList l.add(10); l.add(9); l.add(8); l.add(7); l.add(6); // Prints the LinkedList elements System.out.println("The elements of LinkedList are: "+l); // checking specific elements in the LinkedList System.out.println("Is 9 present in the LinkedList: "+l.contains(9)); System.out.println("Is 99 present in the LinkedList: "+l.contains(99)); } }
Output: The elements of LinkedList are: [10, 9, 8, 7, 6] Is 9 present in the LinkedList: true Is 99 present in the LinkedList: false
Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.
Related Java Programs: