GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java Deque remove() Method with Examples
Selected Reading
Java Deque remove() Method with ExamplesDescriptionThe java Deque remove() method retrieves and removes the head of the queue represented by this deque. DeclarationFollowing is the declaration for java.util.Deque.remove() method public E remove() ParametersNA Return ValueThis method returns the head of the queue represented by this deque. ExceptionNoSuchElementException − if this deque is empty. remove() method with Object as parameterDescriptionThe Java Deque remove(Object) method removes a single instance of the specified element from this deque. DeclarationFollowing is the declaration for java.util.Deque.remove(o) method public boolean remove(Object o) Parameterso − The element to be removed from this deque, if present Return ValueThis method returns true if this deque contains the specified element. ExceptionClassCastException − if the class of the specified element is incompatible with this deque (optional) NullPointerException − iif the specified element is null and this deque does not permit null elements (optional) Example #1The following example shows the usage of Java Deque remove() method with Integers. We're creating an Deque of Integers, adding some elements, print it and then use remove() method to remove the first element. As Deque is modified it is printed to check if first element is removed or not. package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeDemo {
public static void main(String[] args) {
// create an empty array deque
Deque<Integer> deque = new ArrayDeque<>();
// use add() method to add elements in the deque
deque.add(25);
deque.add(30);
deque.add(20);
deque.add(18);
// let us print all the elements available in deque
System.out.println("Deque = " + deque);
// it will retrieve first element after removing from deque
System.out.println("Retrieved Element is = " + deque.remove());
// let us print all the elements available in deque again
System.out.println("Deque = " + deque);
}
}
Let us compile and run the above program, this will produce the following result − Deque = [25, 30, 20, 18] Retrieved Element is = 25 Deque = [30, 20, 18] Example #2The following example shows the usage of Java Deque remove() method with Strings. We're creating an Deque of String, adding some elements, print it and then use remove() method to get the first element. As Deque is modified it is printed to check if first element is removed or not. package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeDemo {
public static void main(String[] args) {
// create an empty array deque
Deque<String> deque = new ArrayDeque<>();
// use add() method to add elements in the deque
deque.add("A");
deque.add("B");
deque.add("C");
deque.add("D");
// let us print all the elements available in deque
System.out.println("Deque = " + deque);
// it will retrieve first element after removing from deque
System.out.println("Retrieved Element is = " + deque.remove());
// let us print all the elements available in deque again
System.out.println("Deque = " + deque);
}
}
Let us compile and run the above program, this will produce the following result − Deque = [A, B, C, D] Retrieved Element is = A Deque = [B, C, D] Example #3The following example shows the usage of Java Deque remove(object) method with Student objects. We're creating an Deque of Student, adding some elements, print it and then use remove(object) method to get the a particular student removed. As Deque is modified it is printed to check if that student object is removed or not. package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeDemo {
public static void main(String[] args) {
// create an empty array deque
Deque<Student> deque = new ArrayDeque<>();
// use add() method to add elements in the deque
deque.add(new Student(1, "Julie"));
deque.add(new Student(2, "Robert"));
deque.add(new Student(3, "Adam"));
// let us print all the elements available in deque
System.out.println("Deque = " + deque);
// it will return true after removing Robert from deque
System.out.println("Student removed :" + deque.remove(new Student(2, "Robert")));
// let us print all the elements available in deque again
System.out.println("Deque = " + deque);
}
}
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);
}
}
Let us compile and run the above program, this will produce the following result − Deque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] Student removed : true Deque = [[ 1, Julie ], [ 3, Adam ]] java_util_deque.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |