1. Overview
1.概述
The difference between Map and HashMap is that the first one is an interface, and the second is an implementation. However, in this article, we’ll dig a bit deeper and explain why interfaces are useful. Also, we’ll learn how to make code more flexible with interfaces and why we have different implementations for the same interface.
Map和HashMap之间的区别在于,前者是一个接口,而后者是一个实现。然而,在这篇文章中,我们将深入挖掘并解释为什么接口是有用的。此外,我们还将学习如何用接口使代码更加灵活,以及为什么同一个接口有不同的实现。
2. Purpose of Interfaces
2.接口的目的
An interface is a contract that defines only behavior. Each class that implements a particular interface should fulfill this contract. To understand it better, we can take an example from real life. Imagine a car. Every person will have a different image in their mind. The term car implies some qualities and behavior. Any object that has these qualities can be called a car. That is why every one of us imagined a different car.
一个接口是一个只定义了行为的契约。每个实现特定接口的类都应该履行这个契约。为了更好地理解它,我们可以举一个现实生活中的例子。想象一下,一辆汽车。每个人在他们的脑海中都会有一个不同的形象。汽车这个词意味着一些品质和行为。任何具有这些品质的物体都可以被称为汽车。这就是为什么我们每个人都想象出了不同的汽车。
Interfaces work the same. Map is an abstraction that defines certain qualities and behaviors. Only the class that has all of these qualities can be a Map.
接口的工作原理是一样的。Map是一个定义了某些品质和行为的抽象概念。只有具备所有这些品质的类才能成为Map.。
3. Different Implementations
3.不同的实施方式
We have different implementations of the Map interface for the same reason we have different car models. All the implementations serve different purposes. It’s impossible to find the best implementation overall. There is only the best implementation for some purpose. Although a sports car is fast and looks cool, it is not the best choice for a family picnic or trip to a furniture store.
我们对Map接口有不同的实现,这与我们有不同的汽车模型的原因相同。所有的实现都服务于不同的目的。总体而言,不可能找到最佳的实现。尽管跑车速度快、外观酷,但它并不是家庭野餐或去家具店的最佳选择。
HashMap is the simplest implementation of the Map interface and provides the basic functionality. Mostly, this implementation covers all the needs. Two other widely used implementations are TreeMap, and LinkedHashMap provides additional features.
HashMap是Map接口的最简单实现,提供了基本的功能。大多数情况下,这个实现涵盖了所有的需求。另外两个广泛使用的实现是TreeMap,以及LinkedHashMap提供了额外的功能。
Here is a more detailed but not complete hierarchy:
这里有一个更详细但不完整的层次结构。
4. Programming to Implementations
4.从编程到实施
Imagine that we would like to print the keys and values of a HashMap in the console:
想象一下,我们想在控制台中打印一个HashMap的键和值。
public class HashMapPrinter {
public void printMap(final HashMap<?, ?> map) {
for (final Entry<?, ?> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
This is a small class that does the job. However, it contains one problem. It will be able to work only with the HashMap. Therefore any attempt to pass into the method TreeMap or even HashMap, referenced by Map will result in a compile error:
这是一个能完成工作的小班级。然而,它包含一个问题。它只能与HashMap一起工作。因此,任何试图传入Map引用的TreeMap或甚至HashMap的方法,都会导致编译错误。
public class Main {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
HashMap<String, String> hashMap = new HashMap<>();
TreeMap<String, String> treeMap = new TreeMap<>();
HashMapPrinter hashMapPrinter = new HashMapPrinter();
hashMapPrinter.printMap(hashMap);
// hashMapPrinter.printMap(treeMap); Compile time error
// hashMapPrinter.printMap(map); Compile time error
}
}
Let’s try to understand why it’s happening. In both of these cases, the compiler cannot be sure that inside this method, there won’t be any invocations on HashMap specific methods.
让我们试着去理解为什么会发生这种情况。在这两种情况下,编译器无法确定在这个方法里面,不会有任何对HashMap具体方法的调用。
TreeMap is on a different branch of the Map implementation (no pun intended), thus it might lack some methods that are defined in the HashMap.
TreeMap是在Map实现的不同分支上(没有双关的意思),因此它可能缺少一些在HashMap中定义的方法。
In the second case, despite the real underlying object of a type HashMap, it is referenced by the Map interface. Therefore, this object will be able to expose only methods defined in the Map and not in the HashMap.
在第二种情况下,尽管真正的底层对象是HashMap类型,它是由Map接口引用的。因此,这个对象将只能暴露在Map中定义的方法,而不是在HashMap中定义的。
Thus, even though our HashMapPrinter is quite a simple class, it’s too specific. With this approach, it would require us to create a specific Printer for each Map implementation.
因此,尽管我们的HashMapPrinter是一个相当简单的类,但它过于特殊。通过这种方法,我们需要为每个Map实现创建一个特定的Printer。
5. Programming to Interfaces
5.对接口进行编程
Often beginners get confused about the meaning of the expression “program to interfaces” or “code against interfaces”. Let’s consider the following example, which will make it a bit clearer. We’ll change the type of the argument to the most general type possible, which is the Map:
初学者往往对 “对接口编程 “或 “对接口编码 “的含义感到困惑。让我们考虑一下下面的例子,这将使它变得更清楚一些。我们将把参数的类型改为最一般的类型,也就是Map:。
public class MapPrinter {
public void printMap(final Map<?, ?> map) {
for (final Entry<?, ?> entry : map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
As we can see, the actual implementation stayed the same, while the only change is the type of argument. This shows that the method didn’t use any specific methods of HashMap. All the needed functionality was already defined in the Map interface, namely, method entrySet().
我们可以看到,实际的实现保持不变,而唯一的变化是参数的类型。这表明该方法没有使用HashMap的任何特定方法。所有需要的功能都已经在Map接口中定义了,即方法entrySet()。
As a result, this minor change created a huge difference. Now, this class can work with any Map implementation:
结果,这个微小的变化创造了一个巨大的差异。现在,这个类可以与任何Map实现一起工作。
public class Main {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
HashMap<String, String> hashMap = new HashMap<>();
TreeMap<String, String> treeMap = new TreeMap<>();
MapPrinter mapPrinter = new MapPrinter();
mapPrinter.printMap(hashMap);
mapPrinter.printMap(treeMap);
mapPrinter.printMap(map);
}
}
Coding to interface helped us to create a versatile class that can work with any implementation of the Map interface. This approach can eliminate code duplication and ensure our classes and methods have a well-defined purpose.
按照接口编码帮助我们创建了一个多功能的类,可以与Map 接口的任何实现一起工作。这种方法可以消除代码的重复,并确保我们的类和方法有一个明确的目的。
6. Where to Use Interfaces
6.在哪里使用接口
Overall, arguments should be of the most general type possible. We saw in a previous example how just a simple change in a signature of a method could improve our code. Another place where we should have the same approach is a constructor:
总的来说,参数应该是尽可能的通用类型。我们在前面的例子中看到,只要简单地改变一个方法的签名就可以改善我们的代码。另一个我们应该有同样方法的地方是构造函数。
public class MapReporter {
private final Map<?, ?> map;
public MapReporter(final Map<?, ?> map) {
this.map = map;
}
public void printMap() {
for (final Entry<?, ?> entry : this.map.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}
}
This class can work with any implementation of the Map, just because we used the right type in the constructor.
这个类可以与任何Map的实现一起工作,只是因为我们在构造函数中使用了正确的类型。
7. Conclusion
7.结语
To summarize, in this tutorial we discussed why interfaces are a great means for abstraction and defining a contract. Using the most general type possible will make code easy to reuse and easy to read. At the same time, this approach reduces the amount of code which is always a good way to simplify the codebase.
总结一下,在本教程中我们讨论了为什么接口是抽象和定义契约的重要手段。尽可能使用最通用的类型会使代码易于重用,易于阅读。同时,这种方法减少了代码量,这总是简化代码库的好方法。
As always, the code is available over on GitHub.
像往常一样,代码可在GitHub上获得。