Example one:
Import Java.util.map;import Java.util.set;import Java.util.hashmap;public class HashMapDemo1 {publicstatic void main ( String args[]) {User User1 = new User ("001", "Jack", 12); User User2 = New User ("002", "Marry", 13); User User3 = New User ("003", "Xiaoming", 14); User User4 = New User ("004", "Xiaofang", 15); HashMap one= New HashMap () One.put (User1.getid (), User1), One.put (User2.getid (), User2), One.put (User3.getid (), User3) ; One.put (User4.getid (), user4);//The number of key-value pairs System.out.println (one.size ());//Direct Print map object will print key tostring= Value ToStringSystem.out.println (one),//key and value are objectuser user = (user) One.get ("002"); SYSTEM.OUT.PRINTLN (user);//Whether it contains a keySystem.out.println (One.containskey ("122"));//traverse/Traverse method One Put all the keys in the map collection into the set set/* and then walk through the set set, Get key, get value * */set Set = One.keyset () by Key, and for (Object object:set) {String key = (St Ring) object; User U = (user) one.get (key); System.out.println (key+ "=" +u);} System.out.println ("=====================");//Key,value in HashMap collection will be packaged as/*map.entry this internal color property * There is a key-value pair that has a map.entryInstance objects * The EntrySet () method allows you to put these instance objects in the Set collection * Traverse set get each object * */set Set1 = One.entryset (); for (Object Object:set1) {map.entry me = (Map.entry) object; System.out.println (Me.getkey () + "=" +me.getvalue ());}}}
Example two:
Package Corelesson;import Java.util.hashmap;import Java.util.map;import Java.util.set;public class HashMapDemo2 { public static void Main (string[] args) {User U1 = new User ("001", "Zhang", 20); User U2 = New User ("002", "Xiaoming", 21); User U3 = New User ("003", "Nihao", 23); hashmap<string, user>users = new hashmap<string,user> (), Users.put (U1.getid (), U1), Users.put (U2.getId (), U2); Users.put (U3.getid (), U3); set<string> set = Users.keyset (); for (String Key:set) {System.out.println (key+ "= =" +users.get (key));} System.out.println ("+++++++++++++++++++++++++++"); set<map.entry<string, user>> Set1 = Users.entryset (); for (map.entry<string, user> me:set1) { System.out.println (Me.getkey () + "=" +me.getvalue ());}}}
Example three:
Package Corelesson;import Java.util.hashmap;public class HashMapDemo3 {public static void main (string[] args) {HashMap <String,Integer> HM = new hashmap<string,integer> (), Hm.put ("X", Ten), Hm.put ("Y"), Hm.put ("X", 30);// Key will overwrite the previous value key how to be the same */* note the implementation of equals and methods and the implementation of the Hashcode method * * */SYSTEM.OUT.PRINTLN (HM); System.out.println ("=====================");//Calculate the number of occurrences per character string s = "abcdafdfdfdasfdfgrgfrgsafgrgtads";/* * Idea: Get each character, as key into HashMap if * If the map does not exist then put (key,1); * If already exists then put (key,value+1) * * */hashmap<string,integer> hm1 = new hashmap<string,integer> (); for (int i = 0;i<s.length (); i++) {String str = s.substring (i,i+1); if (Hm1.containskey (str)) {hm1.put (Str,hm1.get (str) +1);} Else{hm1.put (str, 1);}} System.out.println (HM1);}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java Collection Class "HashMap" "introductory version, instance parsing"