您的位置:首页 > 编程语言 > Java开发

记录JDK 1.8 新特性

2017-08-25 02:52 399 查看
package newFeatures8;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.IntSummaryStatistics;

public class LambdaExpress {

public static void main(String[] args) {

//Haha.main(args);

// 使用Java 8的方法引用更方便,方法引用由::双冒号操作符标示,
// 看起来像C++的作用域解析运算符
List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API");
features.forEach(System.out::println);
//stream():将list集合变成流的形式
//map(功能函数):对集合里的元素进行运算、操作
//forEach():遍历元素
List<Double> costBeforeTax = Arrays.asList(100.08, 200.0, 300.0, 400.0, 500.0);
costBeforeTax.stream().map((cost) -> cost + .12*cost).forEach(cost->{System.out.println(String.format("%.2f", cost));});
//reduce():返回唯一值:如单个求和
double bill = costBeforeTax.stream().map((cost) -> cost + .12*cost).reduce((sum, cost) -> sum + cost).get();
System.out.println("Total : " + String.format("%.2f", bill));

//filter():过滤器返回stream
//collect():通过流返回新的集合
// 创建一个字符串列表,每个字符串长度大于2
List<String> strList=Arrays.asList("addd","bd","c","dss");
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);

// 将字符串换成大写并用逗号链接起来
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);

//本例展示了如何利用流的 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);

/*IntStream、LongStream 和 DoubleStream 等流的类中,有个非常有用的方法叫做 summaryStatistics() 。
可以返回 IntSummaryStatistics、LongSummaryStatistics 或者 DoubleSummaryStatistics,
描述流中元素的各种摘要数据。在本例中,我们用这个方法来计算列表的最大值和最小值。
它也有 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();

//该类设计的巧妙之处就是
/*private int min = Integer.MAX_VALUE;把最大值赋给min
private int max = Integer.MIN_VALUE;把最小值赋给max*/

//IntSummaryStatistics 类默认会执行的方法
/* @Override
public void accept(int value) {
++count;
sum += value;
min = Math.min(min, value);
max = Math.max(max, value);
}*/

//重写了toString()方法
/* public String toString() {
return String.format(
"%s{count=%d, sum=%d, min=%d, average=%f, max=%d}",
this.getClass().getSimpleName(),
getCount(),
getSum(),
getMin(),
getAverage(),
getMax());
}*/
System.out.println(stats.toString());
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());
}
}

class Haha{
public interface BB{
//1.8 开始 接口可以有默认的实现方法
default  void  aa(){
System.out.println("hah");
}
//1.8 开始 接口可以有静态的实现方法
static  void  bb(){
System.out.println("hah");
}
}
public static void main(String args[]){
List<String> languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp");

System.out.println("Languages which starts with J :");
filter(languages, (String str)->str.startsWith("J"));

System.out.println("Languages which ends with a ");
filter(languages, (String 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, (String str)->str.length() > 4);
}

/*public static void filter(List<String> names, Predicate<String> condition) {
for(String name: names)  {
if(condition.test(name)) {
System.out.println(name + " ");
}
}
}*/

//Predicate:断言类 :类似于junit
//Predicate<String> condition=(String str)->str.length() > 4;
// 更好的办法
public static void filter(List<String> names, Predicate<String> condition) {
names.stream().filter((name) -> (condition.test(name))).forEach((name) -> {
System.out.println(name + " ");
});

}
}


package newFeatures8;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class Express {

public static void main(String[] args) {
List<Double> costBeforeTax = Arrays.asList(100.08, 200.0, 300.0, 400.0, 500.0);

/*默认的实现行为如下:
//(s)->System.out.println(s);
for (T t : this)
action.accept(t);*/
//在编译器内部 Lambda 表达式被编译成 私有的静态函数 命令行javap -p
// private static void lambda$0(java.lang.Double);
costBeforeTax.forEach((s)->System.out.println(s));

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jdk1.8 新特性