按值排序HashMap我需要整理我的HashMap根据它中存储的值。这个HashMap包含存储在电话中的联系人名称。此外,我需要在对值进行排序之后,键就会自动排序,或者您可以说键和值是绑定在一起的,因此值中的任何更改都应该反映在键中。HashMap<Integer,String> map = new HashMap<Integer,String>();map.put(1,"froyo");map.put(2,"abby");map.put(3,"denver");map.put(4,"frost");map.put(5,"daisy");所需产出:2,abby;5,daisy;3,denver;4,frost;1,froyo;
3 回答
慕仙森
TA贡献1827条经验 获得超7个赞
public LinkedHashMap<Integer, String> sortHashMapByValues( HashMap<Integer, String> passedMap) { List<Integer> mapKeys = new ArrayList<>(passedMap.keySet()); List<String> mapValues = new ArrayList<>(passedMap.values()); Collections.sort(mapValues); Collections.sort(mapKeys); LinkedHashMap<Integer, String> sortedMap = new LinkedHashMap<>(); Iterator<String> valueIt = mapValues.iterator(); while (valueIt.hasNext()) { String val = valueIt.next(); Iterator<Integer> keyIt = mapKeys.iterator(); while (keyIt.hasNext()) { Integer key = keyIt.next(); String comp1 = passedMap.get(key); String comp2 = val; if (comp1.equals(comp2)) { keyIt.remove(); sortedMap.put(key, val); break; } } } return sortedMap;}
牛魔王的故事
TA贡献1830条经验 获得超3个赞
import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Map.Entry;public class SortMapByValue{ public static boolean ASC = true; public static boolean DESC = false; public static void main(String[] args) { // Creating dummy unsorted map Map<String, Integer> unsortMap = new HashMap<String, Integer>(); unsortMap.put("B", 55); unsortMap.put("A", 80); unsortMap.put("D", 20); unsortMap.put("C", 70); System.out.println("Before sorting......"); printMap(unsortMap); System.out.println("After sorting ascending order......"); Map<String, Integer> sortedMapAsc = sortByComparator(unsortMap, ASC); printMap(sortedMapAsc); System.out.println("After sorting descindeng order......"); Map<String, Integer> sortedMapDesc = sortByComparator(unsortMap, DESC); printMap(sortedMapDesc); } private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order) { List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet()); // Sorting the list based on values Collections.sort(list, new Comparator<Entry<String, Integer>>() { public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { if (order) { return o1.getValue().compareTo(o2.getValue()); } else { return o2.getValue().compareTo(o1.getValue()); } } }); // Maintaining insertion order with the help of LinkedList Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>(); for (Entry<String, Integer> entry : list) { sortedMap.put(entry.getKey(), entry.getValue()); } return sortedMap; } public static void printMap(Map<String, Integer> map) { for (Entry<String, Integer> entry : map.entrySet()) { System.out.println("Key : " + entry.getKey() + " Value : "+ entry.getValue()); } }}
使用了新的java特性,如流等
如果值相同,则按键对map进行排序。
import java.util.*; import java.util.Map.Entry; import java.util.stream.Collectors; public class SortMapByValue { private static boolean ASC = true; private static boolean DESC = false; public static void main(String[] args) { // Creating dummy unsorted map Map<String, Integer> unsortMap = new HashMap<>(); unsortMap.put("B", 55); unsortMap.put("A", 20); unsortMap.put("D", 20); unsortMap.put("C", 70); System.out.println("Before sorting......"); printMap(unsortMap); System.out.println("After sorting ascending order......"); Map<String, Integer> sortedMapAsc = sortByValue(unsortMap, ASC); printMap(sortedMapAsc); System.out.println("After sorting descending order......"); Map<String, Integer> sortedMapDesc = sortByValue(unsortMap, DESC); printMap(sortedMapDesc); } private static Map<String, Integer> sortByValue(Map<String, Integer> unsortMap, final boolean order) { List<Entry<String, Integer>> list = new LinkedList<>(unsortMap.entrySet()); // Sorting the list based on values list.sort((o1, o2) -> order ? o1.getValue().compareTo(o2.getValue()) == 0 ? o1.getKey().compareTo(o2.getKey()) : o1.getValue().compareTo(o2.getValue()) : o2.getValue().compareTo(o1.getValue()) == 0 ? o2.getKey().compareTo(o1.getKey()) : o2.getValue().compareTo(o1.getValue())); return list.stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue, (a, b) -> b, LinkedHashMap::new)); } private static void printMap(Map<String, Integer> map) { map.forEach((key, value) -> System.out.println("Key : " + key + " Value : " + value)); }}
白猪掌柜的
TA贡献1893条经验 获得超10个赞
Map<Integer, String> sortedMap = unsortedMap.entrySet().stream() .sorted(Entry.comparingByValue()) .collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
添加回答
举报
0/150
提交
取消