Java8??Lambda?????????Comparator??????
???????????? ???????[ 2015/3/9 14:30:02 ] ????????Java ??????? ???
????4???????????壨 Type Definitions???????????
???????????????????????????????????? ——?????????????????????ж??
????(h1?? h2) -> h1.getName().compareTo(h2.getName())
??????????????????
????@Test
????public void givenLambdaShortForm_whenSortingEntitiesByName_thenCorrectlySorted() {
????List<Human> humans = Lists.newArrayList(new Human("Sarah"?? 10)?? new Human("Jack"?? 12));
????humans.sort((h1?? h2) -> h1.getName().compareTo(h2.getName()));
????Assert.assertThat(humans.get(0)?? equalTo(new Human("Jack"?? 12)));
????}
????5????t??????????????????
???????????????????о???????????Lambda?????????????
??????????????????compareByNameThenAge???? ——????????????Comparator<Human>???????compareTo?????????????????
????public static int compareByNameThenAge(Human lhs?? Human rhs) {
????if (lhs.name.equals(rhs.name)) {
????return lhs.age - rhs.age;
????} else {
????return lhs.name.compareTo(rhs.name);
????}
????}
?????????????????????????????humans.sort??????
????humans.sort(Human::compareByNameThenAge);
???????????????t?????????Comparator????Ч?????????
????@Test
????public void givenMethodDefinition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
????List<Human> humans = Lists.newArrayList(new Human("Sarah"?? 10)?? new Human("Jack"?? 12));
????humans.sort(Human::compareByNameThenAge);
????Assert.assertThat(humans.get(0)?? equalTo(new Human("Jack"?? 12)));
????}
????6?????Comparator????????
?????????????????????????????ú?Comparator.comparing?????????????????——????????????????????????????Comparable??
??????????????getName() getter?????????Lambda?????????name???б????????
????@Test
????public void givenInstanceMethod_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
????List<Human> humans = Lists.newArrayList(new Human("Sarah"?? 10)?? new Human("Jack"?? 12));
????Collections.sort(humans?? Comparator.comparing(Human::getName));
????Assert.assertThat(humans.get(0)?? equalTo(new Human("Jack"?? 12)));
????}
????7?????????
????JDK 8?????????????????????????Comparator??reverse Comparator??——???????????????????????????????
????@Test
????public void whenSortingEntitiesByNameReversed_thenCorrectlySorted() {
????List<Human> humans = Lists.newArrayList(
????new Human("Sarah"?? 10)?? new Human("Jack"?? 12));
????Comparator<Human> comparator = (h1?? h2) -> h1.getName().compareTo(h2.getName());
????humans.sort(comparator.reversed());
????Assert.assertThat(humans.get(0)?? equalTo(new Human("Sarah"?? 10)));
????}
????8????????????
????????????Lambda????????????????????——????????????д?????????????????????name?????age??????????????
@Test
public void whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
List<Human> humans = Lists.newArrayList(
new Human("Sarah"?? 12)?? new Human("Sarah"?? 10)?? new Human("Zack"?? 12));
humans.sort((lhs?? rhs) -> {
if (lhs.getName().equals(rhs.getName())) {
return lhs.getAge() - rhs.getAge();
} else {
return lhs.getName().compareTo(rhs.getName());
}
});
Assert.assertThat(humans.get(0)?? equalTo(new Human("Sarah"?? 10)));
}
????9???????????????
??????????????——?????name?????????????age????????????Comparator?μ?????????????
??????JDK 8???????????????????Comparator???????chain together????????????????????
????@Test
????public void givenComposition_whenSortingEntitiesByNameThenAge_thenCorrectlySorted() {
????List<Human> humans = Lists.newArrayList(
????new Human("Sarah"?? 12)?? new Human("Sarah"?? 10)?? new Human("Zack"?? 12));
????humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));
????Assert.assertThat(humans.get(0)?? equalTo(new Human("Sarah"?? 10)));
????}
????10?????
??????????????????????????????????????Java 8 Lambda???????б????????——?????ù?????????????????????????塣
??????????Щ??????????????ζ??????????github?????????——???????????Eclipse???????????????ú??????????????С?
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Java???????????Щ???????????????Java????????????????Java?б???Map????????Java Web???????????????Java??????????????д?????Java????????7???????????????????????(java .net ?????)???Java??????????Python??????Java webdriver??λ????????′????е?????Java??д??????????????????Java???????????????JavaScript????????????Java?????????????????? Java???????10??????????????Java?м????????????????java???????ü???????????м???????????????????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????