Java Map Sort sortMapByKey(Map map)

Here you can find the source of sortMapByKey(Map map)

Description

sort Map By Key

License

Apache License

Declaration

@SuppressWarnings({ "unchecked", "rawtypes" })
    public static List<Set<Entry<String, Object>>> sortMapByKey(Map<String, ? extends Object> map) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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  av a  2s.  c o  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

  1. sortMap(Map oldMap)
  2. sortMap(Map oldMap, final boolean asc)
  3. sortMap(Map map, int k)
  4. sortMap(Map map, Comparator> compator)
  5. sortMapByKey(Map data)
  6. sortMapByValue(Map input, final boolean desc)
  7. sortMapByValue(Map map, Comparator comparator)
  8. sortMapByValue(Map map, Comparator> valueComparator)
  9. sortMapByValue(Map map, Comparator> comparator)