All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dstadler.commons.collections.MapUtils Maven / Gradle / Ivy

The newest version!
package org.dstadler.commons.collections;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Some helper methods related to Maps, e.g. sorting by value.
 */
public final class MapUtils {
    // private constructor to prevent instantiation of this class
    private MapUtils() {
    }

    /**
     * Sorts the provided Map by the value instead of by key like TreeMap does.
     *
     * The Value-Type needs to implement the Comparable-interface.
     *
     * Note: this will require one Map.Entry for each element in the Map, so for very large
     * Maps this will incur some memory overhead but will not fully duplicate the contents of the map.
     *
     * @param  the key-type of the Map that should be sorted
     * @param  the value-type of the Map that should be sorted. Needs to derive from {@link Comparable}
     *   
     * @param map A map with some elements which should be sorted by Value.
     *     
     *
     * @return Returns a new List of Map.Entry values sorted by value.
     */
    public static > List> sortByValue(Map map) {
        List> entries = new ArrayList<>(map.entrySet());
        entries.sort(new ByValue<>());
        return entries;
    }

    /**
     * Sorts the provided Map by the value and then by key instead of by key like TreeMap does.
     *
     * The Value-Type and Key-Type need to implement the Comparable-interface.
     *
     * Note: this will require one Map.Entry for each element in the Map, so for very large
     * Maps this will incur some memory overhead but will not fully duplicate the contents of the map.
     *
     * @param  the key-type of the Map that should be sorted Needs to derive from {@link Comparable}
     * @param  the value-type of the Map that should be sorted. Needs to derive from {@link Comparable}
     *
     * @param map A map with some elements which should be sorted by Value first and then by Key.
     *
     *
     * @return Returns a new List of Map.Entry values sorted by value and key.
     */
    public static , V extends Comparable> List> sortByValueAndKey(Map map) {
        List> entries = new ArrayList<>(map.entrySet());
        entries.sort(new ByValueAndKey<>());
        return entries;
    }

    // Helper class for sorting maps by value
    private static class ByValue> implements Comparator>, Serializable {
        private static final long serialVersionUID = 1L;

        @Override
        public int compare(Entry o1, Entry o2) {
            return o1.getValue().compareTo(o2.getValue());
        }
    }

    // Helper class for sorting maps by value and key
    private static class ByValueAndKey, V extends Comparable> implements Comparator>, Serializable {
        private static final long serialVersionUID = 1L;

        @Override
        public int compare(Map.Entry o1, Map.Entry o2) {
            int ret = o1.getValue().compareTo(o2.getValue());
            if(ret != 0) {
                return ret;
            }

            // on equal value, sort by key
            return o1.getKey().compareTo(o2.getKey());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy