sort Map By Key - Java java.util

Java examples for java.util:Map Sort

Description

sort Map By Key

Demo Code

//package com.java2s;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

public class Main {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static List<Set<Entry<String, Object>>> sortMapByKey(
            Map<String, ? extends Object> map) {
        if (map == null) {
            return null;
        }//from  w  w w . j a va 2  s .  co  m
        List<Set<Entry<String, Object>>> returnlist = new LinkedList(
                map.entrySet());
        Collections.sort(returnlist, new Comparator() {
            public int compare(Object o1, Object o2) {
                return ((Comparable) ((Map.Entry) (o1)).getKey())
                        .compareTo(((Map.Entry) (o2)).getKey());
            }
        });
        return returnlist;
    }
}

Related Tutorials