List<HashMap<String, Object>>排序

如果列表中的元素类型是List<HashMap<String, Object>>,排序时需要考虑到value可能是任意类型的对象。在这种情况下,你可以针对具体的类型进行比较,或者使用Comparable接口来确保对象可以被正确比较。

示例代码

假设我们想要根据value的字符串表示来进行排序,可以使用toString()方法将Object转换为String,然后进行比较。下面是一个示例:

java 复制代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SortByValue {

    public static void main(String[] args) {
        // 示例数据
        List<HashMap<String, String>> listOfMaps = new ArrayList<>();
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("key1", "value3");
        map1.put("key2", "value2");
        listOfMaps.add(map1);

        HashMap<String, String> map2 = new HashMap<>();
        map2.put("key3", "value1");
        map2.put("key4", "value4");
        listOfMaps.add(map2);

        HashMap<String, String> map3 = new HashMap<>();
        map3.put("key5", "value5");
        map3.put("key6", "value1");
        listOfMaps.add(map3);

        // 排序
        List<HashMap<String, String>> sortedList = sortByValue(listOfMaps);

        // 输出结果
        System.out.println(sortedList);
    }

    public static List<HashMap<String, String>> sortByValue(List<HashMap<String, String>> listOfMaps) {
        Collections.sort(listOfMaps, new Comparator<HashMap<String, String>>() {
            @Override
            public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
                // 获取第一个键的值进行比较
                Iterator<Map.Entry<String, String>> iterator1 = o1.entrySet().iterator();
                Map.Entry<String, String> entry1 = iterator1.hasNext() ? iterator1.next() : null;
                
                Iterator<Map.Entry<String, String>> iterator2 = o2.entrySet().iterator();
                Map.Entry<String, String> entry2 = iterator2.hasNext() ? iterator2.next() : null;
                
                // 比较两个value
                if (entry1 != null && entry2 != null) {
                    return entry1.getValue().compareTo(entry2.getValue());
                }
                return 0;
            }
        });

        return listOfMaps;
    }
}

上面是升序排序,如果想降序排序,写成entry2.getValue().compareTo(entry1.getValue());就行了

如果是jdk1.8及以上,使用流来处理更简洁

java 复制代码
public static List<HashMap<String, String>> sortByValue(List<HashMap<String, String>> listOfMaps) {
        return listOfMaps.stream()
                .sorted(Comparator.comparing(map -> {
                    // 获取第一个键的值进行比较
                    return map.values().iterator().next();
                }))
                .collect(Collectors.toList());
    }
相关推荐
dtq04241 小时前
C语言刷题数组5,6(求平均值,求最大值)
c语言·数据结构·算法
coolwaterld1 小时前
windows下删文件,找出“到底是谁占用
windows
洛水水2 小时前
【力扣100题】81.寻找两个正序数组的中位数
数据结构·算法·leetcode
Coder-magician2 小时前
《代码随想录》刷题打卡day15:二叉树part05
数据结构·c++·算法
Darling噜啦啦2 小时前
二叉树与递归算法实战:从树结构到 LeetCode 爬楼梯,一文吃透前端数据结构与递归思维
前端·javascript·数据结构
Irissgwe2 小时前
算法的时间复杂度和空间复杂度
数据结构·c++·算法·c·时间复杂度·空间复杂度
caimouse3 小时前
Reactos 第6章 进程间通信(续)
windows
触底反弹3 小时前
拷个 .exe 到新电脑就跑不起来?你缺的不是文件,是对链接的理解
c++·windows·操作系统
W优化大师3 小时前
Windows 更新待处理弹窗一直不消失怎么解决,C 盘空间和后台任务该如何排查
windows·系统优化·磁盘清理·windows11·c盘·系统更新
qq_297574674 小时前
设计模式系列文章(基础篇第22篇):访问者模式——分离数据结构与操作,实现灵活扩展
数据结构·设计模式·访问者模式