The 1,map interface object itself cannot be output directly using iterations. Because map stores a pair of values at each location.
And iterator can only find one value at a time. If you must iterate over the output, you can use the following steps. :
However, the map interface is used only as a lookup, and the output operation is a minority。
Packageclass set;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Set;ImportJava.util.WeakHashMap; Public classtest1{ Public Static voidMain (String args[]) {Map<String,String> map =NULL;//declares a Map object, where key and value are of type stringMap =NewHashmap<string,string>() ; Map.put ("Mldn", "www.mldn.cn");//Add ContentMap.put ("Zhinangtuan", "www.zhinangtuan.net.cn");//Add ContentMap.put ("Mldnjava", "www.mldnjava.cn");//Add Contentset<map.entry<string,string>> Allset =NULL ; Allset= Map.entryset () ; Iterator<map.entry<string,string>> iter =NULL ; ITER=Allset.iterator (); while(Iter.hasnext ()) { map.entry <String,String>me =Iter.next (); System.out.println (Me. GetKey () + "-+" +me. GetValue ()) ; } }};
Output Result:
mldn----www.mldnjava.cn
This output is ultimately output in the form of collection, just the type of operation with Map.entry as the content .
The type of key and value of map can also be used for non-system classes!
Packageclass set;ImportJava.util.HashMap;ImportJava.util.Map;classperson{PrivateString name; Private intAge ; Public Person(String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString toString () {return"Name:" + This. Name + "; Age:" + This. Age; }}; Public classtest1{ Public Static voidMain (String args[]) {Map<string, Person> map =NULL ; Map=NewHashmap<string,person>() ; Map.put ("Zhangsan",New Person ("Zhang San", +));//Add ContentSYSTEM.OUT.PRINTLN (map.get ("Zhangsan")) ; //getkey () method }};
Output Result:
Name: Zhang San; age: 30
But what if the non-system class is the type of the key?
Packageclass set;ImportJava.util.HashMap;ImportJava.util.Map;classperson{PrivateString name; Private intAge ; PublicPerson (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString toString () {return"Name:" + This. Name + "; Age:" + This. Age; }}; Public classtest1{ Public Static voidMain (String args[]) {Map< Person,string> map =NULL ; Map=Newhashmap< Person,string>() ; Map.put (New Person ("Zhang San", +), "Zhangsan");//Add ContentSystem.out.println (Map.get (New Person ("Zhang San",))) ; }};
Output Result:
Null
when the custom class is just a key, the returned result is empty . So why is it possible as a type of value?
For this kind of matching process, there is a feature, that is, the object should be the same as the content can be queried out .
The new person () anonymous method in the put and get methods above gets the same object .
Instance:
Packageclass set;ImportJava.util.HashMap;ImportJava.util.Map;classperson{PrivateString name; Private intAge ; PublicPerson (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString toString () {return"Name:" + This. Name + "; Age:" + This. Age; }}; Public classtest1{ Public Static voidMain (String args[]) {Map<Person,String> map =NULL ; Map=NewHashmap<person,string>() ; Person per=NewPerson ("Zhang San", 30) ; Map.put (per,"Zhangsan");//Add ContentSystem.out.println (Map.get (per)); }};
Output Result:
Zhangsan
But this is not the best solution, because the person and the per object cannot be taken away, and should be like a string, where the content can be found using an anonymous object.
It is then necessary to overwrite the method with the same form as the set interface in which the repeating element is judged .
If you want to use a non-system class as a key, this class must overwrite the following two methods of the object class:Hashcode () equals ()
Packageclass set;ImportJava.util.HashMap;ImportJava.util.Map;classperson{PrivateString name; Private intAge ; PublicPerson (String name,intAge ) { This. Name =name; This. Age =Age ; } PublicString toString () {return"Name:" + This. Name + "; Age:" + This. Age; } Public Boolean equals (Object obj) {//overwrite equals ()if( This==obj) { return true ; } if(! (objinstanceofPerson )) { return false ; } person P=(person) obj; if( This. Name.equals (P.name) && This. age==p.age) { return true ; }Else{ return false ; } } public int Hashcode () {//overwrite hashcode ()return This.name.hashCode () * this . Age; Custom Hashcode. }}; Public classtest1{ Public Static voidMain (String args[]) {Map<Person,String> map =NULL ; Map=NewHashmap<person,string>() ; Map.put (New Person ("Zhang San", +), "Zhangsan");//Add ContentSystem.out.println (Map.get (New Person ("Zhang San",))) ; }};
Output Result:
Zhangsan
Discover that using an anonymous object at this time can take out content. As an object, you actually rely on hashcode () and Equals () to determine whether an object is equal, which is done automatically by the internal system.
Summary: Map can use iterative output:
Map->entryset->set->iterator->map.entry->key and value.
The non-system class as key is guaranteed to overwrite the Equals () and Hashcode () methods, otherwise it is not valid.
Map Interface Usage Considerations