Iterate over HashMap or any Map in java - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Monday, October 24, 2016

Iterate over HashMap or any Map in java

There are many ways to iterate, traverse or loop through HashMap or other Map and we all are familiar with some of the ways to iterate over Map. I have recently attended the interview and interviewer asked me to write all the ways to iterate or traverse on HashMap. This is important to know most of the ways to iterate, traverse or loop through Map orHashMap.

1. Iterating Map in java using iterator and KeySet

Here we use iterator to iterate on Map in java. we will get the set of key object using keySet() on map.

package com.walking.techie;

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

public class HashMapTraversal {

 public static void main(String[] args) {
  Map<Integer, String> map = new HashMap<Integer, String>();
  map.put(1, "Samsung");
  map.put(2, "Xiaomi");
  map.put(3, "Asus");
  map.put(4, "Motorola");
  System.out.println("1.------------------------------------------");
  // Returns a Set view of the keys contained in this map.
  Set<Integer> set = map.keySet();
  // Returns an iterator over the elements in this set.
  Iterator<Integer> itr = set.iterator();
  while (itr.hasNext()) {
   Integer key = itr.next();
   System.out.println("Key=" + key + " Value=" + map.get(key));
  }
 }
}

Output of above program:

1.------------------------------------------
Key=1 Value=Samsung
Key=2 Value=Xiaomi
Key=3 Value=Asus
Key=4 Value=Motorola

2. Iterating Map in java using EntrySet and Java Iterator

Here we will use EntrySet and Iterator to display key and value pair of map. EntrySet is a collection of all Map Entries and contains both Key and Value.

package com.walking.techie;

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

public class HashMapTraversal {

 public static void main(String[] args) {
  Map<Integer, String> map = new HashMap<Integer, String>();
  map.put(1, "Samsung");
  map.put(2, "Xiaomi");
  map.put(3, "Asus");
  map.put(4, "Motorola");
  System.out.println("2.------------------------------------------");
  // Returns a Set view of the mappings contained in this map.
  Set<java.util.Map.Entry<Integer, String>> entry = map.entrySet();
  // Returns an iterator over the elements in this set.
  Iterator<Entry<Integer, String>> iterator = entry.iterator();
  while (iterator.hasNext()) {
   Map.Entry<Integer, String> entry2 = (Entry<Integer, String>) iterator.next();
   System.out.println("Key=" + entry2.getKey() + " Value=" + entry2.getValue());

  }
 }
}

Output of above program:

2.------------------------------------------
Key=1 Value=Samsung
Key=2 Value=Xiaomi
Key=3 Value=Asus
Key=4 Value=Motorola

3. Iterating Map in java using EntrySet and Java for loop

Here we will use EntrySet and java for loop to display key value pairs of map.

package com.walking.techie;

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

public class HashMapTraversal {

 public static void main(String[] args) {
  Map<Integer, String> map = new HashMap<Integer, String>();
  map.put(1, "Samsung");
  map.put(2, "Xiaomi");
  map.put(3, "Asus");
  map.put(4, "Motorola");
  Set<Entry<Integer, String>> entrySet = map.entrySet();
  System.out.println("3.------------------------------------------");
  for (Entry<Integer, String> entry : entrySet) {
   System.out.println("Key=" + entry.getKey() + " Value=" + entry.getValue());
  }
 }
}

Output of above program:

3.------------------------------------------
Key=1 Value=Samsung
Key=2 Value=Xiaomi
Key=3 Value=Asus
Key=4 Value=Motorola

4. Iterating Map in java using KeySet and Java for loop

Here we will use new for each loop introduced in JDK5 for iterating over any map in java and using KeySet of map for getting keys. we will find the value corresponding to key in map.

package com.walking.techie;

import java.util.HashMap;
import java.util.Map;

public class HashMapTraversal {

 public static void main(String[] args) {
  Map<Integer, String> map = new HashMap<Integer, String>();
  map.put(1, "Samsung");
  map.put(2, "Xiaomi");
  map.put(3, "Asus");
  map.put(4, "Motorola");
  System.out.println("4.------------------------------------------");
  for (Integer key : map.keySet()) {
   System.out.println("Key=" + key + " Value=" + map.get(key));
  }
 }
}

Output of above program:

4.------------------------------------------
Key=1 Value=Samsung
Key=2 Value=Xiaomi
Key=3 Value=Asus
Key=4 Value=Motorola

5. Iterating Map in java using Java 8 forEach and lambda expression

In Java 8, we can loop a Map with forEach and lambda expression.

package com.walking.techie;

import java.util.HashMap;
import java.util.Map;

public class HashMapTraversal {

 public static void main(String[] args) {
  Map<Integer, String> map = new HashMap<Integer, String>();
  map.put(1, "Samsung");
  map.put(2, "Xiaomi");
  map.put(3, "Asus");
  map.put(4, "Motorola");
  System.out.println("5.------------------------------------------");
  // using java 8 for each and lambda expression
  map.forEach((k, v) -> {
   System.out.println("Key=" + k + " Value=" + v);
  });
 }
}

Output of above program:

5.------------------------------------------
Key=1 Value=Samsung
Key=2 Value=Xiaomi
Key=3 Value=Asus
Key=4 Value=Motorola

That's all on multiple ways to iterate, traverse and looping map in java. We have seen exactly five ways to iterate, traverse on java Map in combination of keySet, EntrySet using for loop and Iterator and with java 8 forEach.

Let me know if you are familiar with any other ways of iterating and getting each key value from Map in Java.

1 comment :