Java8:使用Lambda表达式增强版Comparator排序

java

学习路上的自我记录-------路好长,就问你慌不慌,大声港,不慌。----jstarseven。

实体类:

 1 package com.server.model;

2

3 /**

4 * Created by jstarseven on 2016/3/21.

5 */

6 public class Human {

7

8 private String name;

9 private int age;

10

11 @Override

12 public String toString() {

13 return "Human{" +

14 "name='" + name + '\'' +

15 ", age=" + age +

16 '}';

17 }

18

19 public Human() {

20 super();

21 }

22

23 public Human(final String name, final int age) {

24 super();

25 this.name = name;

26 this.age = age;

27 }

28

29 public String getName() {

30 return name;

31 }

32

33 public void setName(String name) {

34 this.name = name;

35 }

36

37 public int getAge() {

38 return age;

39 }

40

41 public void setAge(int age) {

42 this.age = age;

43 }

44

45 public static int compareByNameThenAge(Human lhs, Human rhs) {

46 if (lhs.getName().equals(rhs.getName())) {

47 return lhs.getAge() - rhs.getAge();

48 } else {

49 return lhs.getName().compareTo(rhs.getName());

50 }

51 }

52

53

54 }

View Code

测试类:

  1 package com.server.lambda;

2

3 import com.server.model.Human;

4 import org.junit.Test;

5

6 import java.util.ArrayList;

7 import java.util.Collections;

8 import java.util.Comparator;

9 import java.util.List;

10

11 /**

12 * Created by jstarseven on 2016/3/21.

13 */

14 public class TestLambda {

15

16 // 不使用Lambda表达式的基本排序

17 @Test

18 public void givenPreLambda_whenSortingEntitiesByName_thenCorrectlySorted() {

19

20 List<Human> humans = new ArrayList();

21 humans.add(new Human("A", 10));

22 humans.add(new Human("C", 14));

23 humans.add(new Human("C", 12));

24 humans.add(new Human("B", 12));

25 Collections.sort(humans, new Comparator<Human>() {

26 public int compare(Human o1, Human o2) {

27 return o1.getName().compareTo(o2.getName());

28 }

29 });

30 for (Human bean : humans) {

31 System.out.println(bean.toString());

32 }

33 }

34

35

36 // 使用Lambda表达式的基本排序

37 @Test

38 public void whenSortingEntitiesByName_thenCorrectlySorted() {

39 List<Human> humans = new ArrayList();

40 humans.add(new Human("A", 10));

41 humans.add(new Human("C", 14));

42 humans.add(new Human("C", 12));

43 humans.add(new Human("B", 12));

44 humans.sort((Human h1, Human h2) -> h1.getName().compareTo(h2.getName()));

45 for (Human bean : humans) {

46 System.out.println(bean.toString());

47 }

48 }

49

50

51 // 没有类型定义( Type Definitions)的基本排序

52 @Test

53 public void givenLambdaShortForm_whenSortingEntitiesByName_thenCorrectlySorted() {

54 List<Human> humans = new ArrayList();

55 humans.add(new Human("A", 10));

56 humans.add(new Human("C", 14));

57 humans.add(new Human("C", 12));

58 humans.add(new Human("B", 12));

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

60 for (Human bean : humans) {

61 System.out.println(bean.toString());

62 }

63 }

64

65 // 使用静态方法的引用来排序

66 @Test

67 public void givenMethodDefinition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {

68 List<Human> humans = new ArrayList();

69 humans.add(new Human("A", 10));

70 humans.add(new Human("C", 14));

71 humans.add(new Human("C", 12));

72 humans.add(new Human("B", 12));

73 humans.sort(Human::compareByNameThenAge);

74 for (Human bean : humans) {

75 System.out.println(bean.toString());

76 }

77 }

78

79 // 提取Comparator进行排序

80 @Test

81 public void givenInstanceMethod_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {

82 List<Human> humans = new ArrayList();

83 humans.add(new Human("A", 10));

84 humans.add(new Human("C", 14));

85 humans.add(new Human("C", 12));

86 humans.add(new Human("B", 12));

87 Collections.sort(humans, Comparator.comparing(Human::getName));

88 for (Human bean : humans) {

89 System.out.println(bean.toString());

90 }

91 }

92

93 // 反转排序

94 @Test

95 public void whenSortingEntitiesByNameReversed_thenCorrectlySorted() {

96 List<Human> humans = new ArrayList();

97 humans.add(new Human("A", 10));

98 humans.add(new Human("C", 14));

99 humans.add(new Human("C", 12));

100 humans.add(new Human("B", 12));

101 Comparator<Human> comparator = (h1, h2) -> h1.getName().compareTo(h2.getName());

102

103 humans.sort(comparator.reversed());

104 for (Human bean : humans) {

105 System.out.println(bean.toString());

106 }

107 }

108

109 // 多条件排序

110 @Test

111 public void whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {

112 List<Human> humans = new ArrayList();

113 humans.add(new Human("A", 10));

114 humans.add(new Human("C", 14));

115 humans.add(new Human("C", 12));

116 humans.add(new Human("B", 12));

117 humans.sort((lhs, rhs) -> {

118 if (lhs.getName().equals(rhs.getName())) {

119 return lhs.getAge() - rhs.getAge();

120 } else {

121 return lhs.getName().compareTo(rhs.getName());

122 }

123 });

124 for (Human bean : humans) {

125 System.out.println(bean.toString());

126 }

127 }

128

129 // 多条件组合排序

130 @Test

131 public void givenComposition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {

132 List<Human> humans = new ArrayList();

133 humans.add(new Human("A", 10));

134 humans.add(new Human("C", 14));

135 humans.add(new Human("C", 12));

136 humans.add(new Human("B", 12));

137 humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));

138 for (Human bean : humans) {

139 System.out.println(bean.toString());

140 }

141 }

142 }

View Code

  


 -END-

以上是 Java8:使用Lambda表达式增强版Comparator排序 的全部内容, 来源链接: utcz.com/z/391099.html

回到顶部