Using headset() and tailSet():

These two methods are used to find a subset of a treeset whether greater than a specified object less than a specific object.

Example:
import java.util.*;
class demo
{
public static void main(String[] args){
TreeSet ts=new TreeSet();
ts.add(35);
ts.add(54);
ts.add(23);
ts.add(7);
ts.add(33);
ts.add(15);
ts.add(3);
ts.add(30);
TreeSet head=new TreeSet();
head=(TreeSet)ts.headSet(30);
System.out.println("elements less than 30:"+head);
TreeSet tail=new TreeSet();
tail=(TreeSet)ts.tailSet(30);
System.out.println("elements greater than 30:"+tail);
}};
Output
:
elements less than 30:[3, 7, 15, 23]
elements greater than 30:[30, 33, 35, 54]

1 comment:

  1. Reading your article is such a privilege. It does inspire me, I hope that you can share more positive thoughts. Visit my site too. The link is posted below.

    n8fan.net

    www.n8fan.net

    ReplyDelete