LinkedList contains() method with example in java

Published by user on

The contains(E element) method of LinkedList checks if the LinkedList contains the specified element or not. It returns true if the linked list contains the specified element.

Syntax

boolean contains(E e)

Parameter

e is the element to check in the linked-list

Returns Value

It returns true if the linked-list contains specified element otherwise returns false

Program

import java.util.LinkedList;

public class LinkedListContainsExample {

        public static void main(String[] args) {

                LinkedList oddNumbers = new LinkedList();
                oddNumbers.add(5);
                oddNumbers.add(7);
                oddNumbers.add(9);
                oddNumbers.add(11);

                System.out.println("Linked List elements: " + oddNumbers);
                System.out.println("Does this linked-list contains 7: " + oddNumbers.contains(7));

                System.out.println("Does this linked-list contains 4: " + oddNumbers.contains(4));
        }
}

Output

Linked List elements: [5, 7, 9, 11]
Does this linked-list contains 7: true
Does this linked-list contains 4: false
Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *