xinmigo 发表于 2013-1-26 16:02:42

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
页: [1]
查看完整版本: List Set Map循环 插入重复对象注意点