setsexample.java

来自「程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件」· Java 代码 · 共 36 行

JAVA
36
字号
package examples.collections;

import java.util.*;

/** A class to demonstrate the use of the Set
  * and SortedSet interfaces in the java.util package
  */
public class SetsExample {
   /** Test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      // create a set and intialize it
      Set s1 = new HashSet();
      s1.add( new Integer( 6 ) );
      s1.add( new Integer( 100 ) );
      s1.add( new Integer( -89 ) );
      s1.add( new Integer( 2 ) );
      s1.add( new Integer( 57 ) );
      // iterate to display the set values
      Iterator i1 = s1.iterator();
      while ( i1.hasNext() ) {
         System.out.print( i1.next() + " " );
      }
      System.out.println();
      
      // create a SortedSet from a Set
      SortedSet s2 = new TreeSet( s1 );
      // iterate to display the set values
      Iterator i2 = s2.iterator();
      while ( i2.hasNext() ) {
         System.out.print( i2.next() + " " );
      }
      System.out.println();
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?