In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use different Map in Java, it has certain reference value, interested friends can refer to it, I hope you can learn a lot after reading this article, let the editor take you to understand it.
How to use different Map, such as HashMap,TreeMap,HashTable and LinkedHashMap.
Overview of Map
There are four common Map implementations in Java, HashMap,TreeMap,HashTable and LinkedHashMap, and we can describe each Map in one sentence, as follows:
HashMap: based on hash table implementation, unordered; TreeMap: based on red-black tree implementation, sorted by Key; LinkedHashMap: saved insertion order; Hashtable: synchronous, similar to HashMap; HashMap
If the Key of HashMap is a self-defined object, you generally need to override the equals () and hashCode () methods and follow the conventions between them.
Package simplejava;import java.util.HashMap;import java.util.Map.Entry;class Dog {String color; Dog (String c) {color = c;} public String toString () {return color + "dog";}} public class Q26 {public static void main (String [] args) {HashMap hashMap = new HashMap (); Dog D1 = new Dog ("red"); Dog D2 = new Dog ("black"); Dog D3 = new Dog ("white") Dog d4 = new Dog ("white"); hashMap.put (d1,10); hashMap.put (d2,15); hashMap.put (d3,5); hashMap.put (d4,20); / / print size System.out.println (hashMap.size ()); / / loop HashMap for (Entry entry: hashMap.entrySet ()) {System.out.println (entry.getKey (). ToString () + "-" + entry.getValue ()) }}}
Result output:
four
White dog-5
Red dog-10
White dog-20
Black dog-15
Notice that we accidentally added two "white dogs", but HashMap still stores it. This is unreasonable, and now we are wondering how many white dogs are stored in HashMap,5 or 20.
In fact, the Dog class should be defined like this:
Class Dog {String color; Dog (String c) {color = c;} public boolean equals (Object o) {return ((Dog) o) .color.equals (this.color);} public int hashCode () {return color.length ();} public String toString () {return color + "dog";}}
Result output:
three
Red dog-10
White dog-20
Black dog-15
The reason is that HashMap does not allow two identical elements to be stored, and by default, Object's hashCode () and equals () are used to determine whether two objects are the same. The default hashCode () method returns different values for different objects, while the equals () method returns true only if two references are equal, that is, pointing to the same object. If you are not sure, you can check the relationship between hashCode () and equals () for yourself.
For example, check out the most commonly used methods in HashMap, such as iteration,print, etc.
TreeMap
TreeMap is sorted by key, so let's first look at the following code to understand its "sort by key" idea.
Package simplejava;import java.util.Map.Entry;import java.util.TreeMap;class Dog {String color; Dog (String c) {color = c;} public boolean equals (Object o) {return ((Dog) o) .color.equals (this.color);} public int hashCode () {return color.length ();} public String toString () {return color + "dog" } public class Q26 {public static void main (String [] args) {Dog D1 = new Dog ("red"); Dog D2 = new Dog ("black"); Dog D3 = new Dog ("white"); Dog D4 = new Dog ("white"); TreeMap treeMap = new TreeMap (); treeMap.put (d1,10); treeMap.put (d2,15); treeMap.put (d3,5); treeMap.put (d4,20) For (Entry entry: treeMap.entrySet ()) {System.out.println (entry.getKey () + "-" + entry.getValue ());}
Result output:
Exception in thread "main" java.lang.ClassCastException: simplejava.Dog cannot be cast to java.lang.Comparable
At java.util.TreeMap.compare (TreeMap.java:1188)
At java.util.TreeMap.put (TreeMap.java:531)
At simplejava.Q26.main (Q26.java:34)
Because TreeSet is sorted based on Key, objects as key need to be compared to each other, which is why key needs to implement the Comparable interface. For example, you can use a string as a Key because String already implements the Comparable interface.
Now, let's change the Dog to make it comparable, as follows:
Package simplejava;import java.util.Map.Entry;import java.util.TreeMap;class Dog implements Comparable {String color; int size; Dog (String c, int s) {color = c; size = s;} public String toString () {return color + "dog";} @ Override public int compareTo (Dog o) {return o.size-this.size } public class Q26 {public static void main (String [] args) {Dog D1 = new Dog ("red", 30); Dog D2 = new Dog ("black", 20); Dog D3 = new Dog ("white", 10); Dog D4 = new Dog ("white", 10); TreeMap treeMap = new TreeMap (); treeMap.put (D1, 10); treeMap.put (D2,15); treeMap.put (D3,5) TreeMap.put (d4,20); for (Entry entry: treeMap.entrySet ()) {System.out.println (entry.getKey () + "-" + entry.getValue ());}
Result print:
Red dog-10
Black dog-15
White dog-20
In this example, we sort by dog's size
If "Dog D4 = new Dog (" white ", 10);" is replaced with "Dog D4 = new Dog (" white ", 40);", the following information will be output:
White dog-20
Red dog-10
Black dog-15
White dog-5
This is because the current TreeMap uses compareTo () to compare key, and different size means different dogs.
HashTable
Refer to the java documentation: HashMap is basically similar to HashTable, except that it is out of sync and allows null.
LinkedHashMap
LinkedHashMap is a subclass of HashMap, which means that it inherits the features of HashMap. In addition, LinkedHashMap preserves the insertion order.
Let's use the same code, and then replace HashMap with LinkedHashMap, as follows:
Package simplejava;import java.util.LinkedHashMap;import java.util.Map.Entry;class Dog {String color; Dog (String c) {color = c;} public boolean equals (Object o) {return ((Dog) o) .color.equals (this.color);} public int hashCode () {return color.length ();} public String toString () {return color + "dog" } public class Q26 {public static void main (String [] args) {Dog D1 = new Dog ("red"); Dog D2 = new Dog ("black"); Dog D3 = new Dog ("white"); Dog D4 = new Dog ("white"); LinkedHashMap linkedHashMap = new LinkedHashMap (); linkedHashMap.put (d1,10); linkedHashMap.put (d2,15); linkedHashMap.put (d3,5); linkedHashMap.put (d4,20) For (Entry entry: linkedHashMap.entrySet ()) {System.out.println (entry.getKey () + "-" + entry.getValue ());}
Output result:
Red dog-10
Black dog-15
White dog-20
If we use HashMap, the result is as follows, the difference is that the insertion order is not saved:
Red dog-10
White dog-20
Black dog-15
Thank you for reading this article carefully. I hope the article "how to use different Map in Java" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.