首页 > 代码库 > HashSet类

HashSet类

 

 1 import java.util.HashSet;
 2 import java.util.Hashtable;
 3 import java.util.Iterator;
 4 import java.util.Scanner;
 5 import java.util.Set;
 6 
 7 public class demoHashSet {
 8     public static void main(String[] args) {
 9         HashSet<String> stu = new HashSet<String>();
10         Scanner in = new Scanner(System.in);
11         
12         System.out.println("请依次输入学生的姓名, 以空行结束");
13         while(true){
14             String name = in.nextLine();
15             if(name.length() > 0){
16                 stu.add(name);
17             }
18             else{
19                 break;
20             }
21         }
22         
23         System.out.println("所有学生的姓名");
24         Iterator<String> it = stu.iterator();
25         while(true){ 
26             if(it.hasNext()){
27                 System.out.println(it.next().toString());
28             }
29             else {
30                 break;
31             }
32         }
33         
34         System.out.println("请输入要删除的学生姓名, 以空行结束");
35         while(true){
36             String name = in.nextLine();
37             if(name.length() > 0){
38                 stu.remove(name);
39             }
40             else {
41                 break;
42             }
43         }
44         
45         System.out.println("剩余的学生");
46         it = stu.iterator();
47         while(true){ 
48             if(it.hasNext()){
49                 System.out.println(it.next().toString());
50             }
51             else {
52                 break;
53             }
54         }
55         
56         in.close(); //关闭输入流
57     }
58 }