JAVA - How To Use HashMap In Java With Iterator

JAVA - How To Use HashMap In Java With Iterator

                                                                                                                                                            
using hashmap with iterator in java


In this java Collection tutorial we will see How To Use A HashMap With Iterator In Java NetBeans

Source Code:

package JavaDB_001;
import java.util.*;


public class Work {
    public static void main(String[] args){
        //create a HashMap
        //Key is String 
        //value is Integer
       HashMap<String,Integer> employer = new HashMap<String,Integer>();
       
       //filling the HashMap with put method
       employer.put("employer 1", 34);
       employer.put("employer 2", 43);
       employer.put("employer 3", 36);
       employer.put("employer 5", 55);
       employer.put("employer 6", 29);
       employer.put("employer 7", 27);
       employer.put("employer 8", 30);
       employer.put("employer 9", 51);
       
       //displaying only the key of the hashmap
       System.out.println("_______Key_______");
         Iterator<String> itName = employer.keySet().iterator();
            while(itName.hasNext())
             System.out.println(itName.next());
       
       //displaying only the value of the hashmap
       System.out.println("_______Value_______");
        Iterator<Integer> itAge = employer.values().iterator();
          while(itAge.hasNext())
            System.out.println(itAge.next());
       
      //displaying the key and the value of the hashmap
       System.out.println("_______Key_And_Value_______");  
         Iterator<String> itname = employer.keySet().iterator();
            while(itname.hasNext()){
                String s = itname.next();
              System.out.println(itname.next()+" -*- "+employer.get(s));
           }
    }
       
}
///////////////////////////OUTPUT:
_______Key_______
employer 9
employer 5
employer 6
employer 7
employer 8
employer 1
employer 2
employer 3
_______Value_______
51
55
29
27
30
34
43
36
_______Key_And_Value_______
employer 9 -*- 51
employer 5 -*- 55
employer 6 -*- 29
employer 7 -*- 27
employer 8 -*- 30
employer 1 -*- 34
employer 2 -*- 43
employer 3 -*- 36


See Also: How To Use HashMap In Java



Share this

Related Posts

Previous
Next Post »