TreeSet method pollLast() in Java
A TreeSet method pollLast() used to retrieve and remove the last (highest) element or returns null if this set is empty. It returns the last element, or null if this set is empty.
Syntax
 1 public E pollLast()
return : It returns the last element, or null if this set is empty.
TreeSet method pollLast()
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.TreeSet;
 6 
 7 public class Main {
 8 
 9     public static void main(String[] args) {
 10 
 11         // creating immutable list
 12         List<Integer> elements = Arrays.asList(20, 10, 30, 50, 40);
 13 
 14         // create tree set by specifying list collection
 15         TreeSet<Integer> set = new TreeSet<>(elements);
 16         System.out.println("A TreeSet elements : " + set);
 17 
 18         // retrieve and remove last element
 19         Integer res = set.pollLast();
 20         System.out.println("A retrieved element from this set : " + res);
 21         System.out.println("A changed tree set : " + set);
 22     }
 23 }
In the above example, a TreeSet of type integer created by passing a list that sort elements in natural order. A TreeSet method pollLast() called that removes and returns a last element of this set. A resulting element print.
Output
 1 A TreeSet elements : [10, 20, 30, 40, 50]
 2 A retrieved element from this set : 50
 3 A changed tree set : [10, 20, 30, 40]

TreeSet method pollLast() with String

TreeSet method pollLast() with String
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.List;
 5 import java.util.TreeSet;
 6 
 7 public class StringExp {
 8 
 9     public static void main(String[] args) {
 10 
 11         // creating immutable list
 12         List<String> elements = Arrays.asList("Welcome", "Hello", "to", "IOGyan");
 13         System.out.println("A list elements : " + elements);
 14 
 15         // create tree set with custom comparator
 16         TreeSet<String> treeSet = new TreeSet<>((s1, s2) -> s2.length() - s1.length());
 17         // insert elements to tree set
 18         treeSet.addAll(elements);
 19         System.out.println("An elements in tree set :" + treeSet);
 20 
 21         // retrieve and remove last element
 22         String res = treeSet.pollLast();
 23         System.out.println("A retrieved element from this set : " + res);
 24         System.out.println("A changed tree set : " + treeSet);
 25     }
 26 }
In the above example, a TreeSet of type string created by passing a lambda function that sort elements in descending order based on string length. A TreeSet method pollLast() called that removes and returns a last element of this set. A resulting element print.
Output
 1 A list elements : [Welcome, Hello, to, IOGyan]
 2 An elements in tree set :[Welcome, IOGyan, Hello, to]
 3 A retrieved element from this set : to
 4 A changed tree set : [Welcome, IOGyan, Hello]

TreeSet method pollLast() with custom object

TreeSet method pollLast() with custom object
 1 package com.java;
 2 
 3 import java.util.Arrays;
 4 import java.util.Comparator;
 5 import java.util.List;
 6 import java.util.TreeSet;
 7 
 8 public class Example {
 9 
 10     public static void main(String[] args) {
 11 
 12         Employee emp1 = new Employee(100, "Emp1", "Tech",  12000);
 13         Employee emp2 = new Employee(200, "Emp2", "Tech", 31000);
 14         Employee emp3 = new Employee(300,"Emp3", "Admin", 21000);
 15 
 16         // creating immutable list
 17         List<Employee> elements = Arrays.asList(emp1, emp2, emp3);
 18         // create tree set with custom comparator
 19         Comparator<Employee> comparator = (e1, e2) -> (int) (e2.getSalary() - e1.getSalary());
 20         // creating tree set by specifying a custom comparator
 21         TreeSet<Employee> treeSet = new TreeSet<>(comparator);
 22 
 23         // insert elements to sorted set
 24         treeSet.addAll(elements);
 25         System.out.println("An elements in tree set :" + treeSet);
 26 
 27         // retrieve and remove last element
 28         Employee res = treeSet.pollLast();
 29         System.out.println("A retrieved element from this set : " + res);
 30         System.out.println("A changed tree set : " + treeSet);
 31     }
 32 }
In the above example, a TreeSet of type Employee created by passing a lambda function that sort elements in descending order based on employee salary. A TreeSet method pollLast() called that removes and returns a last element of this set. A resulting element print using employee class toString() method.
Output
 1 An elements in tree set :[Emp2 Tech 31000, Emp3 Admin 21000, Emp1 Tech 12000]
 2 A retrieved element from this set : Emp1 Tech 12000
 3 A changed tree set : [Emp2 Tech 31000, Emp3 Admin 21000]

Employee class

Employee class
 1 package com.java;
 2 
 3 public class Employee implements Comparable<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 
 46     @Override
 47     public int compareTo(Employee o) {
 48         return this.getId() - o.getId();
 49     }
 50 }
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us