java.util.TreeSet.headSet(E toElement,boolean inclusive) 方法示例


java.util.TreeSet.headSet(E toElement,boolean inclusive) 方法示例

package com.codingdict;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetDemo {
   public static void main(String[] args) {

      // creating TreeSet
      TreeSet <Integer>tree = new TreeSet<Integer>();
      TreeSet <Integer>treeheadsetincl = new TreeSet<Integer>();

      // adding in the tree
      tree.add(12);
      tree.add(13);
      tree.add(14);
      tree.add(15);
      tree.add(16);
      tree.add(17);

      // getting values for 15 inclusive true
      treeheadsetincl = (TreeSet)tree.headSet(15, true);  

      // creating iterator
      Iterator iterator;
      iterator = treeheadsetincl.iterator();

      //Displaying the tree set data
      System.out.println("Tree set data for '15' inclusive TRUE: ");     

      while (iterator.hasNext()) {
         System.out.println(iterator.next() + " ");
      }

      // getting values for 15 inclusive false
      treeheadsetincl = (TreeSet)tree.headSet(15, false);  

      // creating iterator     
      iterator = treeheadsetincl.iterator();

      //Displaying the tree set data
      System.out.println("Tree set data for '15' inclusive FALSE: ");     

      while (iterator.hasNext()) {
         System.out.println(iterator.next() + " ");
      }
   }    
}