最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用Lambda表達(dá)式實(shí)現(xiàn)排序功能的實(shí)現(xiàn)代碼

 更新時(shí)間:2025年07月18日 09:51:33   作者:程序員小假  
在 Java 中使用 Lambda 表達(dá)式進(jìn)行排序是一種簡(jiǎn)潔且功能強(qiáng)大的方式,特別是在對(duì)集合中的對(duì)象進(jìn)行自定義排序時(shí),Lambda 表達(dá)式可以與 Collections.sort() 或 List.sort() 方法結(jié)合使用,本文給大家介紹了Java使用Lambda表達(dá)式實(shí)現(xiàn)排序功能的方法,需要的朋友可以參考下

基于Comparator排序

在 Java8 之前,都是通過(guò)實(shí)現(xiàn)Comparator接口完成排序,比如:

new Comparator<Student>() {
    @Override
    public int compare(Student h1, Student h2) {
        return h1.getName().compareTo(h2.getName());
    }
};

這里展示的是匿名內(nèi)部類的定義,如果是通用的對(duì)比邏輯,可以直接定義一個(gè)實(shí)現(xiàn)類。使用起來(lái)也比較簡(jiǎn)單,如下就是應(yīng)用:

@Test
void baseSortedOrigin() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    Collections.sort(students, new Comparator<Student>() {
        @Override
        public int compare(Student h1, Student h2) {
            return h1.getName().compareTo(h2.getName());
        }
    });
    Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}

這里使用了 Junit5 實(shí)現(xiàn)單元測(cè)試,用來(lái)驗(yàn)證邏輯非常適合。
因?yàn)槎x的Comparator是使用name字段排序,在 Java 中,String類型的排序是通過(guò)單字符的 ASCII 碼順序判斷的,J排在T的前面,所以Jerry排在第一個(gè)。 

使用 Lambda 表達(dá)式替換Comparator匿名內(nèi)部類

使用過(guò) Java8 的 Lamdba 的應(yīng)該知道,匿名內(nèi)部類可以簡(jiǎn)化為 Lambda 表達(dá)式為:

Collections.sort(students, (Student h1, Student h2) -> h1.getName().compareTo(h2.getName()));

在 Java8 中,List類中增加了sort方法,所以Collections.sort可以直接替換為:

students.sort((Student h1, Student h2) -> h1.getName().compareTo(h2.getName()));

根據(jù) Java8 中 Lambda 的類型推斷,可以將指定的Student類型簡(jiǎn)寫(xiě):

students.sort((h1, h2) -> h1.getName().compareTo(h2.getName()));

至此,整段排序邏輯可以簡(jiǎn)化為:

@Test
void baseSortedLambdaWithInferring() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    students.sort((h1, h2) -> h1.getName().compareTo(h2.getName()));
    Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}

通過(guò)靜態(tài)方法抽取公共的 Lambda 表達(dá)式

可以在Student中定義一個(gè)靜態(tài)方法:

public static int compareByNameThenAge(Student s1, Student s2) {
    if (s1.name.equals(s2.name)) {
        return Integer.compare(s1.age, s2.age);
    } else {
        return s1.name.compareTo(s2.name);
    }
}

這個(gè)方法需要返回一個(gè)int類型參數(shù),在 Java8 中,可以在 Lambda 中使用該方法:

@Test
void sortedUsingStaticMethod() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    students.sort(Student::compareByNameThenAge);
    Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}

借助Comparator的comparing方法

在 Java8 中,Comparator類新增了comparing方法,可以將傳遞的Function參數(shù)作為比較元素,比如:

@Test
void sortedUsingComparator() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    students.sort(Comparator.comparing(Student::getName));
    Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}

多條件排序

在靜態(tài)方法一節(jié)中展示了多條件排序,還可以在Comparator匿名內(nèi)部類中實(shí)現(xiàn)多條件邏輯:

@Test
void sortedMultiCondition() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12),
            new Student("Jerry", 13)
    );
    students.sort((s1, s2) -> {
        if (s1.getName().equals(s2.getName())) {
            return Integer.compare(s1.getAge(), s2.getAge());
        } else {
            return s1.getName().compareTo(s2.getName());
        }
    });
    Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}

從邏輯來(lái)看,多條件排序就是先判斷第一級(jí)條件,如果相等,再判斷第二級(jí)條件,依次類推。在 Java8 中可以使用comparing和一系列thenComparing表示多級(jí)條件判斷,上面的邏輯可以簡(jiǎn)化為:

@Test
void sortedMultiConditionUsingComparator() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12),
            new Student("Jerry", 13)
    );
    students.sort(Comparator.comparing(Student::getName).thenComparing(Student::getAge));
    Assertions.assertEquals(students.get(0), new Student("Jerry", 12));
}

這里的thenComparing方法是可以有多個(gè)的,用于表示多級(jí)條件判斷,這也是函數(shù)式編程的方便之處。 

在Stream中進(jìn)行排序

Java8 中,不但引入了 Lambda 表達(dá)式,還引入了一個(gè)全新的流式 API:Stream API,其中也有sorted方法用于流式計(jì)算時(shí)排序元素,可以傳入Comparator實(shí)現(xiàn)排序邏輯:

@Test
void streamSorted() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    final Comparator<Student> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
    final List<Student> sortedStudents = students.stream()
            .sorted(comparator)
            .collect(Collectors.toList());
    Assertions.assertEquals(sortedStudents.get(0), new Student("Jerry", 12));
}

同樣的,可以通過(guò) Lambda 簡(jiǎn)化書(shū)寫(xiě):

@Test
void streamSortedUsingComparator() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    final Comparator<Student> comparator = Comparator.comparing(Student::getName);
    final List<Student> sortedStudents = students.stream()
            .sorted(comparator)
            .collect(Collectors.toList());
    Assertions.assertEquals(sortedStudents.get(0), new Student("Jerry", 12));
}

倒序排列

調(diào)轉(zhuǎn)排序判斷

排序就是根據(jù)compareTo方法返回的值判斷順序,如果想要倒序排列,只要將返回值取返即可:

@Test
void sortedReverseUsingComparator2() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    final Comparator<Student> comparator = (h1, h2) -> h2.getName().compareTo(h1.getName());
    students.sort(comparator);
    Assertions.assertEquals(students.get(0), new Student("Tom", 10));
}

可以看到,正序排列的時(shí)候,是h1.getName().compareTo(h2.getName()),這里直接倒轉(zhuǎn)過(guò)來(lái),使用的是h2.getName().compareTo(h1.getName()),也就達(dá)到了取反的效果。在 Java 的Collections中定義了一個(gè)java.util.Collections.ReverseComparator內(nèi)部私有類,就是通過(guò)這種方式實(shí)現(xiàn)元素反轉(zhuǎn)。
借助**Comparator****reversed**方法倒序
在 Java8 中新增了reversed方法實(shí)現(xiàn)倒序排列,用起來(lái)也是很簡(jiǎn)單:

@Test
void sortedReverseUsingComparator() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    final Comparator<Student> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
    students.sort(comparator.reversed());
    Assertions.assertEquals(students.get(0), new Student("Tom", 10));
}

在Comparator.comparing中定義排序反轉(zhuǎn)

import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return name.hashCode() + age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class ComparatorTest {

    @Test
    void sortedUsingComparatorReverse() {
        final List<Student> students = Lists.newArrayList(
                new Student("Tom", 10),
                new Student("Jerry", 12)
        );

        // 使用Comparator.comparing的重載方法,傳入Comparator.reverseOrder()實(shí)現(xiàn)倒序
        students.sort(Comparator.comparing(Student::getName, Comparator.reverseOrder()));

        // 驗(yàn)證排序結(jié)果
        assertEquals(students.get(0), new Student("Jerry", 12));
    }
}

在Stream中定義排序反轉(zhuǎn)

Stream中的操作與直接列表排序類似,可以反轉(zhuǎn)Comparator定義,也可以使用Comparator.reverseOrder()反轉(zhuǎn)。實(shí)現(xiàn)如下:

@Test
void streamReverseSorted() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    final Comparator<Student> comparator = (h1, h2) -> h2.getName().compareTo(h1.getName());
    final List<Student> sortedStudents = students.stream()
            .sorted(comparator)
            .collect(Collectors.toList());
    Assertions.assertEquals(sortedStudents.get(0), new Student("Tom", 10));
}
@Test
void streamReverseSortedUsingComparator() {
    final List<Student> students = Lists.newArrayList(
            new Student("Tom", 10),
            new Student("Jerry", 12)
    );
    final List<Student> sortedStudents = students.stream()
            .sorted(Comparator.comparing(Student::getName, Comparator.reverseOrder()))
            .collect(Collectors.toList());
    Assertions.assertEquals(sortedStudents.get(0), new Student("Tom", 10));
}

null 值的判斷

前面的例子中都是有值元素排序,能夠覆蓋大部分場(chǎng)景,但有時(shí)候還是會(huì)碰到元素中存在null的情況:

  1. 列表中的元素是 null
  2. 列表中的元素參與排序條件的字段是 null

如果還是使用前面的那些實(shí)現(xiàn)會(huì)碰到NullPointException異常,即 NPE,簡(jiǎn)單演示一下:

@Test
void sortedNullGotNPE() {
    final List<Student> students = Lists.newArrayList(
            null,
            new Student("Snoopy", 12),
            null
    );
    Assertions.assertThrows(NullPointerException.class,
            () -> students.sort(Comparator.comparing(Student::getName)));
}

所以,需要考慮這些場(chǎng)景。 

元素是 null 的笨拙實(shí)現(xiàn)

最先想到的就是判空:

@Test
void sortedNullNoNPE() {
    final List<Student> students = Lists.newArrayList(
            null,
            new Student("Snoopy", 12),
            null
    );
    students.sort((s1, s2) -> {
        if (s1 == null) {
            return s2 == null ? 0 : 1;
        } else if (s2 == null) {
            return -1;
        }
        return s1.getName().compareTo(s2.getName());
    });
    Assertions.assertNotNull(students.get(0));
    Assertions.assertNull(students.get(1));
    Assertions.assertNull(students.get(2));
}

可以將判空的邏輯抽取出一個(gè)Comparator,通過(guò)組合方式實(shí)現(xiàn):

class NullComparator<T> implements Comparator<T> {
    private final Comparator<T> real;
    NullComparator(Comparator<? super T> real) {
        this.real = (Comparator<T>) real;
    }
    @Override
    public int compare(T a, T b) {
        if (a == null) {
            return (b == null) ? 0 : 1;
        } else if (b == null) {
            return -1;
        } else {
            return (real == null) ? 0 : real.compare(a, b);
        }
    }
}

在 Java8 中已經(jīng)準(zhǔn)備了這個(gè)實(shí)現(xiàn)。 

使用Comparator.nullsLast和Comparator.nullsFirst

使用Comparator.nullsLast實(shí)現(xiàn)null在結(jié)尾:

@Test
void sortedNullLast() {
    final List<Student> students = Lists.newArrayList(
            null,
            new Student("Snoopy", 12),
            null
    );
    students.sort(Comparator.nullsLast(Comparator.comparing(Student::getName)));
    Assertions.assertNotNull(students.get(0));
    Assertions.assertNull(students.get(1));
    Assertions.assertNull(students.get(2));
}

使用Comparator.nullsFirst實(shí)現(xiàn)null在開(kāi)頭:

@Test
void sortedNullFirst() {
    final List<Student> students = Lists.newArrayList(
            null,
            new Student("Snoopy", 12),
            null
    );
    students.sort(Comparator.nullsFirst(Comparator.comparing(Student::getName)));
    Assertions.assertNull(students.get(0));
    Assertions.assertNull(students.get(1));
    Assertions.assertNotNull(students.get(2));
}

是不是很簡(jiǎn)單,接下來(lái)看下如何實(shí)現(xiàn)排序條件的字段是 null 的邏輯。 

排序條件的字段是 null

這個(gè)就是借助Comparator的組合了,就像是套娃實(shí)現(xiàn)了,需要使用兩次Comparator.nullsLast,這里列出實(shí)現(xiàn):

@Test
void sortedNullFieldLast() {
    final List<Student> students = Lists.newArrayList(
            new Student(null, 10),
            new Student("Snoopy", 12),
            null
    );
    final Comparator<Student> nullsLast = Comparator.nullsLast(
            Comparator.nullsLast( // 1
                    Comparator.comparing(
                            Student::getName,
                            Comparator.nullsLast( // 2
                                    Comparator.naturalOrder() // 3
                            )
                    )
            )
    );
    students.sort(nullsLast);
    Assertions.assertEquals(students.get(0), new Student("Snoopy", 12));
    Assertions.assertEquals(students.get(1), new Student(null, 10));
    Assertions.assertNull(students.get(2));
}

代碼邏輯如下:

  1. 代碼 1 是第一層 null-safe 邏輯,用于判斷元素是否為 null;
  2. 代碼 2 是第二層 null-safe 邏輯,用于判斷元素的條件字段是否為 null;
  3. 代碼 3 是條件Comparator,這里使用了Comparator.naturalOrder(),是因?yàn)槭褂昧薙tring排序,也可以寫(xiě)為String::compareTo。如果是復(fù)雜判斷,可以定義一個(gè)更加復(fù)雜的Comparator,組合模式就是這么好用,一層不夠再套一層。

以上就是Java使用Lambda表達(dá)式實(shí)現(xiàn)排序功能的實(shí)現(xiàn)代碼的詳細(xì)內(nèi)容,更多關(guān)于Java Lambda排序功能的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

民勤县| 遂平县| 宣威市| 临邑县| 文山县| 宝兴县| 嘉祥县| 阿尔山市| 临朐县| 滦平县| 静乐县| 芜湖市| 聂拉木县| 勃利县| 察雅县| 延安市| 砀山县| 年辖:市辖区| 驻马店市| 二连浩特市| 新疆| 孝感市| 云安县| 清镇市| 宿州市| 宜章县| 永新县| 印江| 汶上县| 平利县| 抚松县| 全州县| 宁远县| 潮安县| 青铜峡市| 龙海市| 荣昌县| 石屏县| 宁城县| 托克逊县| 呼图壁县|