Java map exists in the Java.util package, is a collection class, and a common collection class has a list. The Map collection class is used to store element pairs (called "Keys" and "values"), where each key is mapped to a value.
In this paper, we use a class diagram to describe a map-related organizational structure. The class diagram is as follows:
Class Diagram Description:
map is a top-level interface, and it can be seen from the class diagram that it mainly includes several methods (parts). SortedMap and Concurrentmap interface inherit from the map interface, from the name can be seen a sort of related interfaces, one is the interface required for synchronization.
Abstractmap is an abstract class that implements the map interface, all the map implementation classes inherit from the Abstractmap, and maps with special needs can be extended by implementing other interfaces (TreeMap, CONCURRENTHASHMAP)
HashMap, the most commonly used in the map is HASHMAP, the basic implementation of the requirements of map, understanding HASHMAP need to understand hashcode () and other methods, not mentioned here first.
Linkedhashmap inherits from HashMap, mainly the key-value of HashMap in the order of insertion by the use of linedlist to save, so at the time of traversal faster than HashMap, Other times generally slower than hashmap (Maintenance LinkedList required)
TreeMap Internal use of red and black tree implementation, because the implementation of the SortedMap interface, so TREEMAP internal is ordered, Java sorting related can refer to Java Comparator and strategy mode, With Treemap.submap, some trees can be obtained. TreeMap generally slower than hashmap
concurrenthashmap is a thread-safe map, but does not inherit Hashmap,concurrentskiplistmap is also thread-safe, corresponding to TreeMap, here is not a discussion.
HashTable is a thread-safe map, and because of the use of the synchronized mechanism, it is not highly efficient and is now deprecated.
Map traversal
Set<k> KeySet ();
Set<map.entry<k,v>> EntrySet ();
Using the above two methods can get the key set and entry collection of map, and then use iterator can be traversed, which is the same as the list or set traversal, there is no more to say.
The traversal code is as follows:
Import Java.util.hashmap;import Java.util.map;import Java.util.map.entry;public class Maptest {public static void main ( String []args) {map<string, object> Map = new hashmap<string, object> (), Map.put ("Orage", map.put); "Map.put", "pear", "for" (String Key:map.keySet ()) {System.out.println ("Key was:" +key + "value is" + map.get (key)) ;} For (entry<string, Object>entry:map.entryset ()) {System.out.println ("Key was:" +entry.getkey () + "value is" + Entry . GetValue ());}}
A map study of Java collection classes