Wednesday 19 August 2015

Print the elemenets of HashMap, Hashtable or any Map in Java

1. Iterating or looping map using foreach loop.

for (String key : profiles.keySet()) {
     System.out.println("key:"+key+"value:"+profiles.get(key));
}

2. Looping HashMap in Java using EntrySet and for loop.

for (Entry<String, String> entry : entrySet) {
     System.out.println("key:"+entry.getKey()+"value:"+entry.getValue());
}

3. Iterating Map in Java using KeySet Iterator.

Set<String> keySet = profiles.keySet();
Iterator<String> keySetIterator = keySet.iterator();
while (keySetIterator.hasNext()) {
      String key = keySetIterator.next();
      System.out.println("key:"+key+"value:"+profiles.get(key));
}

4. Iterating HashMap in Java using EntrySet and Java iterator.

Set<Entry<String,String>> entrySet1 = profiles.entrySet();
Iterator<Entry<String, String>>  iter  = entrySet1.iterator();
while(iter.hasNext()) {
      Entry<String, String>  entry = iter.next();
      System.out.println("key:"+entry.getKey()+
                          "value:"+entry.getValue());
}

HashMapIterator.java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class HashMapIterator {
    public static void main(String[] args) {
          Map<String, String> profiles = new HashMap<String, String>();
          profiles.put("sattu""developer");
          profiles.put("sameer""senior developer");
          profiles.put("shan""architect");
          System.out.println("Iterating or looping map using Java5 foreach loop");
          for (String key : profiles.keySet()) {
                System.out.println("key:"+key+
                              "value:"+profiles.get(key));
          }
          System.out.println("Looping HashMap in Java using EntrySet");
          Set<Map.Entry<String, String>> entrySet = profiles.entrySet();
          for (Entry<String, String> entry : entrySet) {
               System.out.println("key:"+entry.getKey()+
                              "value:"+entry.getValue());
          }

          System.out.println("Looping HashMap in Java using EntrySet iterator");
          Set<Entry<String,String>> entrySet1 = profiles.entrySet();
          Iterator<Entry<String, String>>  iter  = entrySet1.iterator();
          while(iter.hasNext()) {
                 Entry<String, String>  entry = iter.next();
                 System.out.println("key:"+entry.getKey()+
                              "value:"+entry.getValue());
          }

          System.out.println("Iterating Map in Java using KeySet Iterator");
          Set<String> keySet = profiles.keySet();
          Iterator<String> keySetIterator = keySet.iterator();
          while (keySetIterator.hasNext()) {
                 String key = keySetIterator.next();
                 System.out.println("key:"+key+
                            "value:"+profiles.get(key));
          }
   }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...