如何在 Java 中打印 HashMap
本教程介绍了如何在 Java 中打印 HashMap
元素,还列举了一些示例代码来理解这个主题。
HashMap
是 Map
接口的一个实现类,用于将元素收集成键和值对。我们可以使用各种方法来打印它的元素。比如 keySet()
方法、values()
方法、entrySet()
方法、asList()
方法等。我们来看一些例子。
Java 中打印 HashMap
元素
这是 Java 中打印 HashMap
最简单的方法。只要将 HashMap
的引用传递到 println()
方法中,它就会将键值对打印到大括号中。请看下面的例子。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
System.out.println(map);
}
}
输出:
{100=Hundred, 1000=Thousand, 10=Ten}
Java 中使用 keySet()
方法打印 HashMap
元素
我们可以使用 keySet()
方法获得一组键,然后在 for
循环中使用 get()
方法获得值。get()
方法返回与传递的键相关的值。请看下面的例子。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
for (Integer key : map.keySet()) {
System.out.println(key + " = " + map.get(key));
}
}
}
输出:
100 = Hundred
1000 = Thousand
10 = Ten
Java 中使用 forEach()
方法打印 HashMap
元素
从 Java 8 开始,我们可以使用 forEach()
方法,借助 getKey()
和 getValue()
方法打印 HashMap
元素。getKey()
方法从 entrySet
中返回一个键,getValue()
方法返回与该键相关的值。请看下面的例子。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
map.entrySet().forEach(
entry -> { System.out.println(entry.getKey() + " = " + entry.getValue()); });
}
}
Java 中使用 Arrays.asList()
打印 HashMap
元素
我们可以使用 Arrays.asList()
方法来打印 HashMap
元素。asList()
方法返回 HashMap
的列表形式。请看下面的例子。
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
System.out.println(Arrays.asList(map));
}
}
输出:
[{100=Hundred, 1000=Thousand, 10=Ten}]
在 Java 中使用 Collections
类打印 HashMap
元素
我们可以使用 Collections.singletonList()
静态方法来打印 HashMap
元素。singletonList()
方法返回 HashMap
的列表表示。请看下面的例子。
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
System.out.println(Collections.singletonList(map));
}
}
输出:
[{100=Hundred, 1000=Thousand, 10=Ten}]
Java 中使用 entrySet()
方法打印 HashMap
元素
entrySet()
方法返回一组条目,可用于 for 循环打印 HashMap
元素。请看下面的例子。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
输出:
100 = Hundred
1000 = Thousand
10 = Ten
Java 中使用 values()
和 keySet()
方法打印 HashMap
元素
如果我们想单独打印值和键,我们可以使用 values()
和 keySet()
方法。values()
方法返回所有值的列表,而 keySet()
方法返回所有 HashMap
键的列表。请看下面的例子。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
System.out.println(map.values());
System.out.println(map.keySet());
}
}
输出:
[Hundred, Thousand, Ten]
[100, 1000, 10]
Java 中使用 Biconsumer
打印 HashMap
元素
Biconsumer
是 Java 中的一个接口,可用于使用 lambda
表达式打印 HashMap
元素。请看下面的例子。
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
BiConsumer<Integer, String> biconsumer = (key, val) -> System.out.println(key + " = " + val);
map.forEach(biconsumer);
}
}
输出:
100 = Hundred
1000 = Thousand
10 = Ten
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何从 Pandas 的日期时间列中提取月份和年份
发布时间:2024/04/23 浏览次数:160 分类:Python
-
我们可以分别使用 dt.year()和 dt.month()方法从 Datetime 列中提取出年和蛾。我们还可以使用 pandas.DatetimeIndex.month 以及 pandas.DatetimeIndex.year 和 strftime()方法提取年份和月份。
如何检查 NaN 是否存在于 Pandas DataFrame 中
发布时间:2024/04/23 浏览次数:208 分类:Python
-
我们可以使用 isnull()和 isna()方法检查 Pandas DataFrame 中是否存在 NaN。
如何在 Pandas DataFrame 的列中将所有 NaN 值替换为零
发布时间:2024/04/23 浏览次数:198 分类:Python
-
在 Pandas 库中使用 df.fillna(),df.replace()方法在 DataFrame 中将 NaN 值替换为零
如何在 Pandas 中更改列的数据类型
发布时间:2024/04/23 浏览次数:183 分类:Python
-
本教程介绍了如何通过使用 to_numaric,as_type 和 infer 对象来更改 Pandas 中列的数据类型。
如何对 Pandas 中的 DataFrame 行随机排序
发布时间:2024/04/23 浏览次数:128 分类:Python
-
我们可以使用 sample(),shuffle()和 permutation()方法随机地对 Pandas 中的 DataFrame 行进行随机排序。
如何获取 Pandas DataFrame 的行数
发布时间:2024/04/23 浏览次数:71 分类:Python
-
本教程介绍如何通过使用 shape,len()来获取 Pandas DataFrame 的行数,以及有多少行元素满足条件。