@Data @AllArgsConstructor @NoArgsConstructor public class Human { private String name; private int age; }
下面的操做都基於這個類來進行操做。這裏面使用了Lombok類庫,它用註解的方式實現了基本的get和set等方法,讓代碼看起來更加的優雅。java
在Java8以前,對集合排序只能建立一個匿名內部類編程
new Comparator<Human>() { @Override public int compare(Human h1, Human h2) { return h1.getName().compareTo(h2.getName()); } }
下面是簡單的對Humans進行排序(按名稱正序)ide
@Test public void testSortByName_with_plain_java() throws Exception { ArrayList<Human> humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); Collections.sort(humans, new Comparator<Human>() { public int compare(Human h1, Human h2) { return h1.getName().compareTo(h2.getName()); } }); Assert.assertThat(humans.get(0), equalTo(new Human("li", 25))); }
使用JAVA8函數式方式的比較器函數式編程
(Human h1, Human h2) -> h1.getName().compareTo(h2.getName())
下面是使用JAVA8函數式的比較的例子函數
@Test public void testSortByName_with_lambda() throws Exception { ArrayList<Human> 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())
簡化後的比較器是這樣的:spa
@Test public void testSortByNameSimplify_with_lambda() throws Exception { ArrayList<Human> 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類增長一個靜態比較方法,以下:code
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使用這個引用排序
@Test public void testSort_with_givenMethodDefinition() throws Exception { ArrayList<Human> humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); humans.sort(Human::compareByNameThenAge); Assert.assertThat("tomy", is(equalTo(humans.get(1).getName()))); }
JAVA8已經提供了不少方便的比較器供咱們使用,好比Comparator.comparing方法,因此能夠使用Comparator.comparing方法來實現根據Human的name進行比較的操做:ip
@Test public void testSort_with_givenInstanceMethod() throws Exception { ArrayList<Human> 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中也提供了一個支持倒序排序的方法方便咱們更快的進行倒序
@Test public void testSort_with_comparatorReverse() throws Exception { ArrayList<Human> humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); Comparator<Human> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName()); humans.sort(comparator.reversed()); Assert.assertThat("tomy", equalTo(humans.get(0).getName())); }
Lambda提供了更復雜的表達式,還能夠先對name排序再根據age進行排序:
@Test public void testSort_with_multipleComparator() throws Exception { ArrayList<Human> humans = Lists.newArrayList( new Human("tomy", 22), new Human("li", 25) ); Comparator<Human> 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開始,咱們能夠使用鏈式操做進行復合操做來構建更復雜的邏輯:
@Test public void testSort_with_multipleComparator_composition() throws Exception { ArrayList<Human> 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))); }
JDK8真的是一個很是值得咱們學習的版本,它提供了Lambda表達式,帶來了函數式編程的理念,讓JAVA代碼更優雅。