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


java.util.TreeSet.subSet(E fromElement,boolean fromInclusive,E toElement,boolean toInclusive) 方法示例

package com.codingdict;

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

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

      // creating a TreeSet
      TreeSet <Integer>treeadd = new TreeSet<Integer>();
      TreeSet <Integer>treesubset = new TreeSet<Integer>();

      // adding in the tree set
      treeadd.add(1);
      treeadd.add(2);
      treeadd.add(3);
      treeadd.add(4);
      treeadd.add(5);
      treeadd.add(6);
      treeadd.add(7);
      treeadd.add(8);

      // creating subset
      treesubset = (TreeSet)treeadd.subSet(3, true, 7, true);

      // create iterator
      Iterator iterator;
      iterator = treesubset.iterator();

      // displaying the Tree set data
      System.out.println("Tree subset data: ");     

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