contains(Object o) method of Deque Interface in Java.

contains(Object o) method of Deque Interface in Java.


Posted in : Core Java Posted on : February 5, 2011 at 3:49 PM Comments : [ 0 ]

In this method you can test the presence of the specified element into the underlying deque.

contains(Object o) method of Deque Interface in Java.

In this section we will discuss how can contains(Object o) method be implemented in Deque interface in java.

Syntax

boolean contains(Object o)

This method returns either 'true' if the underlying deque contains the specified element or returns false.

In this method you can test the presence of the specified element into the underlying deque. Generally, this method returns true if and only if at least there is one element.

Parameter description

Object o : It takes an element to test for the presence into the underlying deque.

Example of contains(Object o) method

In this example we will show how does contains(Object o) method work in Deque interface. In the following example we will make an ArrayDeque type Deque using add( e) method and also inserted the element at the front and last position in deque using addFirst(e) and addLast(e) method respectively, finally we will implement the contains(o) method for checking the presence of a specified element into deque, and find the result in terms of 'true' & 'false'.

Example :

package devmanuals.com;
import java.util.Deque;
import java.util.ArrayDeque;
public class DequeContains {
 public static void main(String args[]) {
    Deque<String> dqa = new ArrayDeque<String>();
    dqa.add("15");
    dqa.add("16");
    dqa.add("17");
    dqa.add("18");
    System.out.println("Elements of previous deque are :" + dqa);
    System.out.println("And the size of deque : " + dqa.size());
    System.out.println("Insert a new element '10' at the front of the deque");
    // This method will add the element at the front of the deque.
    dqa.addFirst("14");
    System.out.println("Then the new Elements of deque are : " + dqa);
    System.out.println("Now insert a new element '15' at the end of the deque");
    // This method will add the element at the end position of the deque.
    dqa.addLast("19");
    System.out.println("Then the new Elements of deque are : " + dqa);
    System.out.println("And the size of new deque = " + dqa.size());
    // This method will test whether the element '19' is present in deque or not. 
    boolean bol = dqa.contains("19");
    System.out.println("Deque has specified element : " + bol);
    if (bol == true)
      System.out.println("Is the specified element present in deque ? : YES");
    else
      System.out.println("Is the specified element present in deque ? : NO");
  }
}

Output :

Elements of previous deque are :[15, 16, 17, 18]

And the size of deque : 4

Insert a new element '10' at the front of the deque

Then the new Elements of deque are : [14, 15, 16, 17, 18]

Now insert a new element '15' at the end of the deque

Then the new Elements of deque are : [14, 15, 16, 17, 18, 19]

And the size of new deque = 6

Deque has specified element : true

Is the specified element present in deque ? : YES

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics