import java.io.PrintWriter; import java.util.HashSet; import java.util.Scanner; import java.util.Iterator; public class Main { static PrintWriter cout = new PrintWriter(System.out, true); static Scanner cin = new Scanner(System.in); public static void main (String[] args){ HashSet<String> set = new HashSet<String>(); set.add("java"); set.add("c++"); set.add("web"); set.add("math"); set.add("java");//set中已有"java"插入失败 //求set容器中的元素个数 cout.println(set.size()); //遍历set容器中的元素,方法一: for(String i : set) { System.out.println(i); } //方法二: for(Iterator<String> it=set.iterator();it.hasNext();) { String val=it.next(); System.out.println(val); } //判断set中是否包含某个元素 if(set.contains("java")) { cout.println("find success!"); } else { cout.println("losing finding"); } //将set容器清空 set.clear(); //判断容器是否为空 cout.println(set.isEmpty()); } }