java.util.TreeSet.addAll(Collection c) 方法示例


java.util.TreeSet.addAll(Collection c) 方法示例

package com.codingdict;

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

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

      // creating a TreeSet
      TreeSet <Integer>treeone = new TreeSet<Integer>();
      TreeSet <Integer>treetwo = new TreeSet<Integer>();

      // adding in the treeone
      treeone.add(12);
      treeone.add(13);
      treeone.add(14);

      // adding in the treetwo
      treetwo.add(15);
      treetwo.add(16);
      treetwo.add(17);  

      // adding treetwo to treeone
      treeone.addAll(treetwo);

      // create an iterator
      Iterator iterator;
      iterator = treeone.iterator();

      // displaying the Tree set data
      System.out.print("Tree set data: ");     
      while (iterator.hasNext()) {
         System.out.print(iterator.next() + " ");
      }
   }    
}