"JAVA" HashMap Getting Started to abort: using HashMap

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.