Java8 lambda????10?????
???????????? ???????[ 2015/8/3 11:11:05 ] ???????????????
??????4?????lambda?????????????Predicate
?????????????????????????????Java 8????????????????? java.util.function?????????????????????Java?????????????????????Predicate????? java.util.function.Predicate ???????????lambda????????????API?????????????????????????????????????????Java 8 Predicate ???????????????????????????÷?????Predicate??????????????????
public static void main(args[]){
List languages = Arrays.asList("Java"?? "Scala"?? "C++"?? "Haskell"?? "Lisp");
System.out.println("Languages which starts with J :");
filter(languages?? (str)->str.startsWith("J"));
System.out.println("Languages which ends with a ");
filter(languages?? (str)->str.endsWith("a"));
System.out.println("Print all languages :");
filter(languages?? (str)->true);
System.out.println("Print no language : ");
filter(languages?? (str)->false);
System.out.println("Print language whose length greater than 4:");
filter(languages?? (str)->str.length() > 4);
}
public static void filter(List names?? Predicate condition) {
for(String name: names) {
if(condition.test(name)) {
System.out.println(name + " ");
}
}
}
?????????
????Languages which starts with J :
????Java
????Languages which ends with a
????Java
????Scala
????Print all languages :
????Java
????Scala
????C++
????Haskell
????Lisp
????Print no language :
????Print language whose length greater than 4:
????Scala
????Haskell
????// ??????
????public static void filter(List names?? Predicate condition) {
????names.stream().filter((name) -> (condition.test(name))).forEach((name) -> {
????System.out.println(name + " ");
????});
????}
?????????????Stream API????????????????Predicate??????ζ?????????????? filter() ?????滻??д???????????????????lambda???????????????Predicate??????????ж????????????????????????????
??????5???????lambda?????м???Predicate
????????????????java.util.function.Predicate ??????????????? Predicate ???????????????????????????AND??OR??????????????and()??or()??xor()??????????? filter() ??????????????????????磬??????????J???????????????????????????????????????? Predicate ??????????????????????? Predicate.and() ???????????????????????????
????// ??????????and()??or()??xor()????????????Predicate??
????// ??????????????J??????????????????????????????????Predicate??????
????Predicate<String> startsWithJ = (n) -> n.startsWith("J");
????Predicate<String> fourLetterLong = (n) -> n.length() == 4;
????names.stream()
????.filter(startsWithJ.and(fourLetterLong))
????.forEach((n) -> System.out.print("nName?? which starts with 'J' and four letter long is : " + n));
?????????????????? or() ?? xor() ?????????????????????????????????? Predicate ????????????????????????á??????????????????Java???????? Predicate ???????????????lambda???????°?????Ч????
??????6??Java 8?????lambda??????Map??Reduce???
?????????????????????????????map????????????????????????????????У?????? costBeforeTax ?б?????????????????????????? x -> x*x lambda???????? map() ????????????????????е?????????????? forEach() ???б?????????????????API??????????????????к??????????? toList() ??????????? map ???κ???????????????????????????????????????????????????????????????????????????????API?? reduce() ?????????????????????????????????????
????// ?????lambda????????????????12%???
????List costBeforeTax = Arrays.asList(100?? 200?? 300?? 400?? 500);
????for (Integer cost : costBeforeTax) {
????double price = cost + .12*cost;
????System.out.println(price);
????}
????// ???lambda????
????List costBeforeTax = Arrays.asList(100?? 200?? 300?? 400?? 500);
????costBeforeTax.stream().map((cost) -> cost + .12*cost).forEach(System.out::println);
?????????
????112.0
????224.0
????336.0
????448.0
????560.0
????112.0
????224.0
????336.0
????448.0
????560.0
??????6.2??Java 8?????lambda??????Map??Reduce???
??????????????У????????map?????????????б????????????????????? reduce() ????????????????????????Map??Reduce??????????????????????????書(shū)???reduce ???????????????????reduce ??????????μ?????????п???????????????SQL?????? sum()??avg() ???? count() ????????????????? reduce ?????????????????????????????????API????? reduceh() ???????????lambda??????????????????к????IntStream?????????????? average()??count()??sum() ????????????? reduce ?????????mapToLong()??mapToDouble() ????????????????????????????????????????????????????塣?????Java 8??Map Reduce?????????????????м????? 12% ??VAT??????? reduce() ????????????
????// ????????????12%???
????// ???????
????List costBeforeTax = Arrays.asList(100?? 200?? 300?? 400?? 500);
????double total = 0;
????for (Integer cost : costBeforeTax) {
????double price = cost + .12*cost;
????total = total + price;
????}
????System.out.println("Total : " + total);
????// ?·?????
????List costBeforeTax = Arrays.asList(100?? 200?? 300?? 400?? 500);
????double bill = costBeforeTax.stream().map((cost) -> cost + .12*cost).reduce((sum?? cost) -> sum + cost).get();
????System.out.println("Total : " + bill);
?????????
????Total : 1680.0
????Total : 1680.0
??????7???????????????String?б?
??????????Java????????????????????????ò??????????????lambda????????API???????????????????????????????? filter() ????????????? Predicate ????????????????lambda?????????????????????????????lambda????????Java??????????????
????// ?????????????б?????????????????2
????List<String> filtered = strList.stream().filter(x -> x.length()> 2).collect(Collectors.toList());
????System.out.printf("Original List : %s?? filtered list : %s %n"?? strList?? filtered);
?????????
????Original List : [abc?? ?? bcd?? ?? defg?? jk]?? filtered list : [abc?? bcd?? defg]
???????????? filter() ?????и??????????????????У????????????????????????????filter()??????????????μ??б?????????????????????
??????8?????б??????????ú???
?????????????????б?????????????????????????????????????????????????????????????????Щ????????????? map() ???????????????????lambda????????????? map() ???????????????????????????????????????
????// ????????????д???????????????
????List<String> G7 = Arrays.asList("USA"?? "Japan"?? "France"?? "Germany"?? "Italy"?? "U.K."??"Canada");
????String G7Countries = G7.stream().map(x -> x.toUpperCase()).collect(Collectors.joining("?? "));
????System.out.println(G7Countries);
?????????
????USA?? JAPAN?? FRANCE?? GERMANY?? ITALY?? U.K.?? CANADA
??????9???????????????????????б?
??????????????????????? distinct() ??????????????????
????// ?????в?????????????????????б?
????List<Integer> numbers = Arrays.asList(9?? 10?? 3?? 4?? 7?? 3?? 4);
????List<Integer> distinct = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
????System.out.printf("Original List : %s?? Square Without duplicates : %s %n"?? numbers?? distinct);
?????????
????Original List : [9?? 10?? 3?? 4?? 7?? 3?? 4]?? Square Without duplicates : [81?? 100?? 9?? 16?? 49]
??????10????????????????С?????????????
????IntStream??LongStream ?? DoubleStream ?????????У??и???????????????? summaryStatistics() ????????? IntSummaryStatistics??LongSummaryStatistics ???? DoubleSummaryStatistic s??????????????????????????????У?????????????????????б??????С???????? getSum() ?? getAverage() ??????????б????????????????????
????//?????????????С??????????????????
????List<Integer> primes = Arrays.asList(2?? 3?? 5?? 7?? 11?? 13?? 17?? 19?? 23?? 29);
????IntSummaryStatistics stats = primes.stream().mapToInt((x) -> x).summaryStatistics();
????System.out.println("Highest prime number in List : " + stats.getMax());
????System.out.println("Lowest prime number in List : " + stats.getMin());
????System.out.println("Sum of all prime numbers : " + stats.getSum());
????System.out.println("Average of all prime numbers : " + stats.getAverage());
?????????
????Highest prime number in List : 29
????Lowest prime number in List : 2
????Sum of all prime numbers : 129
????Average of all prime numbers : 12.9
????Lambda???? vs ??????
???????lambda??????????????Java?????е??????????????б?????????????????????????????????????? this????????? this ????????????????lambda?????? this ?????????Χlambda??????????????????????????????Java????????lambda??????????????з??????????Java 7?? invokedynamic ????????????????????????
????Java 8 Lambda???????
????10??Java lambda????????API???
???????????????????Java 8??10??lambda?????????????????????????????????????????????????????????????????????????????????????????????????????????????????Netbeans IDE?????lambda??????????Java 8??????á??????????????????????Netbeans??????????????????Netbeans???????????????????????????lambda?????????????????????????????????Java 8??lambdas????ú??????????饗Java 8 Lambdas?? pragmatic functional programming??????????Richard Warburton??????????????Manning??Java 8????Java 8 in Action?????????????????棬??????????е???μ????pdf?????????????????????????????????Java 8??lambda?????????????????????????????
????1??lambda??????????????′????????????? @Functional ???????????????????????????????????SAM??Single Abstract Method ???????????????????Щ???lambda?????????????????????????????????lambda???????????????磬?????????????Runnable??Comparable???? Callable ???????е??????????????????lambda???????????????????????????????? java.util.function ???????????? Predicate??Function??Consumer ?? Supplier?????????????lambda??????
????2??lambda???????????÷?????????????÷????????lambda??????????????????е?lambda???????????????????????????????????????????????á?
????list.forEach(n -> System.out.println(n));
????list.forEach(System.out::println); // ??÷???????
??????????????????κ???????????÷?????????????????????lambda???????????????
????list.forEach((String s) -> System.out.println("*" + s + "*"));
??????????????????????lambda????????????????????????????б????????????????
????3??lambda?????????t??????????????????????lambda??????????
????4??Lambda??????Java????????????????????????????????°????б??????????t????
????5??Lambda???????????????????????з?????????? invokedynamic ?????????????е??á????????JDK?е? javap ????????????class???????? javap -p ?? javap -c -v ???????????lambda????????????????????ó???????
????private static java.lang.Object lambda$0(java.lang.String);
????6??lambda?????и????????????????? final ?? final ????????????????????lambda?????????????????????
????List<Integer> primes = Arrays.asList(new Integer[]{2?? 3??5??7});
????int factor = 2;
????primes.forEach(element -> { factor++; });
????Compile time error : "local variables referenced from a lambda expression must be final or effectively final"
?????????????????????????????????????????
????List<Integer> primes = Arrays.asList(new Integer[]{2?? 3??5??7});
????int factor = 2;
????primes.forEach(element -> { System.out.println(factor*element); });
?????????
?????????????????????????????????Python??
??????????Java 8??lambda?????????10?????????????????Java???????Σ?????????δ??Java????????ü?????????????????????????????Java 5???????????????????????????????????????磺???????????????Autoboxing???????????????API??????????????????????Java?????????????????lambda??????????????????????????????????е?????????????????????????????д??
???????????????????????漰???????????????????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 ??????