// Mega Sena Generator - Java based Mega Sena Number Generator // Version // Copyright (C) 2007 Marcos Peron // http://www.marcosperon.com/download/ // // This library is free software; you can redistribute it and/or // modify it under the terms of either one of the following licences: // // 1. The Eclipse Public License (EPL) version 1.0, // included in this distribution in the file licence-epl-1.0.html // or available at http://www.eclipse.org/legal/epl-v10.html // // 2. The GNU Lesser General Public License (LGPL) version 2.1 or later, // included in this distribution in the file licence-lgpl-2.1.txt // or available at http://www.gnu.org/licenses/lgpl.txt // // This library is distributed on an "AS IS" basis, // WITHOUT WARRANTY OF ANY KIND, either express or implied. // See the individual licence texts for more details. package com.marcosperon.util; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; // TODO: Auto-generated Javadoc /** * The Class MapUtils. * * @author Marcos Peron */ public class MapUtils { /** * Reverse. * * @param map * the map * * @return the map< k, v> */ public static , V> Map reverse(Map map) { List> mapEntry = new LinkedList>(map .entrySet()); Collections.reverse(mapEntry); LinkedHashMap reversedMap = new LinkedHashMap(); for (Iterator> it = mapEntry.iterator(); it.hasNext();) { Map.Entry entry = it.next(); reversedMap.put(entry.getKey(), entry.getValue()); } return (reversedMap); } /** * Sort map by key. * * @param map * the map * * @return the map< k, v> */ public static , V> Map sortMapByKey( Map map) { return (MapUtils.sortMapByKey(map, true)); } /** * Sort map by key. * * @param map * the map * @param ascending * if will be ascending or descending * * @return the map< k, v> */ public static , V> Map sortMapByKey( Map map, boolean ascending) { Comparator c = new Comparator() { @Override public int compare(K o1, K o2) { return o1.compareTo(o2); } }; return (MapUtils.sortMapByKey(map, c, ascending)); } /** * Sort map by key. * * @param map * the map * @param c * the comparator * * @return the map< k, v> */ public static , V> Map sortMapByKey( Map map, final Comparator c) { return (MapUtils.sortMapByKey(map, c, true)); } /** * Sort map by key. * * @param map * the map * @param c * the comparator * @param ascending * if will be ascending or descending * * @return the map< k, v> */ public static , V> Map sortMapByKey( Map map, final Comparator c, boolean ascending) { List> mapEntry = new LinkedList>(map .entrySet()); Collections.sort(mapEntry, new Comparator>() { @Override public int compare(Entry o1, Entry o2) { return (c.compare(o1.getKey(), o2.getKey())); } }); if (!ascending) { Collections.reverse(mapEntry); } LinkedHashMap orderMap = new LinkedHashMap(); for (Iterator> it = mapEntry.iterator(); it.hasNext();) { Map.Entry entry = it.next(); orderMap.put(entry.getKey(), entry.getValue()); } return (orderMap); } /** * Sort map by value. * * @param map * the map * * @return the map< k, v> */ public static > Map sortMapByValue( Map map) { return (MapUtils.sortMapByValue(map, true)); } /** * Sort map by value. * * @param map * the map * @param ascending * if will be ascending or descending * * @return the map< k, v> */ public static > Map sortMapByValue( Map map, boolean ascending) { Comparator c = new Comparator() { @Override public int compare(V o1, V o2) { return o1.compareTo(o2); } }; return (MapUtils.sortMapByValue(map, c, ascending)); } /** * Sort map by value. * * @param map * the map * @param c * the comparator * * @return the map< k, v> */ public static > Map sortMapByValue( Map map, final Comparator c) { return (MapUtils.sortMapByValue(map, c, true)); } /** * Sort map by value. * * @param map * the map * @param c * the comparator * @param ascending * if will be ascending or descending * * @return the map< k, v> */ public static > Map sortMapByValue( Map map, final Comparator c, boolean ascending) { List> mapEntry = new LinkedList>(map .entrySet()); Collections.sort(mapEntry, new Comparator>() { @Override public int compare(Entry o1, Entry o2) { return (c.compare(o1.getValue(), o2.getValue())); } }); if (!ascending) { Collections.reverse(mapEntry); } LinkedHashMap orderMap = new LinkedHashMap(); for (Iterator> it = mapEntry.iterator(); it.hasNext();) { Map.Entry entry = it.next(); orderMap.put(entry.getKey(), entry.getValue()); } return (orderMap); } }