SortedMap interface and TreeMap Example - Walking Techie

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

Friday, February 17, 2017

SortedMap interface and TreeMap Example

Today, Here we will discuss implementation of TreeMap with SortedMap in java Collection framework. I have already discussed about SortedMap interface, methods and exceptions.

TreeMap interface has implemented Map, SortedMap, and NavigableMap interfaces. Therefore, besides the behaviors inherited from the Map, TreeMap also inherits the behaviors defined by SortedMap and NavigableMap. The following picture depicts the API hierarchy of TreeMap:

TreeMap class Hierarchy

SortedMap and TreeMap Example

The following example explain how to work with SortedMap interface methods with TreeMap.

package com.walking.techie;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

public class SortedMapWithTreeMap {

  public static void main(String[] args) {
    SortedMap<Integer, String> httpStatusMap = new TreeMap<>();
    httpStatusMap.put(100, "Continue");
    httpStatusMap.put(101, "Switching Protocols");
    httpStatusMap.put(102, "Processing");
    httpStatusMap.put(200, "OK");
    httpStatusMap.put(202, "Accepted");
    httpStatusMap.put(203, "Non Authoritative Information");
    httpStatusMap.put(300, "Multiple Choices");
    httpStatusMap.put(302, "Moved Temporarily");
    httpStatusMap.put(400, "Bad Request");
    httpStatusMap.put(401, "Unauthorized");
    httpStatusMap.put(405, "Method Not Allowed");
    httpStatusMap.put(500, "Server Error");
    httpStatusMap.put(503, "Service Unavailable");
    httpStatusMap.put(507, "Insufficient Storage");
    httpStatusMap.put(511, "Network Authentication Required");

    System.out.println("*****Range view operations on sorted map*****");

    System.out.println("*****All element of sub map view*****");
    SortedMap<Integer, String> subSortedMap = httpStatusMap.subMap(202, 500);
    for (Integer code : subSortedMap.keySet()) {
      System.out.println("Http Status Code : " + code + " Http Message : " + subSortedMap.get(code));
    }

    System.out.println("\n*****All element of head map view*****");
    SortedMap<Integer, String> headSortedMap = httpStatusMap.headMap(400);
    for (Integer code : headSortedMap.keySet()) {
      System.out.println("Http Status Code : " + code + " Http Message : " + headSortedMap.get(code));
    }

    System.out.println("\n*****All element of tail map view*****");
    SortedMap<Integer, String> tailSortedMap = httpStatusMap.tailMap(500);
    for (Integer code : tailSortedMap.keySet()) {
      System.out.println("Http Status Code : " + code + " Http Message : " + tailSortedMap.get(code));
    }
    System.out.println("*****End Range view operations on sorted map*****");
    System.out.println("\n*****Endpoint operations*****");
    System.out.println("*****First key and first value of SortedMap*****");
    Integer firstKey = httpStatusMap.firstKey();
    String firstValue = httpStatusMap.get(firstKey);
    System.out.println("Http Status Code : " + firstKey + " Http Message : " + firstValue);

    System.out.println("*****Last key and last value of SortedMap*****");
    Integer lastKey = httpStatusMap.lastKey();
    String lastValue = httpStatusMap.get(lastKey);
    System.out.println("Http Status Code : " + lastKey + " Http Message : " + lastValue);
    System.out.println("\n*****End of endpoint operations*****");

    System.out.println("\n***** All key-value of sorted map*****");
    for (Integer code : httpStatusMap.keySet()) {
      System.out.println("Http Status Code : " + code + " Http Message : " + headSortedMap.get(code));
    }

    System.out.println("\n*****Collection views returned from SortedMap");
    Collection<String> values = httpStatusMap.values();
    for (String message : values) {
      System.out.println("Http Message : " + message);
    }

    System.out.println("\n*****Iterate over SortedMap using entry set******");
    Set<Entry<Integer, String>> entrySet = httpStatusMap.entrySet();
    Iterator<Entry<Integer, String>> iterator = entrySet.iterator();
    while (iterator.hasNext()) {
      Entry<Integer, String> entry = iterator.next();
      System.out.println("Http Status Code : " + entry.getKey() + " Http Message : " + entry.getValue());
    }
  }
}

Output of above program is shown below:

*****Range view operations on sorted map*****
*****All element of sub map view*****
Http Status Code : 202 Http Message : Accepted
Http Status Code : 203 Http Message : Non Authoritative Information
Http Status Code : 300 Http Message : Multiple Choices
Http Status Code : 302 Http Message : Moved Temporarily
Http Status Code : 400 Http Message : Bad Request
Http Status Code : 401 Http Message : Unauthorized
Http Status Code : 405 Http Message : Method Not Allowed

*****All element of head map view*****
Http Status Code : 100 Http Message : Continue
Http Status Code : 101 Http Message : Switching Protocols
Http Status Code : 102 Http Message : Processing
Http Status Code : 200 Http Message : OK
Http Status Code : 202 Http Message : Accepted
Http Status Code : 203 Http Message : Non Authoritative Information
Http Status Code : 300 Http Message : Multiple Choices
Http Status Code : 302 Http Message : Moved Temporarily

*****All element of tail map view*****
Http Status Code : 500 Http Message : Server Error
Http Status Code : 503 Http Message : Service Unavailable
Http Status Code : 507 Http Message : Insufficient Storage
Http Status Code : 511 Http Message : Network Authentication Required
*****End Range view operations on sorted map*****

*****Endpoint operations*****
*****First key and first value of SortedMap*****
Http Status Code : 100 Http Message : Continue
*****Last key and last value of SortedMap*****
Http Status Code : 511 Http Message : Network Authentication Required

*****End of endpoint operations*****

***** All key-value of sorted map*****
Http Status Code : 100 Http Message : Continue
Http Status Code : 101 Http Message : Switching Protocols
Http Status Code : 102 Http Message : Processing
Http Status Code : 200 Http Message : OK
Http Status Code : 202 Http Message : Accepted
Http Status Code : 203 Http Message : Non Authoritative Information
Http Status Code : 300 Http Message : Multiple Choices
Http Status Code : 302 Http Message : Moved Temporarily
Http Status Code : 400 Http Message : null
Http Status Code : 401 Http Message : null
Http Status Code : 405 Http Message : null
Http Status Code : 500 Http Message : null
Http Status Code : 503 Http Message : null
Http Status Code : 507 Http Message : null
Http Status Code : 511 Http Message : null

*****Collection views returned from SortedMap
Http Message : Continue
Http Message : Switching Protocols
Http Message : Processing
Http Message : OK
Http Message : Accepted
Http Message : Non Authoritative Information
Http Message : Multiple Choices
Http Message : Moved Temporarily
Http Message : Bad Request
Http Message : Unauthorized
Http Message : Method Not Allowed
Http Message : Server Error
Http Message : Service Unavailable
Http Message : Insufficient Storage
Http Message : Network Authentication Required

*****Iterate over SortedMap using entry set******
Http Status Code : 100 Http Message : Continue
Http Status Code : 101 Http Message : Switching Protocols
Http Status Code : 102 Http Message : Processing
Http Status Code : 200 Http Message : OK
Http Status Code : 202 Http Message : Accepted
Http Status Code : 203 Http Message : Non Authoritative Information
Http Status Code : 300 Http Message : Multiple Choices
Http Status Code : 302 Http Message : Moved Temporarily
Http Status Code : 400 Http Message : Bad Request
Http Status Code : 401 Http Message : Unauthorized
Http Status Code : 405 Http Message : Method Not Allowed
Http Status Code : 500 Http Message : Server Error
Http Status Code : 503 Http Message : Service Unavailable
Http Status Code : 507 Http Message : Insufficient Storage
Http Status Code : 511 Http Message : Network Authentication Required

SortedMap with TreeMap using Comparator

package com.walking.techie;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

public class SortedMapTreeMapWithComparator {

  public static void main(String[] args) {
    SortedMap<Integer, String> httpStatusMap = new TreeMap<>(new ReverseComparator());
    httpStatusMap.put(100, "Continue");
    httpStatusMap.put(101, "Switching Protocols");
    httpStatusMap.put(102, "Processing");
    httpStatusMap.put(200, "OK");
    httpStatusMap.put(202, "Accepted");
    httpStatusMap.put(203, "Non Authoritative Information");
    httpStatusMap.put(300, "Multiple Choices");
    httpStatusMap.put(302, "Moved Temporarily");
    httpStatusMap.put(400, "Bad Request");
    httpStatusMap.put(401, "Unauthorized");
    httpStatusMap.put(405, "Method Not Allowed");
    httpStatusMap.put(500, "Server Error");
    httpStatusMap.put(503, "Service Unavailable");
    httpStatusMap.put(507, "Insufficient Storage");
    httpStatusMap.put(511, "Network Authentication Required");

    for (Integer code : httpStatusMap.keySet()) {
      System.out.println("Http Status Code : " + code + " Http Message : " + httpStatusMap.get(code));
    }

  }

  private static class ReverseComparator implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
      return o2 - o1;
    }
  }
}

Output of above program is shown below:

Http Status Code : 511 Http Message : Network Authentication Required
Http Status Code : 507 Http Message : Insufficient Storage
Http Status Code : 503 Http Message : Service Unavailable
Http Status Code : 500 Http Message : Server Error
Http Status Code : 405 Http Message : Method Not Allowed
Http Status Code : 401 Http Message : Unauthorized
Http Status Code : 400 Http Message : Bad Request
Http Status Code : 302 Http Message : Moved Temporarily
Http Status Code : 300 Http Message : Multiple Choices
Http Status Code : 203 Http Message : Non Authoritative Information
Http Status Code : 202 Http Message : Accepted
Http Status Code : 200 Http Message : OK
Http Status Code : 102 Http Message : Processing
Http Status Code : 101 Http Message : Switching Protocols
Http Status Code : 100 Http Message : Continue

No comments :

Post a Comment