简体   繁体   English

Java:对列表进行排序<Map<String, Object> &gt; dataList = 新的 ArrayList <Map<String, Object> &gt;(dataMap.values())

[英]Java: Sorting a List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>(dataMap.values())

I would like to ask how to sort this list of Map by it's values which is the Object itself我想问一下如何根据对象本身的值对这个 Map 列表进行排序

This is a code from my ServiceImpl :这是来自我的ServiceImpl的代码:

List<Map<String, Object>> dataList = serviceRequestDataDao.findServiceRequestData(serviceRequestQuery);

... ...

 Map<Long, Map<String, Object>> dataMap = toMap(dataList,customReportSetting.getReportType()); 

... ...

dataList = new ArrayList<Map<String,Object>>(dataMap.values());

The dataMap.values() are all the values that I have retrieved from the database. dataMap.values()是我从数据库中检索到的所有值。 I tried to display it using this code since I want it to be sorted by Creation Date Value (from oldest to latest ):我尝试使用此代码显示它,因为我希望它按创建日期值(从最旧到最新)排序:

for (Map<String, Object> mapList : dataList){
    for (Map.Entry<String, Object> entry : mapList.entrySet()){
        String key = entry.getKey();
        Object value = entry.getValue();

        if (key.equals("Creation Date")){
            System.out.println(value);
        }
    }       
}

I just don't know how to sort the WHOLE list containing map of string (keys) and objects (values) .我只是不知道如何排序包含字符串的映射(键)对象(值)整个列表。

The output of Creation Date is like this:创建日期输出是这样的:

05/17/2016 02:56:40 PM

遵循@Andreas 的假设,即Creation DateTimestamp

dataList.sort(Comparator.comparing(m -> (Timestamp)m.get("Creation Date")));

To sort a List of objects that don't have a natural order ( Map doesn't), you call Collections.sort(List<T> list, Comparator<? super T> c) .要对没有自然顺序的对象List进行排序( Map没有),您可以调用Collections.sort(List<T> list, Comparator<? super T> c)

Assuming your Creation Date value is a java.sql.Timestamp , this is how:假设您的Creation Date值是一个java.sql.Timestamp ,这是如何:

Collections.sort(dataList, new Comparator<Map<String, Object>>() {
    @Override
    public int compare(Map<String, Object> row1, Map<String, Object> row2) {
        Timestamp createDate1 = (Timestamp) row1.get("Creation Date");
        Timestamp createDate2 = (Timestamp) row2.get("Creation Date");
        return createDate1.compareTo(createDate2);
    }
});

Or this shorter lambda version if you're on Java 8+:如果您使用的是 Java 8+,或者这个较短的 lambda 版本:

Collections.sort(dataList, (row1, row2) -> {
    Timestamp createDate1 = (Timestamp) row1.get("Creation Date");
    Timestamp createDate2 = (Timestamp) row2.get("Creation Date");
    return createDate1.compareTo(createDate2);
});

Which of course can be shortened to one of these two (second taken from answer by @shmosel ):当然可以缩短为这两者之一(第二个来自@shmosel 的回答):

Collections.sort(dataList, (r1,r2) -> ((Timestamp)r1.get("Creation Date")).compareTo((Timestamp)r2.get("Creation Date")));

dataList.sort(Comparator.comparing(r -> (Timestamp)r.get("Creation Date")));

UPDATE更新

To add a secondary sort by Resolved Date , you'd first have to decide how null values sort.要按Resolved Date添加辅助排序,您首先必须确定null值的排序方式。 That was not a consideration for Creation Date , since that is a NOT NULL column, but Resolved Date is likely null capable.这不是Creation Date的考虑因素,因为它是一个NOT NULL列,但Resolved Date可能是 null 能力。 Let's sort null values last .让我们最后null值进行排序。

For pre-Java 8, do this:对于 Java 8 之前的版本,请执行以下操作:

Timestamp date1 = (Timestamp) row1.get("Creation Date");
Timestamp date2 = (Timestamp) row2.get("Creation Date");
int cmp = date1.compareTo(date2);
if (cmp == 0) {
    date1 = (Timestamp) row1.get("Resolved Date");
    date2 = (Timestamp) row2.get("Resolved Date");
    cmp = (date1 == null ? (date2 == null ? 0 : 1)
                         : (date2 == null ? -1 : date1.compareTo(date2)));
}
return cmp;

To sort null values first, swap 1 and -1 .要首先对null值进行排序,请交换1-1

For Java 8, do this:对于 Java 8,请执行以下操作:

dataList.sort(Comparator.comparing((Map<String, Object> r) -> (Timestamp)r.get("Creation Date"))
                        .thenComparing(r -> (Timestamp)r.get("Resolved Date"),
                                       Comparator.nullsLast(Comparator.naturalOrder())));

With method chaining, the compiler was unable to infer the type of the r parameter, so it has to be explicitly given on the first lambda.使用方法链,编译器无法推断r参数的类型,因此必须在第一个 lambda 上明确给出。

If I talk about list object which is given by you :如果我谈论您提供的列表对象:

List<Map<String, Object>> dataList = serviceRequestDataDao.findServiceRequestData(serviceRequestQuery);

We can sort it by using Java 8 feature stream API :我们可以使用 Java 8 功能流 API 对其进行排序:

dataList= dataList.stream().sorted(Comparator.comparing(map -> (Timestamp)map.get("Creation Date"))).collect(Collectors.toList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM