Here you can find the source of sortMapUsingComparator(final Map
Parameter | Description |
---|---|
T | key type |
V | value type |
map | Map, which will be sorted |
comparator | Comparator for comparing keys |
public static <T, V> Map<T, V> sortMapUsingComparator(final Map<T, V> map, final Comparator<T> comparator)
//package com.java2s; /**/*from ww w .j a v a 2 s .co m*/ * *************************************************************************** * Copyright (c) 2010 Qcadoo Limited * Project: Qcadoo Framework * Version: 1.2.0 * * This file is part of Qcadoo. * * Qcadoo is free software; you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published * by the Free Software Foundation; either version 3 of the License, * or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *************************************************************************** */ import java.util.Collections; import java.util.Comparator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; public class Main { /** * Sort given map by keys, using specified comparator * * @param <T> * key type * @param <V> * value type * @param map * Map, which will be sorted * @param comparator * Comparator for comparing keys * @return new instance of map, sorted by keys */ public static <T, V> Map<T, V> sortMapUsingComparator(final Map<T, V> map, final Comparator<T> comparator) { List<T> operationList = new LinkedList<T>(map.keySet()); Collections.sort(operationList, comparator); Map<T, V> result = new LinkedHashMap<T, V>(); for (T key : operationList) { result.put(key, map.get(key)); } return result; } }