java.util.TreeSet.headSet() 方法示例


java.util.TreeSet.headSet() 方法示例

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>treeheadset = 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 less than 15
      treeheadset = (TreeSet)tree.headSet(15);  

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

      //Displaying the tree set data
      System.out.println("Tree set data: ");     

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