1import java.util.Iterator;
2import java.util.TreeSet;
3public class TreeSetHeadSetEToElementMethodExample
4{
5 public static void main(String[] args)
6 {
7 TreeSet<Integer> ts = new TreeSet<Integer>();
8 TreeSet<Integer> hs = new TreeSet<Integer>();
9 ts.add(1);
10 ts.add(2);
11 ts.add(3);
12 ts.add(4);
13 ts.add(5);
14 ts.add(6);
15 // get values less than 5
16 hs = (TreeSet)ts.headSet(5);
17 // create iterator
18 Iterator<Integer> iterate = hs.iterator();
19 System.out.println("TreeSet data: ");
20 while(iterate.hasNext())
21 {
22 System.out.println(iterate.next() + " ");
23 }
24 }
25}
26
27
28
1import java.util.Iterator;
2import java.util.TreeSet;
3public class TreeSetHeadSetMethodExample
4{
5 public static void main(String[] args)
6 {
7 TreeSet<Integer> ts = new TreeSet<Integer>();
8 TreeSet<Integer> hs = new TreeSet<Integer>();
9 ts.add(1);
10 ts.add(2);
11 ts.add(3);
12 ts.add(4);
13 ts.add(5);
14 ts.add(6);
15 // get values for 5 inclusive true
16 hs = (TreeSet)ts.headSet(5, true);
17 // create iterator
18 Iterator<Integer> iterate = hs.iterator();
19 System.out.println("TreeSet data for '5' inclusive TRUE: ");
20 while(iterate.hasNext())
21 {
22 System.out.println(iterate.next() + " ");
23 }
24 // get values for 5 inclusive false
25 hs = (TreeSet)ts.headSet(5, false);
26 iterate = hs.iterator();
27 System.out.println("TreeSet data for '5' inclusive FALSE: ");
28 while(iterate.hasNext())
29 {
30 System.out.println(iterate.next() + " ");
31 }
32 }
33}
34
35
36