文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 一、HashMap是什么?
- 二、使用步骤
- 1.引入库
- 2.读入数据
前言
提示:这里可以添加本文要记录的大概内容:
Hash Map的遍历以及如何在遍历中删除元素
提示:以下是本篇文章正文内容,下面案例可供参考
一、HashMap是什么?示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
二、使用步骤 1.引入库代码如下(示例):
① forEach遍历方式
② 迭代器 Iterator方式
③ Java8的 lambda表达式
4、加ava8的 stream 单线程和多线程遍历
如果是只需要进行遍历的时候,推荐使用java8之后的
lambda表达式 stream多线程
如果需要遍历过程中删除 推荐使用entrySet迭代器
1、 通过ForEach循环进行遍历
mport java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException {
Map
map.put(1, 10);
map.put(2, 20);
// Iterating entries using a For Each loop for (Map.Entryentry : map.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } }
}
2、 ForEach迭代键值对方式
如果你只想使用键或者值,推荐使用如下方式
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException {
Map
map.put(1, 10);
map.put(2, 20);
// 迭代键
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
// 迭代值
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
}
}
3、使用带泛型的迭代器进行遍历
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException {
Map
map.put(1, 10);
map.put(2, 20);
Iterator> entries = map.entrySet().iterator(); while (entries.hasNext()) { Map.Entry entry = entries.next(); // 遍历中删除元素 ************** if(1.equals(entry.getKey())){ entries.remove(); continue; } System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } }
}
4、使用不带泛型的迭代器进行遍历
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException {
Map map = new HashMap();
map.put(1, 10);
map.put(2, 20);
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer) entry.getKey();
Integer value = (Integer) entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
}
}
5、通过Java8 Lambda表达式遍历
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) throws IOException {
Map map = new HashMap(){{
map.put(1, 10);
map.put(2, 20);
}};
map.forEach((k, v) -> System.out.println("key: " + k + " value:" + v));
}
}
6、stream遍历 *****
下面是多线程遍历,单线程遍历没有.parallel()
public static void main(String[] args) throws IOException {
Map map = new HashMap();
map.put(1, 10);
map.put(2, 20);
map.entrySet().stram().parallel().foreach((entry) -> {
System.out.println(entry.getKey()+entry.getValue());
}
```java
输出
---
# 总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。



