The code demonstrates how to use the pollFirst() method to remove and retrieve the lowest (smallest) item from a TreeSet in Java.
In the GetRemove_Lowest class, a TreeSet object named num is created and integers such as 70, 50, 100, 80, 30, 10, 60, 20, 90, and 40 are added to it using the add() method.
The pollFirst() method is then used to remove and retrieve the lowest item from the num set. The returned item is stored in the item variable. The removed item is printed using the System.out.println() statement with the message "Removed Item : " + item.
Finally, the updated num set, after removing the lowest item, is printed using the System.out.println() statement with the message "TreeSet Element : " + num.
import java.io.*; import java.util.*; public class GetRemove_Lowest { public static void main(String args[]) { TreeSet <Integer> num = new TreeSet <Integer>(); num.add(70); num.add(50); num.add(100); num.add(80); num.add(30); num.add(10); num.add(60); num.add(20); num.add(90); num.add(40); System.out.println("TreeSet Element : " + num); int item = num.pollFirst(); System.out.println("Removed Item : " + item); System.out.println("TreeSet Element : " + num); } }
TreeSet Element : [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] Removed Item : 10 TreeSet Element : [20, 30, 40, 50, 60, 70, 80, 90, 100]
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions