Java, Map Common has hashmap, treemap and so on, map is an interface, we can not directly declare a map type of object, in the actual development, the more commonly used map data structure is hashmap and treemap, they are direct sub-class map. The map structure in Java is stored in the Key->value key-value pair, and according to the map's characteristics, there is no two key element in the same map, and value does not have this restriction. In other words, the key is unique in the same map, and value is not unique. If the access efficiency is considered, it is recommended to use the HASHMAP data structure, and if you need to take into account the order of key, we recommend the use of TreeMap, but treemap in the deletion, add the process need to sort, poor performance.
TreeMap, after creating a TreeMap object and adding elements to it, the added elements are automatically sorted by the key value.
// Create a TreeMap object New Treemap<integer, string>(); Map.put (1, "a"); Map.put (2, "C"); // iterating through the collection for (Iterator<integer> it =+ "" + Person.getname ());}
We can also declare a HashMap object and assign the HashMap object to TreeMap.
New TREEMAP (map);
Sorting by key value is simple, but because the key value is unique, cannot be duplicated, all, in the actual process may be used to sort value value;
Sort Value values: (Descending)
//declaring HashMap objectsmap<string, integer> map =NewHashmap<string, integer>();//Convert a Map collection to a list collectionlist<entry<string,integer>> list =NewArraylist<entry<string,integer>>(Map.entryset ());//Sort by Collections.sort (List L, Comparator c) methodCollections.sort (list,NewComparator<map.entry<string, integer>>() { Public intCompare (Map.entry<string, integer>O1, Map.entry<string, integer>O2) { return(O2.getvalue ()-o1.getvalue ()); }});
If you are sorting in ascending order, you only need to modify O2.getvalue ()-O1.getvalue () to O1.getvalue ()-O2.getvalue ().
about Map.entryset ():
The map is an interface in Java, and Map.entry is an internal interface to the map. Map provides some common methods, such as KeySet (), EntrySet (), and so on, the KeySet () method return value is a collection of key values in map, and the return value of EntrySet () returns a set collection of type Map.entry. Map.entry is an internal interface to the map declaration, which is generic and defined as entry<k,v>. It represents an entity (a Key-value pair) in a map. The interface has a getkey (), GetValue method.
HashMap the right two methods of traversal.
Method-: (High efficiency, recommended use)
// using Map.entry New = while (Iter.hasnext ()) { = (map.entry) Iter.next (); = Entry.getkey (); =
Method Two: (Low efficiency, not recommended)
// The key value is obtained first, and the corresponding value value is obtained from the map using the key value . New = while (Iter.hasnext ()) { = iter.next (); =
The map class provides a method called EntrySet () that returns a set of objects that are map.entry instantiated. Next, the Map.entry class provides a Getkey () method and a GetValue () method, so the first code can be organized more logically. The unnecessary "get" process is reduced.
Of course, there are other sorting methods, I am learning, I hope to learn more, better knowledge, write more efficient code.
Java Map Sorting issues