Friday, 8 January 2016

How to traverse TreeMap in Java

Following are some of the ways to traverse TreeMap in Java.

a. Using the entrySet method.
/* Method 1 to traverse */
public static void traverseMapUsingEntrySet(Map<Integer, String> map) {
         Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
         while (it.hasNext()) {
                  Map.Entry<Integer, String> entry = it.next();
                  System.out.println(entry.getKey() + " = " + entry.getValue());
         }
}

b. By using keySet method and for-each loop
/* Method 2 to traverse*/
public static void traverseMapUsingKeySet(Map<Integer, String> map) {
         Set<Integer> keys = map.keySet();

         for (Integer key : keys) {
                  System.out.println(key + " = " + map.get(key));
         }
}

c. Using for-each loop and entrySet
/* Method 3 to traverse*/
public static void traverseMapUsingForEach(Map<Integer, String> map) {
         for (Map.Entry<Integer, String> entry : map.entrySet()) {
                  System.out.println(entry.getKey() + " = " + entry.getValue());
         }
}

d. Using keyset iterator
/* Method 4 to traverse*/
public static void traverseMapUsingKeySetIter(Map<Integer, String> map) {
         Set<Integer> keys = map.keySet();

         Iterator<Integer> iter = keys.iterator();
         while (iter.hasNext()) {
                  int key = iter.next();
                  System.out.println(key + " = " + map.get(key));
         }
}

e. Using streams
Streams are introduced in Java8, you can go through following link to know more about Java8 features and streams.


/* Method 5 to traverse */
public static void traverseMapUsingStreams(Map<Integer, String> map) {
         map.entrySet().stream().forEach(System.out::println);
}


Following is the complete working application.

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

public class TreeMapTraversalDemo {
 /* Method 1 to traverse */
 public static void traverseMapUsingEntrySet(Map<Integer, String> map) {
  Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<Integer, String> entry = it.next();
   System.out.println(entry.getKey() + " = " + entry.getValue());
  }
 }

 /* Method 2 to traverse */
 public static void traverseMapUsingKeySet(Map<Integer, String> map) {
  Set<Integer> keys = map.keySet();

  for (Integer key : keys) {
   System.out.println(key + " = " + map.get(key));
  }
 }

 /* Method 3 to traverse */
 public static void traverseMapUsingForEach(Map<Integer, String> map) {
  for (Map.Entry<Integer, String> entry : map.entrySet()) {
   System.out.println(entry.getKey() + " = " + entry.getValue());
  }
 }

 /* Method 4 to traverse */
 public static void traverseMapUsingKeySetIter(Map<Integer, String> map) {
  Set<Integer> keys = map.keySet();

  Iterator<Integer> iter = keys.iterator();
  while (iter.hasNext()) {
   int key = iter.next();
   System.out.println(key + " = " + map.get(key));
  }
 }

 /* Method 5 to traverse */
 public static void traverseMapUsingStreams(Map<Integer, String> map) {
  map.entrySet().stream().forEach(System.out::println);
 }

 public static void main(String args[]) {
  Map<Integer, String> treeMap = new TreeMap<>();

  treeMap.put(1, "Hari Krishna");
  treeMap.put(2, "Jaideep");
  treeMap.put(3, "Sailaja");
  treeMap.put(4, "Sankalp");

  System.out.println("Traverse Using the entrySet");
  System.out.println("**************************");
  traverseMapUsingEntrySet(treeMap);

  System.out.println("Traverse Using the keySet");
  System.out.println("**************************");
  traverseMapUsingKeySet(treeMap);

  System.out.println("Traverse Using the for-each");
  System.out.println("**************************");
  traverseMapUsingForEach(treeMap);

  System.out.println("Traverse Using the keySet iterator");
  System.out.println("**************************");
  traverseMapUsingKeySetIter(treeMap);

  System.out.println("Traverse Using stream");
  System.out.println("**************************");
  traverseMapUsingStreams(treeMap);
 }
}


Output

Traverse Using the entrySet
**************************
1 = Hari Krishna
2 = Jaideep
3 = Sailaja
4 = Sankalp
Traverse Using the keySet
**************************
1 = Hari Krishna
2 = Jaideep
3 = Sailaja
4 = Sankalp
Traverse Using the for-each
**************************
1 = Hari Krishna
2 = Jaideep
3 = Sailaja
4 = Sankalp
Traverse Using the keySet iterator
**************************
1 = Hari Krishna
2 = Jaideep
3 = Sailaja
4 = Sankalp
Traverse Using stream
**************************
1=Hari Krishna
2=Jaideep
3=Sailaja
4=Sankalp


You may like



                                                 Home

No comments:

Post a Comment