A LinkedTransferQueue method removeAll() removes all of this collection's elements that are also contained in the specified collection. Once method executed successfully, this queue will contain no elements in common with the specified collection. It returns boolean value true, if this queue changed, otherwise false.
1 public boolean removeAll(Collection<?> c)
c : It is a collection containing an elements to be removed from this queue.
return : It returns boolean value true, if this queue changed, otherwise false.
LinkedTransferQueue method removeAll()
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.concurrent.LinkedTransferQueue;
5
6 public class Main {
7
8 public static void main(String[] args) {
9
10 LinkedTransferQueue<Integer> queue = new LinkedTransferQueue();
11 queue.addAll(Arrays.asList(120, 135, 140, 145));
12 System.out.println("A LinkedTransferQueue elements : " + queue);
13
14
15 boolean res = queue.removeAll(Arrays.asList(120, 135));
16 System.out.println("Does this queue changed : " + res);
17 System.out.println("A LinkedTransferQueue elements : " + queue);
18
19 boolean res1 = queue.removeAll(Arrays.asList(1000, 135));
20 System.out.println("Does this queue changed : " + res1);
21 System.out.println("A LinkedTransferQueue elements : " + queue);
22 }
23 }
In the above example, a LinkedTransferQueue of type Integer created and added elements using addAll() method by passing a collection. A removeAll() method called by passing a collection that removes all of the elements from this queue and return boolean value true, as this queue changed. A removeAll() method called again that returns boolean value false, as this queue not changed. It compares elements using Integer class equals() method.
1 A LinkedTransferQueue elements : [120, 135, 140, 145]
2 Does this queue changed : true
3 A LinkedTransferQueue elements : [140, 145]
4 Does this queue changed : false
5 A LinkedTransferQueue elements : [140, 145]
LinkedTransferQueue method removeAll() with String
LinkedTransferQueue method removeAll() with String
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.concurrent.LinkedTransferQueue;
5
6 public class StringExp {
7
8 public static void main(String[] args) {
9
10 LinkedTransferQueue<String> queue = new LinkedTransferQueue();
11 queue.addAll(Arrays.asList("Welcome", "to", "IOGyan"));
12 System.out.println("A LinkedTransferQueue elements : " + queue);
13
14
15 boolean res = queue.removeAll(Arrays.asList("to", "IOGyan"));
16 System.out.println("Does this queue changed : " + res);
17 System.out.println("A LinkedTransferQueue elements : " + queue);
18
19 boolean res1 = queue.removeAll(Arrays.asList("THANKS"));
20 System.out.println("Does this queue changed : " + res1);
21 System.out.println("A LinkedTransferQueue elements : " + queue);
22 }
23 }
In the above example, a LinkedTransferQueue of type String created and added elements using addAll() method by passing a collection. A removeAll() method called by passing a collection that removes all of the strings from this queue and return boolean value true, as this queue changed.
A removeAll() method called again that returns boolean value false, as this queue not changed. It compares strings using String class equals() method.
1 A LinkedTransferQueue elements : [Welcome, to, IOGyan]
2 Does this queue changed : true
3 A LinkedTransferQueue elements : [Welcome]
4 Does this queue changed : false
5 A LinkedTransferQueue elements : [Welcome]
LinkedTransferQueue method removeAll() with custom object
LinkedTransferQueue method removeAll() with custom object
1 package com.java;
2
3 import java.util.Arrays;
4 import java.util.concurrent.LinkedTransferQueue;
5
6 public class Example {
7
8 public static void main(String[] args) {
9
10 Employee emp1 = new Employee(100, "Emp1", "Tech", 12000);
11 Employee emp2 = new Employee(200, "Emp2", "Tech", 31000);
12 Employee emp3 = new Employee(300,"Emp3", "Admin", 21000);
13
14 LinkedTransferQueue<Employee> queue = new LinkedTransferQueue();
15 queue.addAll(Arrays.asList(emp2, emp1));
16 System.out.println("A LinkedTransferQueue elements : " + queue);
17
18
19 boolean res = queue.removeAll(Arrays.asList(emp2, emp1));
20 System.out.println("Does this queue changed : " + res);
21 System.out.println("A LinkedTransferQueue elements : " + queue);
22
23 boolean res1 = queue.removeAll(Arrays.asList(emp3));
24 System.out.println("Does this queue changed : " + res1);
25 System.out.println("A LinkedTransferQueue elements : " + queue);
26 }
27 }
In the above example, a LinkedTransferQueue of type Employee created and added elements using addAll() method by passing a collection. A removeAll() method called by passing a collection that removes all of the employees from this queue and return boolean value true, as this queue changed. A removeAll() method called again that returns boolean value false, as this queue not changed. It compares employees using Employee class equals() method that compares employee IDs.
1 A LinkedTransferQueue elements : [Emp2 Tech 31000, Emp1 Tech 12000]
2 Does this queue changed : true
3 A LinkedTransferQueue elements : []
4 Does this queue changed : false
5 A LinkedTransferQueue elements : []
Employee class
1 package com.java;
2
3 public class Employee {
4
5 private final int id;
6 private final String name;
7 private final String dept;
8 private final Long salary;
9
10 public Employee(int id, String name, String dept, long salary) {
11 this.id = id;
12 this.name = name;
13 this.dept = dept;
14 this.salary = salary;
15 }
16 public int getId() {
17 return this.id;
18 }
19
20 public String getDept() {
21 return this.dept;
22 }
23
24 public String getName() {
25 return this.name;
26 }
27
28 public Long getSalary() {
29 return this.salary;
30 }
31
32 @Override
33 public int hashCode() {
34 return this.getId() + this.getName().hashCode();
35 }
36
37 @Override
38 public boolean equals(Object obj) {
39 return this.id == ((Employee)obj).id;
40 }
41
42 public String toString() {
43 return this.name + " " + this.dept + " " + this.salary;
44 }
45 }
Related options for your search