在lambda中使用comparable处理Java8集合排序方法中的空检查

8qgya5xd  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(298)

我有一份清单 Member 需要在java 8 collection.sort方法中为 Person 如果我有空值,请使用firstname。目前我有 Member 哪个实现 Comparable 接口以便 compareTo 其他成员的全名。我从timsort binarysort中得到一个nullpointerexception。似乎这是openjdk中的一个bug。找到了此问题中nullpointerexception的解决方案。以上问题中的任何建议解决方案,以及我在下面提到的内容。
主程序

import java.util.Collections;
import java.util.List;

public class SortMembersByFirstName {
    public static void main(String[] args) {
        List<Member> members = getMembers();
        List<Member> sorted = setMemberCacheOrderByFirstName(members);
        for (Member mem: sorted) {
            System.out.println(mem.toString());
        }
    }

    private static List<Member> getMembers() {
        Person tim = new Person("Tim", "Southee");
        Member member1 = new Member(tim);
        Person eoin = new Person("Eoin", "Morgan");
        Member member2 = new Member(eoin);
        Person moeenAli = new Person("Moeen", "Ali");
        Member member3 = new Member(moeenAli);
        Person timFirstNameNull = new Person(null, "Tim");
        Member member4 = new Member(timFirstNameNull);
        Person timFirstNameLastNameNull = new Person(null, null);
        Member member5 = new Member(timFirstNameLastNameNull);
        Person nullPerson = null;
        Member member6 = new Member(nullPerson);

        List<Member> members = new ArrayList<>();
        members.add(member1);
        members.add(member2);
        members.add(member3);
        members.add(member4);
        members.add(member5);
        members.add(member6);
        return members;
    }

    /**
     * Order list members by first name
     */
    private static List<Member> setMemberCacheOrderByFirstName(List<Member> members) {
        // TimSort.binarySort NPE
        Collections.sort(members, (m1, m2) ->
            m1.getPerson().getFirstName().compareTo(m2.getPerson().getFirstName())
        );
        return members;
    }
}

成员类

class Member implements Comparable<Member>{
    private Person person;
    public Member(Person person){
        this.person = person;
    }
    public Person getPerson(){
        return this.person;
    }
    public void setPerson(Person person){
        this.person = person;
    }
    public String getFullName(){
        return this.getPerson() != null ? this.getPerson().getFirstName() + " " + this.getPerson().getLastName() : "";
    }
    public int compareTo(Member o) {
        return this.getFullName().compareTo(o.getFullName());
    }

    @Override
    public String toString() {
        return "Member{" +
                "person=" + person +
                '}';
    }
}

人类

class Person{
    private String firstName;
    private String lastName;
    public String getFirstName(){
        return this.firstName;
    }
    public String getLastName(){
        return this.firstName;
    }
    public void setFirstName(String firstName){
        this.firstName = firstName;
    }

    @Override
    public String toString() {
        return "Person{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }

    public Person(String firstname, String lastname){
        this.firstName = firstname;
        this.lastName= lastname;
    }

}

一种解决方案是捕获抛出的nullpointerexeceptions。这样地

private static List<Member> setMemberCacheOrderByFirstName(List<Member> members) {
        Collections.sort(members, (m1, m2) -> {
            try {
                return m1.getPerson().getFirstName().compareTo(m2.getPerson().getFirstName());
            }catch (NullPointerException e){
                return 0;
            }
        });
        return members;
    }

或者手动检查是否存在不太好的空值。

private static List<Member> setMemberCacheOrderByFirstName(List<Member> members) {
        Collections.sort(members, (m1, m2) -> {
            // sort the list for firstnames
            if (m1.getPerson() != null && m2.getPerson() != null && m1.getPerson().getFirstName() != null
                    && m2.getPerson().getFirstName() != null) {
                return m1.getPerson().getFirstName().compareTo(m2.getPerson().getFirstName());
            } else {
                // Assuming any firstname is null
                if (m1.getPerson().getFirstName() == null || m2.getPerson().getFirstName() == null
                        && (m1.getPerson() != null && m2.getPerson() != null)) {
                    if (m1.getPerson().getFirstName() == null) {
                        m1.getPerson().setFirstName("");
                        return m1.getPerson().getFirstName().compareTo(m2.getPerson().getFirstName());
                    }
                    m2.getPerson().setFirstName("");
                    return m1.getPerson().getFirstName().compareTo(m2.getPerson().getFirstName());
                }
                // Assuming any member or person is null from schedulers
                return "".compareTo("");
            }
        });
        return members;
    }
vulvrdjw

vulvrdjw1#

使用 Comparator.nullsFirstComparator.nullsLast 这将是正确的选择。
如果您需要在 first . 你可以用这个

Collections.sort(members, (m1, m2) -> {
            if(m1.getPerson() == null || m2.getPerson() == null ||
                    m1.getPerson().getFirstName() == null || m2.getPerson().getFirstName() == null){
                return -1;
            }

            if(m1.getPerson() != null && m2.getPerson() != null &&
                    m1.getPerson().getFirstName() != null && m2.getPerson().getFirstName() != null){
                return m1.getPerson().getFirstName().compareTo(m2.getPerson().getFirstName());
            }
            return 0;
        });

如果您需要在 last . 你可以用这个

Collections.sort(members, (m1, m2) -> {
            if(m1.getPerson() == null && m2.getPerson() == null &&
                    m1.getPerson().getFirstName() == null && m2.getPerson().getFirstName() == null){
                return -1;
            }

            if(m1.getPerson() != null && m2.getPerson() != null &&
                    m1.getPerson().getFirstName() != null && m2.getPerson().getFirstName() != null){
                return m1.getPerson().getFirstName().compareTo(m2.getPerson().getFirstName());
            }
            return 0;
        });

优化的方法是。

Collections.sort(members, (m1, m2) -> {
            if (m1.getPerson() == null || m2.getPerson() == null ||
                    m1.getPerson().getFirstName() == null ||
                    m2.getPerson().getFirstName() == null) {
                return -1;
            } else {
                return m1.getPerson().getFirstName().compareTo(m2.getPerson().getFirstName());
            }
        });

相关问题