1.3 java8新特性总结

java8中重要的4个新特性:java

  • Lambda
  • Stream
  • Optional
  • 日期时间API
  • 接口方法(default和static方法,jdk9可定义private方法)

1、Lambdaapp

 1 import java.util.Comparator;
 2 import java.util.function.Consumer;
 3 
 4 /**
 5  * @author zhaojigang
 6  * @date 2018/5/19
 7  */
 8 public class LambdaTest {
 9     /**
10      * Lambda 是一个匿名函数。
11      * 一、语法
12      * Lambda表达式引入了操做符为"->",该操做符将Lambda分为两个部分:
13      * 左侧:指定了Lambda表达式须要的全部参数
14      * 右侧:指定了Lambda体,即Lambda表达式要执行的功能。
15      *
16      * 二、示例
17      */
18     public void testLambda() {
19         /**
20          * 语法格式一:无参,无返回值
21          */
22         Runnable task = () -> System.out.println("hello lambda");
23         /**
24          * 语法格式二:一个参数,无返回值
25          * 注意:参数类型能够经过
26          */
27         Consumer<String> consumer = str -> System.out.println(str);
28         /**
29          * 语法格式三:一个参数,有返回值
30          * 注意:当Lambda体只有一条语句时,省略大括号和return
31          */
32         Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);
33     }
34 
35     /**
36      * 函数式接口:只包含一个抽象方法的接口。
37      * 一、能够经过Lambda表达式来建立该接口的对象
38      * 二、能够在任意函数式接口上使用@FunctionalInterface注解,这样作能够检查它是不是一个函数式接口
39      *
40      * Java内置了四大核心函数式接口:
41      * 一、Consumer<T>:void accept(T t),消费型接口
42      * 二、Supplier<T>:T get(),供给型接口
43      * 三、Function<T, R>:R apply(T t),函数型接口
44      * 四、Predicate<T>:boolean test(T t),断言型接口
45      * 还有部分子接口。
46      */
47     public void testFunctionalInterface() {
48         Consumer<String> consumer = str -> System.out.println(str);
49     }
50 }

 

2、Streamide

  1 import java.util.ArrayList;
  2 import java.util.HashMap;
  3 import java.util.List;
  4 import java.util.Map;
  5 import java.util.Optional;
  6 import java.util.OptionalLong;
  7 import java.util.stream.LongStream;
  8 
  9 /**
 10  * @author zhaojigang
 11  * @date 2018/5/19
 12  */
 13 public class StreamTest {
 14 
 15     static List<Integer> integerList = new ArrayList<Integer>() {{
 16         add(1);
 17         add(2);
 18         add(3);
 19         add(4);
 20         add(5);
 21         add(5);
 22         add(5);
 23     }};
 24 
 25     static List<Integer> integerList2 = new ArrayList<Integer>() {{
 26         add(10);
 27         add(20);
 28         add(30);
 29     }};
 30 
 31     static Map<String, List<Integer>> map1 = new HashMap<>();
 32 
 33     static {
 34         map1.put("list1", integerList);
 35         map1.put("list2", integerList2);
 36     }
 37 
 38     /**
 39      * 分片与筛选
 40      */
 41     public static void test1() {
 42         integerList.stream()
 43                 .filter(x -> x > 2) // 3,4,5,5,5
 44                 .skip(2) //5,5,5
 45                 .limit(2) //5,5 短路:一旦获取到2个元素后再也不向后迭代
 46                 .distinct() //5
 47                 .forEach(System.out::println);
 48     }
 49 
 50     /**
 51      * 映射
 52      * map(Function f):接收一个函数做为参数,该函数会被应用到每一个元 素上,并将其映射成一个新的元素
 53      * flatMap(Function f):接收一个函数做为参数,将流中的每一个值都换成另外一个流,而后把全部流链接成一个流
 54      */
 55     public static void test2() {
 56         integerList.stream()
 57                 .map(x -> x + 10)
 58                 .forEach(System.out::println);
 59 
 60         map1.values().stream()
 61                 .flatMap(x -> x.stream()) // x是每个List,flatMap将每个List的Stream合并起来
 62                 .forEach(System.out::println);
 63 
 64     }
 65 
 66     /**
 67      * 排序
 68      * sorted():产生一个新流,其中按天然顺序排序(按照元素的Comparable接口)
 69      * sorted(Comparator comp):产生一个新流,其中按比较器顺序排序(按照自定义的Comparator)
 70      */
 71     public static void test3() {
 72         integerList.stream()
 73                 .sorted()
 74                 .forEach(System.out::println);
 75 
 76         integerList.stream()
 77                 .sorted((x, y) -> {
 78                     if (x < y) {
 79                         return 1;
 80                     } else {
 81                         return -1;
 82                     }
 83                 })
 84                 .forEach(System.out::println);
 85 
 86     }
 87 
 88     /**
 89      * 查找与匹配
 90      * allMatch(Predicate p):检查是否匹配全部元素
 91      * anyMatch(Predicate p):检查是否至少匹配一个元素
 92      * noneMatch(Predicate p):检查是否没有匹配全部元素
 93      * findFirst():返回第一个元素
 94      * findAny():返回当前流中的任意元素
 95      * count():返回流中元素总数
 96      * max(Comparator c):返回流中最大值
 97      * min(Comparator c):返回流中最小值
 98      */
 99     public static void test4() {
100         final boolean allMatch = integerList.stream().allMatch(x -> x > 4);
101         final boolean anyMatch = integerList.stream().anyMatch(x -> x > 4);
102         final boolean noneMatch = integerList.stream().noneMatch(x -> x > 4);
103         final Optional<Integer> first = integerList.stream().filter(x -> x > 3).findFirst();
104         final Optional<Integer> any = integerList.stream().filter(x -> x > 3).findAny();
105         final long count = integerList.stream().filter(x -> x > 4).count();
106         final Optional<Integer> max = integerList.stream()
107                 .max((x, y) -> {
108                     if (x < y) {
109                         return 1;
110                     } else {
111                         return -1;
112                     }
113                 });
114 
115     }
116 
117     /**
118      * 规约
119      * <p>
120      * reduce(T iden, BinaryOperator b):能够将流中元素反复结合起来,获得一个值。返回T,其中iden是初始值
121      * reduce(BinaryOperator b):能够将流中元素反复结合起来,获得一个值。返回Optional<T>
122      */
123     public static void test5() {
124         // 计算:100+1+2+3+4+5+5+5
125         final Integer sum = integerList.stream().reduce(100, (x, y) -> x + y);
126         final Optional<Integer> sumOptional = integerList.stream().reduce((x, y) -> x + y);
127     }
128 
129     /**
130      * 收集
131      * 经常使用:
132      * 一、将流元素收集到List:List<Employee> emps= list.stream().collect(Collectors.toList());
133      * 二、将流元素收集到Set:List<Employee> emps= list.stream().collect(Collectors.toSet());
134      * 三、链接流中每一个字符串:String str= list.stream().map(Employee::getName).collect(Collectors.joining());
135      * 四、分组: Map<Emp.Status, List<Emp>> map= list.stream().collect(Collectors.groupingBy(Employee::getStatus));
136      *
137      * 不经常使用:
138      * 一、根据true或false进行分区:Map<Boolean,List<Emp>>vd= list.stream().collect(Collectors.partitioningBy(Employee::getManage));
139      * 二、根据比较器选择最大值:Optional<Emp>max= list.stream().collect(Collectors.maxBy(comparingInt(Employee::getSalary)));
140      * 三、根据比较器选择最小值:Optional<Emp> min = list.stream().collect(Collectors.minBy(comparingInt(Employee::getSalary)));
141      * 四、将流元素收集到任意指定集合:Collection<Employee> emps=list.stream().collect(Collectors.toCollection(ArrayList::new));
142      * 五、计算流中元素的个数:long count = list.stream().collect(Collectors.counting());
143      * 六、对流中元素的属性求和:int total=list.stream().collect(Collectors.summingInt(Employee::getSalary));
144      * 七、计算流中元素Integer属性的平均值:double avg= list.stream().collect(Collectors.averagingInt(Employee::getSalary));
145      */
146     public static void test6() {
147     }
148 
149     /**
150      * 并行流与串行流
151      * 并行流就是把一个内容分红多个数据块,并用不一样的线程分别处理每一个数据块的流。
152      * 底层:fork/join
153      * Stream API能够声明性地经过parallel()与sequential()在并行流与顺序流之间进行切换
154      */
155     public static void test7(){
156         long start = System.currentTimeMillis();
157 
158         final OptionalLong sum = LongStream.rangeClosed(0, 100000000000L)
159                 .parallel()
160                 .reduce(Long::sum);
161 
162         System.out.println(sum + "-" + (System.currentTimeMillis() - start));
163     }
164 
165     public static void main(String[] args) {
166         test7();
167     }
168 }

 

3、Optional函数

 1 import java.util.Optional;
 2 
 3 /**
 4  * @author zhaojigang
 5  * @date 2018/5/19
 6  */
 7 public class OptionalTest {
 8 
 9     public static void main(String[] args) {
10 //        String godName = "shijia";
11         String godName = null;
12         // 经常使用方式
13         final String god = Optional.ofNullable(godName).orElse("yesu");
14         System.out.println(god);
15     }
16 }

 

4、日期时间APIspa

 1 import java.time.Duration;
 2 import java.time.Instant;
 3 import java.time.LocalDateTime;
 4 import java.time.format.DateTimeFormatter;
 5 
 6 /**
 7  * @author zhaojigang
 8  * @date 2018/5/19
 9  */
10 public class TimeTest {
11 
12     /**
13      * 1、日期建立:
14      * LocalDate localDate = LocalDate.now();
15      * LocalTime localTime = LocalTime.now();
16      * LocalDateTime localDateTime = LocalDateTime.now();
17      *
18      * LocalDate localDate = LocalDate.of(2016, 10, 26);
19      * LocalTime localTime = LocalTime.of(02, 22, 56);
20      * LocalDateTime localDateTime = LocalDateTime.of(2016, 10, 26, 12, 10, 55);
21      *
22      * 2、日期加减运算
23      * plusDays, plusWeeks, plusMonths, plusYears
24      * minusDays, minusWeeks, minusMonths, minusYears
25      * plus, minus
26      *
27      * 3、日期比较计算
28      * isBefore, isAfter
29      *
30      * 4、是否闰年
31      * isLeapYear
32      *
33      * 5、时间戳与时间间隔运算
34      * Instant 时间戳:以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算
35      * Duration:用于计算两个“时间”间隔
36      * Period:用于计算两个“日期”间隔
37      *
38      * 6、时间校订器
39      * TemporalAdjuster/TemporalAdjusters 调整时间:例如获取下一个周日等
40      *
41      * 7、日期解析和格式化
42      * java.time.format.DateTimeFormatter类
43      */
44     public static void main(String[] args) {
45         /**
46          * 计算时间间隔
47          */
48         Instant in1 = Instant.now();
49         Instant in2 = Instant.now();
50         System.out.println(Duration.between(in1, in2).toMillis());
51 
52         /**
53          * 日期格式化
54          */
55         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
56         LocalDateTime time1 = LocalDateTime.now();
57         String formatDateTime = time1.format(formatter);
58 
59         /**
60          * 日期解析
61          */
62         LocalDateTime dateTime = LocalDateTime.parse(formatDateTime, formatter);
63     }
64 }

日期操做咱们一般会使用Apache commons包,可是这样就会引入一个包,可以使用java自己的就是用java自己的。线程

 

5、接口方法code

 1 /**
 2  * @author zhaojigang
 3  * @date 2018/5/19
 4  */
 5 public class InterfaceTest {
 6     public static void main(String[] args) {
 7         MyImpl myClass = new MyImpl();
 8         System.out.println(myClass.getName());
 9         System.out.println(MyInterface.getAge());
10     }
11 }
12 
13 interface MyInterface {
14     default String getName(){
15         return "nana";
16     }
17 
18     static Integer getAge(){
19         return 18;
20     }
21 }
22 
23 /**
24  * 若一个接口中定义了一个默认方法,而另一个父类中又定义了一个同名的方法时,取父类
25  * 若一个接口中定义了一个默认方法,而另一个实现接口中又定义了一个同名的方法时,实现类须要执行重写其中一个
26  */
27 class MyImpl implements MyInterface {
28 }

在jdk9中,接口中能够定义private方法。orm

相关文章
相关标签/搜索