日期:2014-05-20 浏览次数:20745 次
public static void main(String[] args){ HashSet hs = new HashSet(); hs.add("1st"); hs.add("2nd"); hs.add(new Integer(3)); hs.add(new Double(4.8)); hs.add("2nd"); //导入重复元素 hs.add(new Integer(3)); //导入重复元素,不会成功,HashSet中不允许有重复元素 hs.add(new Date()); System.out.println("开始:size="+hs.size()); Iterator it = hs.iterator(); while(it.hasNext()){ Object o = it.next(); System.out.println(o); } System.out.println("---------------------------"); hs.remove("2nd"); System.out.println("移除元素后:size="+hs.size()); //下列循环未执行 while(it.hasNext()){ Object o = it.next(); System.out.println(o); } System.out.println("---------------------------"); System.out.println(hs); }