入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

java中map集合类用法(hashmap用法)

创建时间:2016-08-26 投稿人: 浏览次数:1421

map键值对,值一般存储的是对象。hashmap中常用的方法,put(object key,object value);

get(object key);//根据key值找出对应的value值。

判断键是否存在:containsKey(object key)

判断值是否存在:containsValue(object value)


1.Map的特性即「键-值」(Key-Value)匹配

java.util.HashMap实作了Map界面,

HashMap在内部实作使用哈希(Hash),很快的时间内可以寻得「键-值」匹配.

2. Map<String, String> map =

                        new HashMap<String, String>();

        String key1 = "caterpillar";

        String key2 = "justin";

        map.put(key1, "caterpillar的讯息");

        map.put(key2, "justin的讯息");

        

        System.out.println(map.get(key1));

        System.out.println(map.get(key2));

3.可以使用values()方法返回一个实作Collection的对象,当中包括所有的「值」对象

.

   Map<String, String> map =

                  new HashMap<String, String>();

 

        map.put("justin", "justin的讯息");

        map.put("momor", "momor的讯息");

        map.put("caterpillar", "caterpillar的讯息");

        

        Collection collection = map.values();

        Iterator iterator = collection.iterator();

        while(iterator.hasNext()) {

            System.out.println(iterator.next());

        }

        System.out.println();

4. Map<String, String> map =

                   new LinkedHashMap<String, String>();

        

        map.put("justin", "justin的讯息");

        map.put("momor", "momor的讯息");

        map.put("caterpillar", "caterpillar的讯息");

        

        for(String value : map.values()) {

            System.out.println(value);

        }


声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像