AbstractMap abstract class implements some simple and general methods, which is not difficult in itself. However, there are two methods in this abstract class that deserve attention. The source code implementation of keySet and values methods can be said to be a textbook model.
Abstract classes are usually implemented as skeletons, and public methods are implemented for their subclasses. In the last article, we explained the Map interface, and this paper analyzes and studies the AbstractMap class.
There are quite a few data structures of map types in Java. As their skeleton, AbstractMap implements some methods of Map interface, that is, it provides a general method for its various map subclasses, and the methods that are not implemented may be different.
An abstract class cannot directly create an instance of the abstract class through the new keyword, but it can have a constructor. AbstractMap provides a protected and decorated parameterless constructor, which means that only its subclasses can access it (of course, it is an abstract class itself, and other classes cannot instantiate it directly), which means that only its subclasses can call this parameterless constructor.
In the Map interface, an entry interface is defined internally. This interface is the internal implementation of a key-value pair maintained by Map mapping, and the key value is stored in this Map.Entry. AbstractMap to implement this internal interface, which has two * * *: one is a changeable SimpleEntry, and the other is an immutable SimpleImmutableEntry.
publicstaticclasssimpleentryimplemententry,java.io.Serializable
Map. The entrance interface and Serializable are realized.
Its method is relatively simple, and it is the operation of taking and storing values. The definition of a key value is a final modification, which means that it is an immutable reference. In addition, its setValue method is a bit special, and the stored value returns not the stored value, but the previous old value. What you need to learn is the equals and hashCode methods it overrides.
publicstaticclassSimpleImmutableEntryimplementsEntry,Java . io . serializablesimpleimmutableentry
An entry defined as immutable is actually immutable, because it does not provide a setValue method, so it cannot be modified by the setValue method when multiple threads access it at the same time. Compared with SimpleEntry, its key and value member variables are defined as final types. Calling the setValue method will throw an UnsupportedOperationException exception.
Its equals and hashCode methods are consistent with SimpleEntry.
Next, let's take a look at which methods in the Map interface are implemented by the AbstractMap abstract class.
publicintsize()
An entrySet method is defined in the Map, which returns the Set collection of the Map. Entry, and directly calling the size method of set set is the size of the map.
publicbooleanisEmpty()
Call the size method above, and it will be empty if it is equal to 0.
public boolean contains key(object key)
The implementation of this method is relatively simple. The iterator of the Set is obtained by calling the entrySet method to traverse the Map. Entry and compare it with the parameter key. Maps can be stored as null key values. Because key=null is specially stored in the Map (the hashCode value cannot be calculated), it also determines whether the parameter key is empty.