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
- Package com.pb.collection;
- Import Java.util.HashMap;
- Import Java.util.Iterator;
- Import Java.util.Set;
- Import Java.util.Map.Entry;
- Public class Hashmapdemo {
- public static void Main (string[] args) {
- hashmap<string, string> HashMap = new hashmap<string, string> ();
- Hashmap.put ("cn", "China");
- Hashmap.put ("JP", "Japan");
- Hashmap.put ("fr", "France");
- System.out.println (HASHMAP);
- System.out.println ("cn:" + hashmap.get ("cn"));
- System.out.println (Hashmap.containskey ("cn"));
- System.out.println (Hashmap.keyset ());
- System.out.println (Hashmap.isempty ());
- Hashmap.remove ("cn");
- System.out.println (Hashmap.containskey ("cn"));
- //Using iterator traversal HashMap
- Iterator it = Hashmap.keyset (). Iterator ();
- While (It.hasnext ()) {
- String key = (string) it.next ();
- System.out.println ("key:" + key);
- System.out.println ("value:" + hashmap.get (key));
- }
- //Another way to traverse HashMap
- set<entry<string, string>> sets = Hashmap.entryset ();
- for (entry<string, string> entry:sets) {
- System.out.print (Entry.getkey () + ",");
- System.out.println (Entry.getvalue ());
- }
- }
- }
2: An example of a combination of list and HASHMAP implementations
[Java]View Plaincopy
- Import Java.util.Iterator;
- Import java.util.List;
- Import Java.util.HashMap;
- Import java.util.ArrayList;
- Import Java.util.Map;
- Import Java.util.Scanner;
- Import Java.util.Set;
- Import Java.util.Map.Entry;
- /**
- * In the case of not creating student classes, enter n student information from the keyboard (school number, name, age),
- * When the input is complete, print out the individual student information
- * @author Ccna_zhang
- *
- */
- Public class Assignment {
- public static void Main (string[] args) {
- //Define a list that holds student information, with an element type of HashMap
- listnew arraylist
- Scanner input = new Scanner (system.in);
- System.out.println ("Please enter the student's information, y means continue, n means exit");
- While ("Y". Equals (Input.next ())) {
- hashmap<string, object> map = new hashmap<string, object> ();
- System.out.println ("Please enter the study number");
- Map.put ("Studentno", Input.next ());
- System.out.println ("Please enter name");
- Map.put ("name", Input.next ());
- System.out.println ("Please enter Age");
- Map.put ("Age", Input.nextint ());
- List.add (map);
- System.out.println ("Please continue to enter the student's information, y means continue, n means exit");
- }
- System.out.println ("The Student information entered is:");
- System.out.println ("Number of Students:" + list.size ());
- Iterator
- int i = 1;
- While (It.hasnext ()) {
- hashmap<string, object> stumap = It.next ();
- System.out.print ("First" + i + "student's Information");
- System.out.println ("School Number:" + stumap.get ("Studentno") + ", Name:" + stumap.get ("name") + ", Age:" + STUMAP.G ET ("age"));
- }
- }
- }
Usage and traversal of HashMap in Java (RPM)