The map interface differs from collection:
The set elements in the collection are isolated, and can be understood as single, and are stored in a single-column collection
The set elements in the map are paired, and can be understood as a couple, a pair of pairs, called a double-column collection.
The map is stored in: Key value pairs, keys can not be repeated, values can be repeated
Common collections in the map interface:
1.HASHMAP: The storage structure of the hash table, but cannot guarantee the access order
2.LINKEDHASHMAP: Store data using hash tables and linked lists, which can be ordered
Common methods for map interfaces:
Example:
Packagedemo;ImportJava.util.HashMap;ImportJava.util.Map; Public classMapdemo { Public Static voidMain (string[] args) {function1 (); Function2 (); Function3 (); } Public Static voidfunction1 () {//storing key-value pairs in a collectionmap<string, integer> map =NewHashmap<string, integer>(); Map.put ("A", 1); Map.put ("B", 2); Map.put ("C", 3); Map.put ("C", 4); SYSTEM.OUT.PRINTLN (map); //{b=2, c=4, a=1}//= Connection key value pairs, which are stored in duplicate keys, will overwrite } Public Static voidfunction2 () {//get a value by keyMap<integer, string> map =NewHashmap<integer, string>(); Map.put (1, "a"); Map.put (2, "B"); Map.put (3, "C"); String value= Map.get (1); System.out.println (value);//a//There is a return value, there is no return null } Public Static voidFunction3 () {//remove a key-value pair from a collectionMap<integer, string> map =NewHashmap<integer, string>(); Map.put (1, "a"); Map.put (2, "B"); Map.put (3, "C"); Map.Remove (3); SYSTEM.OUT.PRINTLN (map); //{1=a, 2=b} }}
Traversal of the Map interface:
The first way:
Packagedemo;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Set; Public classMapdemo { Public Static voidMain (string[] args) {function1 (); Function2 (); } Public Static voidfunction1 () {Map<string, integer> map =NewHashmap<string, integer>(); Map.put ("A", 1); Map.put ("B", 2); Map.put ("C", 3); Map.put ("D", 4); Set<String> set =Map.keyset (); Iterator<String> it =Set.iterator (); while(It.hasnext ()) {String key=It.next (); Integer value=Map.get (key); SYSTEM.OUT.PRINTLN (Key+ "<==>" +value); } } Public Static voidfunction2 () {Map<string, integer> map =NewHashmap<string, integer>(); Map.put ("A", 1); Map.put ("B", 2); Map.put ("C", 3); Map.put ("D", 4); for(String key:map.keySet ()) {Integer value=Map.get (key); SYSTEM.OUT.PRINTLN (Key+ "<==>" +value); } }}//unordered traversal of output
The second way (according to the mapping relationship):
Packagedemo;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Map.Entry;ImportJava.util.Set; Public classMapdemo { Public Static voidMain (string[] args) {Map<integer, string> map =NewHashmap<integer, string>(); Map.put (1, "a"); Map.put (2, "B"); Map.put (3, "C"); Set<entry<integer, string>> set =Map.entryset (); Iterator<entry<integer, string>> it =Set.iterator (); while(It.hasnext ()) {Entry<integer, string> entry =It.next (); Integer Key=Entry.getkey (); String value=Entry.getvalue (); SYSTEM.OUT.PRINTLN (Key+ "<==>" +value); } }}
To store your custom objects:
Packagedemo; Public classPerson {PrivateString name; Private intAge ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicPerson (String name,intAge ) { Super(); This. Name =name; This. Age =Age ; } PublicPerson () {Super(); //TODO auto-generated Constructor stub} @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + "]"; } @Override Public inthashcode () {Final intPrime = 31; intresult = 1; Result= Prime * result +Age ; Result= Prime * result + ((name = =NULL) ? 0: Name.hashcode ()); returnresult; } @Override Public Booleanequals (Object obj) {if( This==obj)return true; if(obj = =NULL) return false; if(GetClass ()! =Obj.getclass ())return false; person Other=(person) obj; if(Age! =other.age)return false; if(Name = =NULL) { if(Other.name! =NULL) return false; } Else if(!name.equals (other.name))return false; return true; }}
Packagedemo;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Map.Entry;//Store Custom Types Public classHashmapdemo { Public Static voidMain (string[] args) {function1 (); Function2 (); } Public Static voidfunction1 () {//custom types appear as valueshashmap<string, person> map =NewHashmap<string, person>(); Map.put ("A",NewPerson ("1", 20)); Map.put ("B",NewPerson ("2", 20)); Map.put ("C",NewPerson ("3", 20)); //using two kinds of traversal for(String key:map.keySet ()) {person value=Map.get (key); SYSTEM.OUT.PRINTLN (Key+ "<==>" +value); } for(Entry<string, person>Entry:map.entrySet ()) {String key=Entry.getkey (); Person Value=Entry.getvalue (); SYSTEM.OUT.PRINTLN (Key+ "<==>" +value); } } Public Static voidfunction2 () {//custom types appear as keys//guaranteed key uniqueness, need to override Hashcode and Equals methodHashmap<person, string> map =NewHashmap<person, string>(); Map.put (NewPerson ("A", "a"), "a"); Map.put (NewPerson ("B", +), "a"); Map.put (NewPerson ("C", +), "a"); Map.put (NewPerson ("C", +), "a"); //two types of traversal for(Person Key:map.keySet ()) {String value=Map.get (key); SYSTEM.OUT.PRINTLN (Key+ "<==>" +value); } for(Entry<person, string>Entry:map.entrySet ()) {System.out.println (Entry.getkey ()+ "<==>" +Entry.getvalue ()); } }}
Linkedhashmap Collection:
Packagedemo;ImportJava.util.LinkedHashMap; Public classLinkedhashmapdemo { Public Static voidMain (string[] args) {Linkedhashmap<string, string> link =NewLinkedhashmap<string, string>(); Link.put ("1", "a"); Link.put ("2", "a"); Link.put ("3", "a"); Link.put ("4", "a"); System.out.println (link); //{1=a, 2=a, 3=a, 4=a}//Consistent Access Order }}
There is also a Hashtable set under the set interface, but it is obsolete and is now replaced by HashMap
However, one issue to note:
HashMap allows null values to be stored, Hashtable does not allow null values to be stored, neither of which allows null keys to be stored
Java Learning Note 32 (Set Framework VI: MAP interface)