Java Program to Get the Subset of a TreeSet Collection Based on a Specified Range

You are currently viewing Java Program to Get the Subset of a TreeSet Collection Based on a Specified Range

Java program to get the subset of a TreeSet collection based on a specified range

// Java program to get the subset of a TreeSet collection 
// based on a specified range

import java.io.*;
import java.util.*;

public class Main {
  public static void main(String args[]) {
    TreeSet < Integer > tree = new TreeSet < Integer > ();

    tree.add(10);
    tree.add(20);
    tree.add(25);
    tree.add(30);
    tree.add(35);
    tree.add(40);

    TreeSet < Integer > subSet = (TreeSet < Integer > ) tree.subSet(20, 35);

    System.out.println("TreeSet: " + tree);
    System.out.println("SubSet: " + subSet);
  }
}

Output

TreeSet: [10, 20, 25, 30, 35, 40]
SubSet: [20, 25, 30]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply