# The data structure **Repository Path**: airfariy/the-data-structure ## Basic Information - **Project Name**: The data structure - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-06-24 - **Last Updated**: 2021-08-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 关于排序在java函数式编程中的应用 java8引入的函数式编程,使用Lambda的List排序 ```java @Test public void testSortByName_with_lambda() throws Exception { ArrayList humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); humans.sort((Human h1, Human h2) -> h1.getName().compareTo(h2.getName())); Assert.assertThat("tomy", equalTo(humans.get(1).getName())); } ``` 没有类型定义的排序 对于上面的表达式还可以进行简化,JAVA编译器可以根据上下文推测出排序的类型: (h1, h2) -> h1.getName().compareTo(h2.getName()) 简化后的比较器是这样的: ```java @Test public void testSortByNameSimplify_with_lambda() throws Exception { ArrayList humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); humans.sort((h1, h2) -> h1.getName().compareTo(h2.getName())); Assert.assertThat("tomy", equalTo(humans.get(1).getName())); } ``` 使用静态方法引用 JAVA8还可以提供使用Lambda表达式的静态类型引用,我们在Human类增加一个静态比较方法,如下: ```java public static int compareByNameThenAge(Human h1, Human h2) { if (h1.getName().equals(h2.getName())) { return Integer.compare(h1.getAge(), h2.getAge()); } return h1.getName().compareTo(h2.getName()); } ``` 然后就可以在humans.sort使用这个引用 ```java @Test public void testSort_with_givenMethodDefinition() throws Exception { ArrayList humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); humans.sort(Human::compareByNameThenAge); Assert.assertThat("tomy", is(equalTo(humans.get(1).getName()))); } ``` 使用单独的Comparator JAVA8已经提供了很多方便的比较器供我们使用,比如Comparator.comparing方法,所以可以使用Comparator.comparing方法来实现根据Human的name进行比较的操作: ```java @Test public void testSort_with_givenInstanceMethod() throws Exception { ArrayList humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); Collections.sort(humans, Comparator.comparing(Human::getName)); Assert.assertThat("tomy", equalTo(humans.get(1).getName())); } ``` 反序 JDK8中也提供了一个支持倒序排序的方法方便我们更快的进行倒序 ```java @Test public void testSort_with_comparatorReverse() throws Exception { ArrayList humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); Comparator comparator = (h1, h2) -> h1.getName().compareTo(h2.getName()); humans.sort(comparator.reversed()); Assert.assertThat("tomy", equalTo(humans.get(0).getName())); } ``` 使用多个条件进行排序 Lambda提供了更复杂的表达式,还可以先对name排序再根据age进行排序: ```java @Test public void testSort_with_multipleComparator() throws Exception { ArrayList humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); Comparator comparator = (h1, h2) -> {if (h1.getName().equals(h2.getName())) { return Integer.compare(h1.getAge(), h2.getAge()); } return h1.getName().compareTo(h2.getName()); }; humans.sort(comparator.reversed()); Assert.assertThat("tomy", equalTo(humans.get(0).getName())); } ``` 使用多个条件进行排序-组合的方式 Comparator对这种组合的排序有更优雅实现,从JDK8开始,我们可以使用链式操作进行复合操作来构建更复杂的逻辑: ```java @Test public void testSort_with_multipleComparator_composition() throws Exception { ArrayList humans = Lists.newArrayList( new Human("tomy", 22), new Human("tomy", 25) ); humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge)); Assert.assertThat(humans.get(0), equalTo(new Human("tomy", 22))); } ```