Map怎么实现按单个或多个Value排序

其他教程   发布日期:2023年06月10日   浏览次数:480

本篇内容主要讲解“Map怎么实现按单个或多个Value排序”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Map怎么实现按单个或多个Value排序”吧!

Map可以先按照value进行排序,然后按照key进行排序。 或者先按照key进行排序,然后按照value进行排序,这都是可以的。

并且,大家可以制定自己的排序规则。
按单个value排序:

  1. import java.util.Collections;
  2. import java.util.HashMap;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import static java.util.Map.Entry.comparingByValue;
  6. import static java.util.stream.Collectors.toMap;
  7. public class SortTest {
  8. public static void main(String[] args) throws Exception {
  9. // 创建一个字符串为Key,数字为值的map
  10. Map<String, Integer> budget = new HashMap<>();
  11. budget.put("clothes", 120);
  12. budget.put("grocery", 150);
  13. budget.put("transportation", 100);
  14. budget.put("utility", 130);
  15. budget.put("rent", 1150);
  16. budget.put("miscellneous", 90);
  17. System.out.println("排序前: " + budget);
  18. // 按值排序 升序
  19. Map<String, Integer> sorted = budget
  20. .entrySet()
  21. .stream()
  22. .sorted(comparingByValue())
  23. .collect(
  24. toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
  25. LinkedHashMap::new));
  26. System.out.println("升序按值排序后的map: " + sorted);
  27. // 按值排序降序
  28. sorted = budget
  29. .entrySet()
  30. .stream()
  31. .sorted(Collections.reverseOrder(comparingByValue()))
  32. .collect(
  33. toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
  34. LinkedHashMap::new));
  35. System.out.println("降序按值排序后的map: " + sorted);
  36. }
  37. }

按多个value排序:

  1. data = data.stream().sorted(Comparator.comparing(o -> {
  2. StringBuffer key = new StringBuffer();
  3. fieldList.stream().forEach((a)-> {
  4. key.append(o.get(a)+"");
  5. });
  6. return key.toString();
  7. } )).collect(Collectors.toList());

下面的代码中,首先按照value的数值从大到小进行排序,当value数值大小相同时,再按照key的长度从长到短进行排序,这个操作与Stream流式操作相结合。

  1. /**
  2. * Map按照整数型的value进行降序排序,当value相同时,按照key的长度进行排序
  3. *
  4. * @param map
  5. * @return
  6. */
  7. public static LinkedHashMap<String, Integer> sortMap(Map<String, Integer> map) {
  8. return map.entrySet().stream().sorted(((item1, item2) -> {
  9. int compare = item2.getValue().compareTo(item1.getValue());
  10. if (compare == 0) {
  11. if (item1.getKey().length() < item2.getKey().length()) {
  12. compare = 1;
  13. } else if (item1.getKey().length() > item2.getKey().length()) {
  14. compare = -1;
  15. }
  16. }
  17. return compare;
  18. })).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
  19. }

补充:对Map中的Value进行降序排序,当Value相同时,按照Key降序排序

  1. package com.ethjava;
  2. import java.util.*;
  3. public class mappaixu1 {
  4. public static void main(String[] args){
  5. Map<Integer,Integer> hashMap=new HashMap<Integer, Integer>();
  6. hashMap.put(1,10);
  7. hashMap.put(5,7);
  8. hashMap.put(2,9);
  9. hashMap.put(3,7);
  10. hashMap.put(3,6);//key是不可重复的,当这里再次输入Key=3时的,将会覆盖掉前面的(3,7)
  11. hashMap.put(4,7);
  12. //遍历
  13. for(Map.Entry<Integer,Integer> e:hashMap.entrySet()){
  14. System.out.println("Key: "+e.getKey()+"对应的Value: "+e.getValue());
  15. }
  16. //Key: 1对应的Value: 10
  17. //Key: 2对应的Value: 9
  18. //Key: 3对应的Value: 6
  19. //Key: 4对应的Value: 7
  20. //Key: 5对应的Value: 7
  21. //这里为什么自动按照key升序排序输出???为什么
  22. // 某梦说,这里是因为凑巧正序输出,hashMap输出相对于输入是无序的。
  23. //下面按照Value进行倒序排列
  24. ArrayList<Map.Entry<Integer,Integer>> arrayList=new ArrayList<Map.Entry<Integer, Integer>>(hashMap.entrySet());
  25. Collections.sort(arrayList,new Comparator<Map.Entry<Integer,Integer>>(){
  26. @Override
  27. public int compare(Map.Entry<Integer,Integer> o1,Map.Entry<Integer,Integer> o2 ){
  28. //按照Value进行倒序,若Value相同,按照Key正序排序
  29. //方法1:return o2.getValue() - o1.getValue();
  30. //方法2:return o2.getValue().compareTo(o1.getValue());//对于Integer,String都是可以应用的
  31. //按照Value进行倒序,若Value相同,按照Key倒序排序
  32. int result = o2.getValue().compareTo(o1.getValue());
  33. //方法学习:public int compareTo( NumberSubClass referenceName )
  34. //referenceName -- 可以是一个 Byte, Double, Integer, Float, Long 或 Short 类型的参数。
  35. //返回值:如果指定的数与参数相等返回0。
  36. // 如果指定的数小于参数返回 -1。
  37. //如果指定的数大于参数返回 1
  38. if(result!=0){
  39. return result;//即两个Value不相同,就按照Value倒序输出
  40. }else{
  41. return o2.getKey().compareTo(o1.getKey());}
  42. //若两个Value相同,就按照Key倒序输出
  43. }
  44. });
  45. //这里arrayList里的顺序已经按照自己的排序进行了调整
  46. for(int i=0;i<arrayList.size();i++){
  47. System.out.println(arrayList.get(i));
  48. //方法一和方法二输出:
  49. //1=10
  50. //2=9
  51. //4=7
  52. //5=7
  53. //3=6
  54. //当按照Value倒序排序,但是当Value相同时,按照Key顺序正序排序
  55. //方法二
  56. //1=10
  57. //2=9
  58. //5=7
  59. //4=7
  60. //3=6
  61. //当按照Value倒序输出,但是当Value相同时,按照Key倒序输出
  62. }
  63. for(Map.Entry<Integer,Integer> e:hashMap.entrySet()){
  64. System.out.println(e);
  65. //1=10
  66. //2=9
  67. //3=6
  68. //4=7
  69. //5=7
  70. //这里表明hashMap中存取的内容顺序并没有进行任何改变,改变的是arrayList里的内容的顺序
  71. }
  72. }
  73. }

以上就是Map怎么实现按单个或多个Value排序的详细内容,更多关于Map怎么实现按单个或多个Value排序的资料请关注九品源码其它相关文章!