TreeSet : subSet(), isEmpty(), pollFirst()

The subSet() methods returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

The isEmpty() methods returns true if this set contains no elements.

The pollFirst() methods retrieves and removes the first (lowest) element, or returns null if this set is empty.

Program

0

 

 

 

Program Source

import java.util.TreeSet;

public class Javaapp {

    public static void main(String[] args) {
        
        TreeSet ts = new TreeSet();
        ts.add(20);
        ts.add(40);
        ts.add(60);
        ts.add(80);
        System.out.println("subSet(15, 65)  : "+ts.subSet(15, 65));
        
        while(ts.isEmpty() == false)
        {
            System.out.println(ts.pollFirst()+" Removed");
        }
        
        System.out.println("ts is Empty  : "+ts);
    }
}

Leave a Comment