- /*
- * Set:无序无重复
- */
- Set set=new HashSet();//用HashSet实现,用TreeSet都可以,但hashset用的多
- //两者区别:hashset添到集合中用hash算法计算,treeset同理,
- //添加元素(无序无重复,去掉重复的元素)
- set.add(1);
- set.add(2.0);
- set.add(1);
- set.add("kitty");
- set.add(new Boolean(true));
- System.out.println(set);
- //修改元素
- //查询元素
- //删除元素remove
- set.remove(true);
- System.out.println(set);
- //由于集合无序,所以只能添加和删除元素,其他操作无法进行
- //遍历集合
- Iterator it=set.iterator();//iterator 游标,在开始时指向首元素前面,
- //hasnext指向下以元素
- while(it.hasNext())
- {
- Object o=it.next();
- System.out.println(o);
- }
- System.out.println(set);
- for(Object o:set)//只能用增强的for循环遍历,不能用普通的遍历,但必须保证jdk在4.0以上
- {
- System.out.println(set);
- }
转载于:https://blog.51cto.com/bannana/1045324