关于set,list,map

来源:互联网 发布:黑河龙江网络 编辑:程序博客网 时间:2024/04/20 03:46

List Set,区别在于List是有序的Collection,且其中允许重复的元素,比如我们常用的Vector,ArrayList,LinkedList,都是实现了List接口的类;而Set是一种不包含重复的元素的Collection。


Map没有继承Collection接口,Map提供key到value的映射。一个Map中不能包含相同的key,每个key只能映射一个 value。HashMap是实现了Map接口的具体类。

map的一些常用方法:

 import java.util.*;
     
         .............
        Map  person_message = new HashMap();
        person_message.put("sid","123");
        person_message.put("sname","lt");
        person_message.put("sphone","345");  
      
        //提取get(key)
           System.out.println(person_message.get("sphone"));        //  输出"345"
       //构建map集合中所有key对象的集合
           Set set = person_message.keySet();
           Iterator it = set.iterator();
           while(it.hasNext())
              System.out.print(it.next()+" ");                                         //  输出"sid  sname  sphone"
       //构建map集合中所有values值的集合
           Collection coll = person_message.values();
            Iterator it = coll.iterator();
            while(it.hasNext())
                System.out.print(it.next()+" ");                                    //  输出"123 lt  345"
未完待续。。。。。。

原创粉丝点击