Map遍历方式
entrySet
推荐
最常用,性能很好
示例:
for (Map.Entry<Integer, Integer> entry : map.entrySet())
{
System.out.println(entry.getKey() + ":" + entry.getValue());
}
Iterator
使用foreach遍历map时,如果改变其大小,会报错
使用Iterator的remove方法可以删除元素
性能很好
Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
while (it.hasNext())
{
Map.Entry<Integer, Integer> entry = it.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
// it.remove(); 删除元素
}
Lambda
map.forEach((key, value) -> {
System.out.println(key + ":" + value);
});
keySet get(key)
for (Integer key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
keySet values
for (Integer key : map.keySet()) {
System.out.println(key);
}
for (Integer value : map.values()) {
System.out.println(value);
}
Map遍历方式的更多相关文章
- java map遍历方式及效率
本文转载自Java Map遍历方式的选择. 只给出遍历方式及结论.测试数据可以去原文看. 如果你使用HashMap 同时遍历key和value时,keySet与entrySet方法的性能差异取决于ke ...
- Java Map遍历方式的选择
[原文] 1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keyS ...
- Java中Map常用方法总结以及遍历方式的汇总
一.整理: 看到array,就要想到角标. 看到link,就要想到first,last. 看到hash,就要想到hashCode,equals. 看到tree,就要想到两个接口.Comparable, ...
- Map三种遍历方式
Map三种遍历方式 package decorator; import java.util.Collection; import java.util.HashMap; import java.util ...
- map的四种遍历方式
map是Java中非常常用的一种数据结构,但map不同于set和list都继承自Collection接口. 所以map没有实现Collection的Iterator 方法,自身没有迭代器来遍历元素. ...
- Map集合的应用及其遍历方式
---> HashMap :底层基于哈希表 存储原理也使用哈希表来存放的: 往HashMap添加了元素 ,首先会调用键的hashCode方法 获得一个哈希值,然后 ...
- Collection、Map、数组 遍历方式
结论:无论是数组还是Collection for each 都是一个非常好的选择 一.for each底层实现 对于Collection,for each是隐式调用Iterator实现的,效率比显示调 ...
- Map集合的遍历方式:
迭代器来遍历 : entrySet() ; keySet(); values(); eg.HashMap<String,String> map = new HashMap<Strin ...
- Java Map各遍历方式的性能比较
1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keySet拿到的 ...
随机推荐
- 13、前端知识点--ajax原理以及实现过程
一.简略版的 Ajax简介 Ajax(Asyncchronous JavaScript and Xml),翻译过来就是说:异步的javaScript和xml, Ajax不是新的编程语言,而是一种使用现 ...
- Mysql日期和字符的相互转换
今天从网上查到了一些关于MySQL数据库的日期转换函数的转换的用法,在这里记录一下: mysql日期和字符相互转换 date_format(date,'%Y-%m-%d') ------------- ...
- React与Typescript整合
0. Typescript Typescript对于前端来说可以说是越来越重要了,前端的很多项目都用Typescript进行了重构.这主要得益于Typescript有比较好的类型支持,在编码的过程中可 ...
- python 中PIL.Image和OpenCV图像格式相互转换
PIL.Image转换成OpenCV格式: import cv2 from PIL import Image import numpy image = Image.open("plane ...
- Debug to add expression
Debug expression
- Mongodb副本集实现及读写分离
前言 地址:https://blog.csdn.net/majinggogogo/article/details/51586409 作者介绍了,mongodb副本集的读写原理,原理是通过代码层来实现. ...
- VUE 生成二维码插件
原文:https://www.jianshu.com/p/496fd1cbee8d npm install qrcodejs2 --save 页面中引入 dom 结构 JS 方法编写 export d ...
- matplotlib--直线和点
直线和点: import matplotlib.pyplot as plt import numpy as np x=np.linspace(-10,10,10) y=x**2 h=plt.plot( ...
- service pom
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- OC + RAC (八) 查看信号状态和跳过信号
-(void)_test9{ /// RACCommand又叫命令 是用来收发数据的 监听按钮点击,网络请求.... RACCommand * command = [[RACCommand alloc ...