Print Map, sorted by Key. - Java java.util

Java examples for java.util:Map Operation

Description

Print Map, sorted by Key.

Demo Code


//package com.java2s;
import java.util.ArrayList;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class Main {
    /** Print Map, sorted by Key. */
    public static <K extends Comparable<K>, V> void printMap(Map<K, V> map) {
        List<K> keys = new ArrayList<>(map.keySet());
        Collections.sort(keys);//w w  w  . j  ava  2 s  . co m
        for (K key : keys) {
            System.out.println(key + ":" + map.get(key));
        }
    }
}

Related Tutorials