Sort a map recursively - Java java.util

Java examples for java.util:Map Sort

Description

Sort a map recursively

Demo Code


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class Main{
    /**//  www .j  av  a2 s  . c  o m
     * Sort a map recursively
     * 
     * @param in
     * @return 
     */
    public static TreeMap<String, Object> sortMap(Map<String, Object> in) {
        TreeMap<String, Object> out = new TreeMap<>(new StringComparator());

        for (Map.Entry<String, Object> entry : in.entrySet()) {

            String key = entry.getKey();
            Object value = entry.getValue();

            if (value instanceof Map) {
                out.put(key, sortMap((Map) value));
            } else {
                out.put(key, value);
            }
        }

        return out;
    }
}

Related Tutorials