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

Java中的lambda和stream實(shí)現(xiàn)排序

 更新時(shí)間:2022年09月21日 11:21:10   作者:ldcaws  
這篇文章主要介紹了Java中的lambda和stream實(shí)現(xiàn)排序,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

前言

在日常開發(fā)中,對數(shù)據(jù)排序是非常常見的一種需求,一般通過如下兩種方式:

  • 存儲系統(tǒng):通過SQL、NoSQL的排序功能,查詢的結(jié)果是完成排序的結(jié)果;
  • 內(nèi)存:通過在內(nèi)存中進(jìn)行排序,查詢的結(jié)果是無序的結(jié)果;

下面聊聊通過Java中的lambda和stream實(shí)現(xiàn)在內(nèi)存中對數(shù)據(jù)進(jìn)行排序。

1、定義一個基礎(chǔ)類

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {

    private String name;
    private int 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 && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

2、使用Comparator排序

	@Test
    void test() {
        List<Student> students = Lists.newArrayList(
                new Student("caocao", 21),
                new Student("sunquan", 20)
        );

        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        Assertions.assertEquals(students.get(0), new Student("caocao", 21));
    }

在定義的Comparator中使用name字段排序,string類型的排序是通過ASCII碼順序進(jìn)行判斷。

3、使用lambda排序

	@Test
    void test() {
        List<Student> students = Lists.newArrayList(
                new Student("caocao", 21),
                new Student("sunquan", 20)
        );

        students.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));
        Assertions.assertEquals(students.get(0), new Student("caocao", 21));
    }

將內(nèi)部類Comparator替換為lambda表達(dá)式,使代碼更簡潔。

4、使用Comparator的comparing方法排序

	@Test
    void test() {
        List<Student> students = Lists.newArrayList(
                new Student("caocao", 21),
                new Student("sunquan", 20)
        );

        students.sort(Comparator.comparing(Student::getName));
        Assertions.assertEquals(students.get(0), new Student("caocao", 21));
    }

5、自定義比對方法

在Student類中自定義比對方法

	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);
        }
    }

先比對name,再比對age

	@Test
    void test() {
        List<Student> students = Lists.newArrayList(
                new Student("caocao", 21),
                new Student("sunquan", 20)
        );

        students.sort((o1,o2) -> Student.compareByNameThenAge(o1,o2));
        Assertions.assertEquals(students.get(0), new Student("caocao", 21));
    }

6、使用stream排序

在流式計(jì)算時(shí)進(jìn)行排序

	@Test
    void test() {
        List<Student> students = Lists.newArrayList(
                new Student("caocao", 21),
                new Student("sunquan", 20)
        );

        List<Student> result = students.stream().sorted(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        }).collect(Collectors.toList());
        
        Assertions.assertEquals(result.get(0), new Student("caocao", 21));
    }

7、null值判斷

若列表中元素是null或列表中元素參與排序的字段是null,會出現(xiàn)NullPointException異常,即 NPE,簡單演示一下:

	@Test
    void sortedNullGotNPE() {
        List<Student> students = Lists.newArrayList(
                null,
                new Student("liubei", 12)
        );
        students.sort(Comparator.comparing(Student::getName));
    }

修改為:

	@Test
    void sortedNullGotNPE() {
        List<Student> students = Lists.newArrayList(
                null,
                new Student("liubei", 12)
        );
        //students.sort(Comparator.comparing(Student::getName));

        Assertions.assertThrows(NullPointerException.class,
                () -> students.sort(Comparator.comparing(Student::getName)));
    }

到此這篇關(guān)于Java中的lambda和stream實(shí)現(xiàn)排序的文章就介紹到這了,更多相關(guān)Java lambda和stream 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

永善县| 万州区| 田东县| 萨嘎县| 冕宁县| 保定市| 波密县| 中牟县| 和林格尔县| 汉源县| 信丰县| 伽师县| 花莲市| 井冈山市| 九龙县| 凯里市| 谷城县| 青海省| 尼木县| 麦盖提县| 寻乌县| 阿坝县| 萨迦县| 莱芜市| 年辖:市辖区| 抚州市| 阿拉尔市| 合水县| 南召县| 佛教| 大理市| 福清市| 富源县| 新乡县| 威宁| 阜新市| 怀仁县| 濮阳市| 兖州市| 九龙坡区| 札达县|