Why the fuck do neither Java nor Guava have a Function to do this?
(Map<X,Y> aMap)
| 1447 | |
| 1448 | /** Why the fuck do neither Java nor Guava have a Function to do this? */ |
| 1449 | @SuppressWarnings("rawtypes") |
| 1450 | public static <X, Y extends Comparable> LinkedHashMap<X,Y> sortByValuesDescending(Map<X,Y> aMap) { |
| 1451 | List<Map.Entry<X,Y>> tEntrySet = new LinkedList<>(aMap.entrySet()); |
| 1452 | Collections.sort(tEntrySet, new Comparator<Map.Entry<X,Y>>() {@SuppressWarnings("unchecked") @Override public int compare(Entry<X, Y> aValue1, Entry<X, Y> aValue2) {return -aValue1.getValue().compareTo(aValue2.getValue());}}); |
| 1453 | LinkedHashMap<X,Y> rMap = new LinkedHashMap<>(); |
| 1454 | for (Map.Entry<X,Y> tEntry : tEntrySet) rMap.put(tEntry.getKey(), tEntry.getValue()); |
| 1455 | return rMap; |
| 1456 | } |
| 1457 | |
| 1458 | public static <E> E select(long aIndex, E aReplacement, List<E> aList) { |
| 1459 | if (aList == null || aList.isEmpty()) return aReplacement; |
no test coverage detected