The use and traversal of HashMap in Java

Source: Internet
Author: User

the use and traversal of HashMap in JavaCategory: Algorithms and data Structures 2011-10-19 10:53 5345 people read reviews (0) favorite reports Hashmapjavastringobjectiteratorlist

1: A simple example of using HashMap

[Java]View Plaincopy
  1. Package com.pb.collection;
  2. Import Java.util.HashMap;
  3. Import Java.util.Iterator;
  4. Import Java.util.Set;
  5. Import Java.util.Map.Entry;
  6. Public class Hashmapdemo {
  7. public static void Main (string[] args) {
  8. hashmap<string, string> HashMap = new hashmap<string, string> ();
  9. Hashmap.put ("cn", "China");
  10. Hashmap.put ("JP", "Japan");
  11. Hashmap.put ("fr", "France");
  12. System.out.println (HASHMAP);
  13. System.out.println ("cn:" + hashmap.get ("cn"));
  14. System.out.println (Hashmap.containskey ("cn"));
  15. System.out.println (Hashmap.keyset ());
  16. System.out.println (Hashmap.isempty ());
  17. Hashmap.remove ("cn");
  18. System.out.println (Hashmap.containskey ("cn"));
  19. //Using iterator traversal HashMap
  20. Iterator it = Hashmap.keyset (). Iterator ();
  21. While (It.hasnext ()) {
  22. String key = (string) it.next ();
  23. System.out.println ("key:" + key);
  24. System.out.println ("value:" + hashmap.get (key));
  25. }
  26. //Another way to traverse HashMap
  27. set<entry<string, string>> sets = Hashmap.entryset ();
  28. for (entry<string, string> entry:sets) {
  29. System.out.print (Entry.getkey () + ",");
  30. System.out.println (Entry.getvalue ());
  31. }
  32. }
  33. }


2: An example of a combination of list and HASHMAP implementations

[Java]View Plaincopy
  1. Import Java.util.Iterator;
  2. Import java.util.List;
  3. Import Java.util.HashMap;
  4. Import java.util.ArrayList;
  5. Import Java.util.Map;
  6. Import Java.util.Scanner;
  7. Import Java.util.Set;
  8. Import Java.util.Map.Entry;
  9. /**
  10. * In the case of not creating student classes, enter n student information from the keyboard (school number, name, age),
  11. * When the input is complete, print out the individual student information
  12. * @author Ccna_zhang
  13. *
  14. */
  15. Public class Assignment {
  16. public static void Main (string[] args) {
  17. //Define a list that holds student information, with an element type of HashMap
  18. listnew arraylist
  19. Scanner input = new Scanner (system.in);
  20. System.out.println ("Please enter the student's information, y means continue, n means exit");
  21. While ("Y". Equals (Input.next ())) {
  22. hashmap<string, object> map = new hashmap<string, object> ();
  23. System.out.println ("Please enter the study number");
  24. Map.put ("Studentno", Input.next ());
  25. System.out.println ("Please enter name");
  26. Map.put ("name", Input.next ());
  27. System.out.println ("Please enter Age");
  28. Map.put ("Age", Input.nextint ());
  29. List.add (map);
  30. System.out.println ("Please continue to enter the student's information, y means continue, n means exit");
  31. }
  32. System.out.println ("The Student information entered is:");
  33. System.out.println ("Number of Students:" + list.size ());
  34. Iterator
  35. int i = 1;
  36. While (It.hasnext ()) {
  37. hashmap<string, object> stumap = It.next ();
  38. System.out.print ("First" + i + "student's Information");
  39. System.out.println ("School Number:" + stumap.get ("Studentno") + ", Name:" + stumap.get ("name") + ", Age:" + STUMAP.G  ET ("age"));
  40. }
  41. }
  42. }

Usage and traversal of HashMap in Java (RPM)

Related Article

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.