Absrtact: As the beginning of HashMap series of learning notes, I think we should familiarize myself with HashMap's usage scene first.
==========================================================================================
Map is essentially a data structure belonging to a collection type.
The following code snippet illustrates the common operation of Map (curd) and takes care to traverse the map as much as possible using the entry method.
1Hashmap<string, double> scores =NewHashmap<>();2 3Scores.put ("Sam", 100.0);4Scores.put ("Jacky", 50.0);5Scores.put ("Tim", 80.0);6 7 System.out.print (scores);8 9Scores.put ("Tim", 100.0);Ten System.out.print (scores); One System.out.print (scores.tostring ()); A -System.out.print (Scores.get ("Sam")); - theSystem.out.println (Scores.containskey ("Sam")); -System.out.println (Scores.containsvalue (100.0)); - - //Traverse, cost more time +Iterator<string> it =Scores.keyset (). iterator (); - while(It.hasnext ()) { +String tmp =It.next (); A SYSTEM.OUT.PRINTLN (TMP); at System.out.println (Scores.get (TMP)); - } - -System.out.println ("Count of student:" +scores.size ()); -SYSTEM.OUT.PRINTLN ("scores is" + (Scores.isempty ()? " Empty ":" Not Empty ")); - in //Traverse and delete target, cost less -iterator<map.entry<string, double>> begit =Scores.entryset (). iterator (); to for(; Begit.hasnext ();) { +map.entry<string, double> item =Begit.next (); - the if(Item.getvalue () = = 100.0) { * Begit.remove (); $ }Panax Notoginseng } - the System.out.println (scores); + A scores.clear (); theSYSTEM.OUT.PRINTLN (scores);
The output is as follows:
{tim=80.0, jacky=50.0, sam=100.0} {tim=100.0, jacky=50.0, sam=100.0} {tim=100.0, jacky=50.0, sam=100.0}100.0true
True
Tim
100.0
Jacky
50.0
Sam
100.0
Count of Student:3
Scores is not empty
{jacky=50.0}
{}
PS: The actual project HashMap with a few, complex operations (such as key HashMap) do not do a demonstration.
"JAVA" HashMap Getting Started to abort: using HashMap