Home > Java > javaTutorial > What is the way to sort using streams and custom comparators in Java 8?

What is the way to sort using streams and custom comparators in Java 8?

王林
Release: 2023-05-07 21:46:06
forward
1652 people have browsed it

java8 stream sorting and custom comparator

Use java 8 stream sorting LIst and custom object comparator

The main comparison is time, but the time is the latest time, compareto in java The ACSII code of the method comparison, so 2020 and 2021 must be 0 in front, but what we need is the most recent date in front, so we invert the returned result. Normally, the edittime of object 1 is compared with the time of object 2. If the time of object 1 is less than the time of object 2, 1 will be returned. So we will find that 2020 is in front. We only need to invert, because there are only three results of compareto, one is 1 in ascending order, -1 in descending order, and 0 is equal. means (String class implements the Comparable interface.

CompareTo method returns 0 for equality, 1 for ascending order, and -1 for descending order). Can you understand why it is inverted here?

Because after inversion, the 2021-09-06 can be in front. You can sort by most recent time.

如果有的java 8 stream 不懂的可以去看看哦

List<Map<String,Object>> resultList = null;

        Map<String,Object> ss1 = new HashMap<>();
        ss1.put("dictcode","2");
        ss1.put("DictValue","霓虹灯广告1");
        ss1.put("EditTime","2021-09-16 17:47:30");
        Map<String,Object> ss2 = new HashMap<>();
        ss2.put("dictcode","1");
        ss2.put("DictValue","霓虹灯广告2");
        ss2.put("EditTime","2020-04-16 17:47:30");
        Map<String,Object> ss3 = new HashMap<>();
        ss3.put("dictcode","21");
        ss3.put("DictValue","霓虹灯广告3");
        ss3.put("EditTime","2020-09-13 17:47:30");
        Map<String,Object> ss4 = new HashMap<>();
        ss4.put("dictcode","3");
        ss4.put("DictValue","霓虹灯广告4");
        ss4.put("EditTime","2020-09-16 17:47:30");
        Map<String,Object> ss5 = new HashMap<>();
        ss5.put("dictcode","4");
        ss5.put("DictValue","霓虹灯广告5");
        ss5.put("EditTime","2020-09-16 17:47:30");

        List<Map<String, Object>> maps = Arrays.asList(ss1, ss2, ss3, ss5, ss4);

        //默认按edittime排序,如果时间相同或者为空,则按照code排序
        maps.stream().sorted((o1, o2) -> {
            if (StringUtils.isEmpty(o1.get("EditTime").toString()) || StringUtils.isEmpty(o2.get("EditTime").toString())) {
                return o1.get("dictcode").toString().compareTo(o2.get("dictcode").toString());
            } else if (o1.get("EditTime").equals(o2.get("EditTime"))) {
                return o1.get("dictcode").toString().compareTo(o2.get("dictcode").toString());
            } else
                if (o1.get("EditTime").toString().compareTo(o2.get("EditTime").toString()) > 0)
                    return -1;
                 else return 1;
            /*if (StringUtils.isEmpty(o1.get("EditTime").toString())||StringUtils.isEmpty(o2.get("EditTime").toString())
            || o1.get("EditTime").equals(o2.get("EditTime"))) {
                return o1.get("dictcode").toString().compareTo(o2.get("dictcode").toString());
            }
            else {
               if (o1.get("EditTime").toString().compareTo(o2.get("EditTime").toString()) >0)
                   return -1;
               return 1;
            }*/
        }).collect(Collectors.toList()).forEach(System.out::println);
Copy after login

java stream sorting problem

When we process data, we often need to sort it and then return it to the front-end call. For example, sorting in ascending order of time, the front-end display data is sorted by time. .

Here you can use the sorted() method of stream to perform customized sorting

Let’s talk about the usage

The example data here is a list collection, Listlist, There are many attributes in the Data entity class, including the time field and month. When we sort using the stream().sorted() method, because the elements are class reference types, we need to customize a Comparator to sort in ascending order by month. Sort.

The code is as follows:

list.stream().sorted(Comparator.comparing(o->new Integer(o.getMonth()))).collect(Collectors.toList());
Copy after login

Note:

The parameters in the comparator here use Lambda expression, new Integer(o.getMonth()), which is encapsulated into Integer The type is because the month attribute in the Data entity class we designed is of String type, not Integer. The conversion to Integer is for subsequent month traversal judgment. If the attribute is Integer Month, the method reference of the class can be directly defined in the comparator.

is as follows:

list.stream().sorted(Comparator.comparing(Data::getMonth).collect(Collectors.toList());
Copy after login

The above is the detailed content of What is the way to sort using streams and custom comparators in Java 8?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template