本篇内容主要讲解“Map怎么实现按单个或多个Value排序”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Map怎么实现按单个或多个Value排序”吧!
Map可以先按照value进行排序,然后按照key进行排序。 或者先按照key进行排序,然后按照value进行排序,这都是可以的。
并且,大家可以制定自己的排序规则。
按单个value排序:
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import static java.util.Map.Entry.comparingByValue;
- import static java.util.stream.Collectors.toMap;
- public class SortTest {
- public static void main(String[] args) throws Exception {
- // 创建一个字符串为Key,数字为值的map
- Map<String, Integer> budget = new HashMap<>();
- budget.put("clothes", 120);
- budget.put("grocery", 150);
- budget.put("transportation", 100);
- budget.put("utility", 130);
- budget.put("rent", 1150);
- budget.put("miscellneous", 90);
- System.out.println("排序前: " + budget);
- // 按值排序 升序
- Map<String, Integer> sorted = budget
- .entrySet()
- .stream()
- .sorted(comparingByValue())
- .collect(
- toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
- LinkedHashMap::new));
- System.out.println("升序按值排序后的map: " + sorted);
- // 按值排序降序
- sorted = budget
- .entrySet()
- .stream()
- .sorted(Collections.reverseOrder(comparingByValue()))
- .collect(
- toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
- LinkedHashMap::new));
- System.out.println("降序按值排序后的map: " + sorted);
- }
- }
按多个value排序:
- data = data.stream().sorted(Comparator.comparing(o -> {
- StringBuffer key = new StringBuffer();
- fieldList.stream().forEach((a)-> {
- key.append(o.get(a)+"");
- });
- return key.toString();
- } )).collect(Collectors.toList());
下面的代码中,首先按照value的数值从大到小进行排序,当value数值大小相同时,再按照key的长度从长到短进行排序,这个操作与Stream流式操作相结合。
- /**
- * Map按照整数型的value进行降序排序,当value相同时,按照key的长度进行排序
- *
- * @param map
- * @return
- */
- public static LinkedHashMap<String, Integer> sortMap(Map<String, Integer> map) {
- return map.entrySet().stream().sorted(((item1, item2) -> {
- int compare = item2.getValue().compareTo(item1.getValue());
- if (compare == 0) {
- if (item1.getKey().length() < item2.getKey().length()) {
- compare = 1;
- } else if (item1.getKey().length() > item2.getKey().length()) {
- compare = -1;
- }
- }
- return compare;
- })).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
- }
- package com.ethjava;
- import java.util.*;
- public class mappaixu1 {
- public static void main(String[] args){
- Map<Integer,Integer> hashMap=new HashMap<Integer, Integer>();
- hashMap.put(1,10);
- hashMap.put(5,7);
- hashMap.put(2,9);
- hashMap.put(3,7);
- hashMap.put(3,6);//key是不可重复的,当这里再次输入Key=3时的,将会覆盖掉前面的(3,7)
- hashMap.put(4,7);
- //遍历
- for(Map.Entry<Integer,Integer> e:hashMap.entrySet()){
- System.out.println("Key: "+e.getKey()+"对应的Value: "+e.getValue());
- }
- //Key: 1对应的Value: 10
- //Key: 2对应的Value: 9
- //Key: 3对应的Value: 6
- //Key: 4对应的Value: 7
- //Key: 5对应的Value: 7
- //这里为什么自动按照key升序排序输出???为什么
- // 某梦说,这里是因为凑巧正序输出,hashMap输出相对于输入是无序的。
- //下面按照Value进行倒序排列
- ArrayList<Map.Entry<Integer,Integer>> arrayList=new ArrayList<Map.Entry<Integer, Integer>>(hashMap.entrySet());
- Collections.sort(arrayList,new Comparator<Map.Entry<Integer,Integer>>(){
- @Override
- public int compare(Map.Entry<Integer,Integer> o1,Map.Entry<Integer,Integer> o2 ){
- //按照Value进行倒序,若Value相同,按照Key正序排序
- //方法1:return o2.getValue() - o1.getValue();
- //方法2:return o2.getValue().compareTo(o1.getValue());//对于Integer,String都是可以应用的
- //按照Value进行倒序,若Value相同,按照Key倒序排序
- int result = o2.getValue().compareTo(o1.getValue());
- //方法学习:public int compareTo( NumberSubClass referenceName )
- //referenceName -- 可以是一个 Byte, Double, Integer, Float, Long 或 Short 类型的参数。
- //返回值:如果指定的数与参数相等返回0。
- // 如果指定的数小于参数返回 -1。
- //如果指定的数大于参数返回 1
- if(result!=0){
- return result;//即两个Value不相同,就按照Value倒序输出
- }else{
- return o2.getKey().compareTo(o1.getKey());}
- //若两个Value相同,就按照Key倒序输出
- }
- });
- //这里arrayList里的顺序已经按照自己的排序进行了调整
- for(int i=0;i<arrayList.size();i++){
- System.out.println(arrayList.get(i));
- //方法一和方法二输出:
- //1=10
- //2=9
- //4=7
- //5=7
- //3=6
- //当按照Value倒序排序,但是当Value相同时,按照Key顺序正序排序
- //方法二
- //1=10
- //2=9
- //5=7
- //4=7
- //3=6
- //当按照Value倒序输出,但是当Value相同时,按照Key倒序输出
- }
- for(Map.Entry<Integer,Integer> e:hashMap.entrySet()){
- System.out.println(e);
- //1=10
- //2=9
- //3=6
- //4=7
- //5=7
- //这里表明hashMap中存取的内容顺序并没有进行任何改变,改变的是arrayList里的内容的顺序
- }
- }
- }
以上就是Map怎么实现按单个或多个Value排序的详细内容,更多关于Map怎么实现按单个或多个Value排序的资料请关注九品源码其它相关文章!