Home » Java programming language

Java LinkedList boolean contains(Object o) method with Example

Java LinkedList boolean contains(Object o) method: Here, we are going to learn about the boolean contains(Object o) method of LinkedList class with its syntax and example.
Submitted by Preeti Jain, on June 17, 2019

LinkedList boolean contains(Object o) method

  • This method is available in package java.util.Collection and here, Collection is an interface.
  • This method is declared in interface Collection and implemented by the LinkedList Class.
  • This method is used to check or identify whether an element or Object exists or not in the Linked list.
  • In this method, if Object exists in the linked list then it returns true else false.

Syntax:

    boolean  contains(Object o){
    }

Parameter(s):

This method accepts one parameter.

Return value:

The return type of this method is boolean that means this method returns true if the searched element is present in the linked list else it will return false after execution.

Java program to demonstrate example of LinkedList contains(Object o) method

import java.util.LinkedList;

public class LinkList {
 public static void main(String[] args) {

  // Create an object of linked list 
  LinkedList list = new LinkedList();

  // use add() method to add few elements in the linked list 
  list.add(10);
  list.add(20);
  list.add(30);
  list.add(40);
  list.add(50);

  //  Current Linked list Output
  System.out.println("The  Current Linked List is :" + list);

  // With the help of contains(Object o) we will check 
  // whether 20 exists or not exists in the linked list. 

  System.out.println("The contains() will return or false : " + list.contains(20));
 }
}

Output

D:\Programs>javac LinkList.java

D:\Programs>java LinkList
The  Current Linked List is :[10, 20, 30, 40, 50]
The Searching Element is:true



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.