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 PriorityQueue removeAll() Method
Selected Reading
Java PriorityQueue removeAll() MethodDescriptionThe java PriorityQueue removeAll(Collection<?> c) method removes all of common elements present in the arraydeque object and the provided collection's elements. This method modifies the arraydeque object. DeclarationFollowing is the declaration for java.util.PriorityQueue.removeAll(Collection<?> c) method public boolean removeAll(Collection<?> c) Parametersc − The collection containing elements to be removed from this collection. Return Valuetrue if this arraydeque is changed as a result of the call. ExceptionNullPointerException − if this arraydeque contains one or more null elements and the specified collection does not support null elements, or if the specified collection is null. Removing All Given Elements of a PriorityQueue of Ints ExampleThe following example shows the usage of Java PriorityQueue removeAll() method with Integers. We're creating an PriorityQueue of Integers, adding some elements, print it and then use removeAll() method to remove few elements. As PriorityQueue is modified it is printed to check if specified elements are removed or not. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Arrays;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<Integer> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add(25);
queue.add(30);
queue.add(20);
queue.add(18);
// let us print all the elements available in queue
System.out.println("PriorityQueue = " + queue);
// it will remove two common elements
System.out.println("PriorityQueue modified: " + queue.removeAll(Arrays.asList(11,30,20,12)));
// let us print all the elements available in queue again
System.out.println("PriorityQueue = " + queue);
}
}
OutputLet us compile and run the above program, this will produce the following result − PriorityQueue = [18, 20, 25, 30] PriorityQueue modified: true PriorityQueue = [18, 25] Removing All Given Elements of a PriorityQueue of Strings ExampleThe following example shows the usage of Java PriorityQueue removeAll() method with Strings. We're creating an PriorityQueue of Strings, adding some elements, print it and then use removeAll() method to remove few elements. As PriorityQueue is modified it is printed to check if specified elements are removed or not. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Arrays;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<String> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add("A");
queue.add("B");
queue.add("C");
queue.add("D");
// let us print all the elements available in queue
System.out.println("PriorityQueue = " + queue);
// it will remove two common elements
System.out.println("PriorityQueue modified: " + queue.removeAll(Arrays.asList("E","B","C","F")));
// let us print all the elements available in queue again
System.out.println("PriorityQueue = " + queue);
}
}
OutputLet us compile and run the above program, this will produce the following result − PriorityQueue = [A, B, C, D] PriorityQueue modified: true PriorityQueue = [A, D] Removing All Given Elements of a PriorityQueue of Objects ExampleThe following example shows the usage of Java PriorityQueue removeAll() method with Student objects. We're creating an PriorityQueue of Student objects, adding some elements, print it and then use removeAll() method to remove few students. As PriorityQueue is modified it is printed to check if specified students are removed or not. package com.tutorialspoint;
import java.util.PriorityQueue;
import java.util.Arrays;
public class PriorityQueueDemo {
public static void main(String[] args) {
// create an empty priority queue
PriorityQueue<Student> queue = new PriorityQueue<>();
// use add() method to add elements in the queue
queue.add(new Student(1, "Julie"));
queue.add(new Student(2, "Robert"));
queue.add(new Student(3, "Adam"));
// let us print all the elements available in queue
System.out.println("PriorityQueue = " + queue);
// it will return true after removing two students from queue
System.out.println("Student removed : " + queue.removeAll(
Arrays.asList(new Student(2, "Robert"),new Student(3, "Adam"))));
// let us print all the elements available in queue again
System.out.println("PriorityQueue = " + queue);
}
}
class Student implements Comparable<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);
}
@Override
public int compareTo(Student student) {
return this.rollNo - student.rollNo;
}
}
OutputLet us compile and run the above program, this will produce the following result − PriorityQueue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] Student removed :true PriorityQueue = [[ 1, Julie ]] java_util_priorityqueue.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. |