设为首页 加入收藏

TOP

HashMap遍历和使用
2017-08-19 10:24:48 】 浏览:695
Tags:HashMap 使用

map的几种遍历方式:


Map< String, String> map = new HashMap<>();


  map.put("aa", "@sohu.com");


  map.put("bb","@163.com");


  map.put("cc", "@sina.com");


  System.out.println("普通的遍历方法,通过Map.keySet遍历key和value");//普通使用,二次取值


  for (String key : map.keySet()) {


      System.out.println("key= "+key+" and value= "+map.get(key));


  }


  System.out.println("通过Map.entrySet使用iterator遍历key和value:");


  Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();


  while(it.hasNext()){


      Map.Entry<String, String> entry = it.next();


      System.out.println("key= "+entry.getKey()+" and value= "+entry.getValue());


  }


  System.out.println("通过Map.entrySet遍历key和value");    //推荐这种,特别是容量大的时候


  for(Map.Entry<String, String> entry : map.entrySet()){


      System.out.println("key= "+entry.getKey()+" and value= "+entry.getValue());


  }
??System.out.println(“通过Map.values()遍历所有的value,但不能遍历key”);


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


      System.out.println("value = "+v);


  }


HashMap和Hashtable的联系和区别 
实现原理相同,功能相同,底层都是哈希表结构,查询速度快,在很多情况下可以互用,早期的版本一般都是安全的。


hashmap的特点 
HashMap是map接口的子类,是将键映射到值的对象,其中键和值都是对象,不是线程安全的 
hashMap用hash表来存储map的键 
? key是无序唯一,可以有一个为null 
? value无序不唯一,可以有对个null 
linkedHashMap使用hash表存储map中的键,并且使用linked双向链表管理顺序


HashMap可以通过下面的语句进行同步: 
Map m = Collections.synchronizeMap(hashMap);


几大常用集合的效率对比 
这里写图片描述


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇详解equals()方法和hashCode()方法 下一篇InputStreamReader读取文件出现乱..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目