List Set Map循环 插入重复对象注意点

public static void main(String[] args) {
java 中Map的使用(不允许有重复的对象):
Map map = new HashMap();
map.put("a", "123");
map.put("b", "456");
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
Object obj = it.next();
System.out.println("key:"+obj);
System.out.println("vlaue:"+map.get(obj));
}
}


import java.util.HashSet;
import java.util.Iterator;
public class WpsklHashSet
{
//java 中Set的使用(不允许有重复的对象):
public static void main(String[] args)
{
HashSet hashSet=new HashSet();
String a=new String("A");
String b=new String("B");
String c=new String("B");
hashSet.add(a);
hashSet.add(b);
System.out.println(hashSet.size());
String cz=hashSet.add(c)?"此对象不存在":"已经存在";
System.out.println("测试是否可以添加对象 "+cz);
System.out.println(hashSet.isEmpty());
//测试其中是否已经包含某个对象
System.out.println(hashSet.contains("A"));
Iterator ir=hashSet.iterator();
while(ir.hasNext())
{
System.out.println(ir.next());
}
//测试某个对象是否可以删除
System.out.println(hashSet.remove("a"));
System.out.println(hashSet.remove("A"));
//经过测试,如果你想再次使用ir变量,必须重新更新以下
ir=hashSet.iterator();
while(ir.hasNext())
{
System.out.println(ir.next());
}

}
}


public static void main(String[] args)
{
List ls=new List();
String a="aaa";
String b="aaa";
ls.add(a);
ls.add(b);
System.out.println(ls.size());


}
http://blog.sina.com.cn/s/blog_3e3779c10100b1ri.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值