Java 中的 hashmap 和 map 之间的区别
本教程介绍了 Java 中 Map
和 HashMap
之间的主要区别。
在 Java 中,Map
是用于以键值对存储数据的接口,而 HashMap
是 Map
接口的实现类。Java 有几个类(TreeHashMap
,LinkedHashMap
)实现了 Map
接口,以将数据存储到键值对中。让我们看一些例子。
Java 中的 Map
接口
不能单独使用 Map
接口来保存数据,但是我们可以创建其实现类的对象,然后使用 Map
引用来保存对象。在这里,我们使用 HashMap
类来存储数据,并使用 Map
接口来保存该类的引用。请参见下面的示例。
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println(map);
}
}
输出:
{One=1, Two=2, Three=3}
Java 中的 HashMap
类
HashMap
是 Map
接口的一个实现类。因此,我们可以使用它来创建键值对的集合。请参见下面的示例。
import java.util.HashMap;
public class SimpleTesting {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println(map);
}
}
输出:
{One=1, Two=2, Three=3}
在 Java 中使用 Map
引用来保存对象
由于 Map
是一个接口,我们可以使用它来保存其实现类的引用,例如 HashMap
,TreeMap
等。我们可以将 TreeMap
或 HashMap
对象保存在 Map
接口中。请参见下面的示例。
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class SimpleTesting {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
System.out.println(map);
Map<String, Integer> tmap = new TreeMap<>(map);
System.out.println(tmap);
}
}
输出:
{One=1, Two=2, Three=3}
{One=1, Three=3, Two=2}
在 Java 中使用 Map
引用保存对象
这是在与实现类一起使用时使用 Map
参考的重要示例。瞧,我们有一个方法以 Map
对象作为参数。因此,在调用时,我们可以传递任何类的对象,例如 HashMap
或 HashTable
。请参见下面的示例。
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class SimpleTesting {
static void printMap(Map<String, Integer> map) {
for (String key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
}
public static void main(String[] args) {
HashMap<String, Integer> hashmap = new HashMap<>();
hashmap.put("One", 1);
hashmap.put("Two", 2);
hashmap.put("Three", 3);
printMap(hashmap);
TreeMap<String, Integer> tmap = new TreeMap<>(hashmap);
printMap(tmap);
LinkedHashMap<String, Integer> lmap = new LinkedHashMap<>(hashmap);
printMap(lmap);
}
}
运行如下代码。
Java SimpleTesting 10 20
输出:
10
20
相关文章
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/24 浏览次数:1782 分类:Python
-
在这篇文章中,我们将使用不同的函数来使用 Pandas DataFrame 列来展平层次索引。我们将使用的方法是重置索引和 as_index() 函数。
计算 Pandas DataFrame 中的方差
发布时间:2024/04/23 浏览次数:212 分类:Python
-
本教程演示了如何计算 Python Pandas DataFrame 中的方差。
Pandas 中的 Groupby 索引列
发布时间:2024/04/23 浏览次数:89 分类:Python
-
本教程将介绍如何使用 Python Pandas Groupby 对数据进行分类,然后将函数应用于类别。通过示例使用 groupby() 函数按 Pandas 中的多个索引列进行分组。