赞
踩
目录
详细介绍可参考B站视频:动态-哔哩哔哩
优点:
1.代码更加简洁
2.减少匿名内部类的创建,节省资源
3.使用时不用去记忆所使用的接口和抽象函数
缺点:
1.不易于后期维护,必须熟悉lambda表达式和抽象函数中参数的类型
2.可读性差
1. 方法的参数或局部变量类型必须为接口才能使用Lambda
2. 接口中有且仅有一个抽象方法(@FunctionalInterface)
- public class demo {
- public static void main(String[] args) {
- fun1((arr)->{
- int sum = 0 ;
- for (int i : arr) {
- sum += i;
- }
- return sum;
- });
- }
- public static void fun1(Operator operator){
- int[] arr = {1,2,3,4};
- int sum = operator.getSum(arr);
- System.out.println("sum = " + sum);
- }
- }
- /**
- * 函数式接口
- */
- @FunctionalInterface
- interface Operator{
- int getSum(int[] arr);
- }
在JDK中帮我们提供的有函数式接口,主要是在 java.util.function 包中。
Supplier(供给型或生产型接口):无参有返回值的接口,对于的Lambda表达式需要提供一个返回数据的类型。
Consumer(消费型接口):该接口中的方法可以接收一个参数,接收的参数类型由泛型指定,对参数的操作 方式根据该接口的实现类决定,不需要返回值。
扩展方法 --> andThen 。如果一个方法的参数和返回值全部是Consumer类型,那么就可以实现效果,消费一个数据的时候,先通过调用者对象处理参数,将处理的结果再通过f对象处理,将两个处理的结果进行返回。
Function(函数型接口):有参有返回值的接口,Function接口是根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。有参数有返回值。
扩展方法:同样具有andThen,另外compose方法的作用顺序和andThen方法刚好相反而静态方法identity则是,输入什么参数就返回什么参数(参考源码)
Predicate(断言型接口):有参且返回值为Boolean的接口。
扩展方法:
and(Predicate p):先将参数通过调用者判断真假,再将参数通过p判断真假,全真为真,否则为假
or(Predicate p):全假为假,否则为真
negate():取反
java8提供的stream api可以让程序员像操作数据库一样操作集合,通过各种条件对集合进行一次性的过滤,省去对集合的反复过滤存储塞选的过程。
1.Stream的特点:
- public static void main(String[] args) {
- List<String> list = new ArrayList<>();
- list.stream();
- Set<String> set = new HashSet<>();
- set.stream();
- Vector vector = new Vector();
- vector.stream();
- }
注:Map没有实现Collection接口,可通过获取对应的key或value集合获取
- public static void main(String[] args) {
- Stream<String> a1 = Stream.of("a1", "a2", "a3");
- String[] arr1 = {"aa","bb","cc"};
- Stream<String> arr11 = Stream.of(arr1);
- Integer[] arr2 = {1,2,3,4};
- Stream<Integer> arr21 = Stream.of(arr2);
- arr21.forEach(System.out::println);
- // 注意:基本数据类型的数组是不行的
- int[] arr3 = {1,2,3,4};
- Stream.of(arr3).forEach(System.out::println);
- }
| 方法名 | 方法作用 | 返回值类型 | 方法种类 |
| count | 统计个数 | long | 终结 |
| forEach | 逐一处理 | void | 终结 |
| filter | 过滤 | Stream | 函数拼接 |
| limit | 取用前几个 | Stream | 函数拼接 |
| skip | 跳过前几个 | Stream | 函数拼接 |
| nap | 映射 | Stream | 函数拼接 |
| concat | 组合 | Stream | 函数拼接 |
- public void test02(){
- List<Integer> list = new ArrayList<>();
- // 通过List 接口 直接获取并行流
- Stream<Integer> integerStream = list.parallelStream();
- // 将已有的串行流转换为并行流
- Stream<Integer> parallel = Stream.of(1, 2, 3).parallel();
- }
- /**
- * 并行流操作
- */
- @Test
- public void test03(){
- Stream.of(1,4,2,6,1,5,9)
- .parallel() // 将流转换为并发流,Stream处理的时候就会通过多线程处理
- .filter(s->{
- System.out.println(Thread.currentThread() + " s=" +s);
- return s > 2;
- }).count();
- }
在多线程的处理下,数据安全问题处理:
- /**
- * 使用线程安全的容器
- */
- @Test
- public void test03(){
- Vector v = new Vector();
- Object obj = new Object();
- IntStream.rangeClosed(1,1000)
- .parallel()
- .forEach(i->{
- synchronized (obj){
- v.add(i);
- }
- });
- System.out.println(v.size());
- }
- /**
- * 将线程不安全的容器转换为线程安全的容器
- */
- @Test
- public void test04(){
- List<Integer> listNew = new ArrayList<>();
- // 将线程不安全的容器包装为线程安全的容器
- List<Integer> synchronizedList = Collections.synchronizedList(listNew);
- Object obj = new Object();
- IntStream.rangeClosed(1,1000)
- .parallel()
- .forEach(i->{
- synchronizedList.add(i);
- });
- System.out.println(synchronizedList.size());
- }
- /**
- * 我们还可以通过Stream中的 toArray方法或者 collect方法来操作
- * 就是满足线程安全的要求
- */
- @Test
- public void test05(){
- List<Integer> listNew = new ArrayList<>();
- Object obj = new Object();
- List<Integer> list = IntStream.rangeClosed(1, 1000)
- .parallel()
- .boxed()
- .collect(Collectors.toList());
- System.out.println(list.size());
- }
https://www.cnblogs.com/buptlyh/p/16596907.html
全网最全Fork-Join讲解(从概括到实战)_fork join_王翔:的博客-CSDN博客
当类A、B、C、D都实现了接口xxInterface,此时在接口xxInterface中新增一个方法,那么A、B、C、D都必须实现这个方法,但是想象一下,如果有数百个类实现了一个接口,那么几乎不可能更改所有这些类中的代码。这就是为什么在Java8中,我们有了一个新概念“默认方法”。这些方法可以添加到任何现有接口中,我们不需要强制在实现类中实现这些方法,因此我们可以在不破坏代码的情况下将这些默认方法添加到现有接口中。我们只需要在新增的方法中使用default即可。
语法规则:
interface 接口名{
修饰符 default 返回值类型 方法名{
方法体;
}
}
使用方式:
1. 实现类直接调用接口的默认方法
2. 实现类重写接口的默认方法
作用也是为了接口的扩展。
语法规则:
interface 接口名{
修饰符 static 返回值类型 方法名{
方法体;
}
}
使用方式:
接口中的静态方法在实现类中是不能被重写的,调用的话只能通过接口类型来实现: 接口名.静态方法名();
1. 默认方法通过实例调用,静态方法通过接口名调用
2. 默认方法可以被继承,实现类可以直接调用接口默认方法,也可以重写接口默认方法
3. 静态方法不能被继承,实现类不能重写接口的静态方法,只能使用接口名调用
- /**
- * 旧版日期时间设计的问题
- */
- @Test
- public void test01() throws Exception{
- // 1.设计不合理
- Date date = new Date(2021,05,05);
- System.out.println(date);
- // 2.时间格式化和解析操作是线程不安全的
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- for (int i = 0; i < 50; i++) {
- new Thread(()->{
- // System.out.println(sdf.format(date));
- try {
- System.out.println(sdf.parse("2021-05-06"));
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }).start();
- }
- }
2.1 日期时间的常见操作
- /**
- * JDK8 日期时间操作
- */
- @Test
- public void test01(){
- // 1.创建指定的日期
- LocalDate date1 = LocalDate.of(2021, 05, 06);
- System.out.println("date1 = "+date1);
- // 2.得到当前的日期
- LocalDate now = LocalDate.now();
- System.out.println("now = "+now);
- // 3.根据LocalDate对象获取对应的日期信息
- System.out.println("年:" + now.getYear());
- System.out.println("月:" + now.getMonth().getValue());
- System.out.println("日:" + now.getDayOfMonth());
- System.out.println("星期:" + now.getDayOfWeek().getValue());
- }
- /**
- * 时间操作
- */
- @Test
- public void test02(){
- // 1.得到指定的时间
- LocalTime time = LocalTime.of(5,26,33,23145);
- System.out.println(time);
- // 2.获取当前的时间
- LocalTime now = LocalTime.now();
- System.out.println(now);
- // 3.获取时间信息
- System.out.println(now.getHour());
- System.out.println(now.getMinute());
- System.out.println(now.getSecond());
- System.out.println(now.getNano());
- }
- /**
- * 日期时间类型 LocalDateTime
- */
- @Test
- public void test03(){
- // 获取指定的日期时间
- LocalDateTime dateTime =
- LocalDateTime.of(2020
- , 06
- , 01
- , 12
- , 12
- , 33, 213);
- System.out.println(dateTime);
- // 获取当前的日期时间
- LocalDateTime now = LocalDateTime.now();
- System.out.println(now);
- // 获取日期时间信息
- System.out.println(now.getYear());
- System.out.println(now.getMonth().getValue());
- System.out.println(now.getDayOfMonth());
- System.out.println(now.getDayOfWeek().getValue());
- System.out.println(now.getHour());
- System.out.println(now.getMinute());
- System.out.println(now.getSecond());
- System.out.println(now.getNano());
- }
- /**
- * 日期时间的修改
- */
- @Test
- public void test01(){
- LocalDateTime now = LocalDateTime.now();
- System.out.println("now = "+now);
- // 修改日期时间 对日期时间的修改,对已存在的LocalDate对象,创建了它模板
- // 并不会修改原来的信息
- LocalDateTime localDateTime = now.withYear(1998);
- System.out.println("now :"+now);
- System.out.println("修改后的:" + localDateTime);
- System.out.println("月份:" + now.withMonth(10));
- System.out.println("天:" + now.withDayOfMonth(6));
- System.out.println("小时:" + now.withHour(8));
- System.out.println("分钟:" + now.withMinute(15));
- // 在当前日期时间的基础上 加上或者减去指定的时间
- System.out.println("两天后:" + now.plusDays(2));
- System.out.println("10年后:"+now.plusYears(10));
- System.out.println("6个月后 = " + now.plusMonths(6));
- System.out.println("10年前 = " + now.minusYears(10));
- System.out.println("半年前 = " + now.minusMonths(6));
- System.out.println("一周前 = " + now.minusDays(7));
- }
- /**
- * 日期时间的比较
- */
- @Test
- public void test02(){
- LocalDate now = LocalDate.now();
- LocalDate date = LocalDate.of(2020, 1, 3);
- // 在JDK8中要实现 日期的比较 isAfter isBefore isEqual 通过这几个方法来直接比较
- System.out.println(now.isAfter(date)); // true
- System.out.println(now.isBefore(date)); // false
- System.out.println(now.isEqual(date)); // false
- }
- /**
- * 日期格式化
- */
- @Test
- public void test01(){
- LocalDateTime now = LocalDateTime.now();
- // 指定格式 使用系统默认的格式 2021-05-27T16:16:38.139
- DateTimeFormatter isoLocalDateTime =
- DateTimeFormatter.ISO_LOCAL_DATE_TIME;
- // 将日期时间转换为字符串
- String format = now.format(isoLocalDateTime);
- System.out.println("format = " + format);
- // 通过 ofPattern 方法来指定特定的格式
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMM-dd HH:mm:ss");
- String format1 = now.format(dateTimeFormatter);
- // 2021-05-27 16:16:38
- System.out.println("format1 = " + format1);
- // 将字符串解析为一个 日期时间类型
- LocalDateTime parse = LocalDateTime.parse("1997-05-06 22:45:16",
- dateTimeFormatter);
- // parse = 1997-05-06T22:45:16
- System.out.println("parse = " + parse);
- }
- /**
- * Instant 时间戳
- * 可以用来统计时间消耗
- */
- @Test
- public void test01() throws Exception{
- Instant now = Instant.now();
- System.out.println("now = " + now);
- // 获取从1970年一月一日 00:00:00 到现在的 纳秒
- System.out.println(now.getNano());
- Thread.sleep(5);
- Instant now1 = Instant.now();
- System.out.println("耗时:" + (now1.getNano() - now.getNano()));
- }
- /**
- * 计算日期时间差
- */
- @Test
- public void test01(){
- // 计算时间差
- LocalTime now = LocalTime.now();
- LocalTime time = LocalTime.of(22, 48, 59);
- System.out.println("now = " + now);
- // 通过Duration来计算时间差
- Duration duration = Duration.between(now, time);
- System.out.println(duration.toDays()); // 0
- System.out.println(duration.toHours()); // 6
- System.out.println(duration.toMinutes()); // 368
- System.out.println(duration.toMillis()); // 22124240
- // 计算日期差
- LocalDate nowDate = LocalDate.now();
- LocalDate date = LocalDate.of(1997, 12, 5);
- Period period = Period.between(date, nowDate);
- System.out.println(period.getYears()); // 23
- System.out.println(period.getMonths()); // 5
- System.out.println(period.getDays()); // 22
- }
- /**
- * 时间校正器
- */
- @Test
- public void test02(){
- LocalDateTime now = LocalDateTime.now();
- // 将当前的日期调整到下个月的一号
- TemporalAdjuster adJuster = (temporal)->{
- LocalDateTime dateTime = (LocalDateTime) temporal;
- LocalDateTime nextMonth = dateTime.plusMonths(1).withDayOfMonth(1);
- System.out.println("nextMonth = " + nextMonth);
- return nextMonth;
- };
- // 我们可以通过TemporalAdjusters 来实现
- // LocalDateTime nextMonth = now.with(adJuster);
- LocalDateTime nextMonth =
- now.with(TemporalAdjusters.firstDayOfNextMonth());
- System.out.println("nextMonth = " + nextMonth);
- }
- /**
- * 时区操作
- */
- @Test
- public void test01() {
- // 1.获取所有的时区id
- // ZoneId.getAvailableZoneIds().forEach(System.out::println);
- // 获取当前时间 中国使用的 东八区的时区,比标准时间早8个小时
- LocalDateTime now = LocalDateTime.now();
- System.out.println("now = " + now); // 2021-05-27T17:17:06.951
- // 获取标准时间
- ZonedDateTime bz = ZonedDateTime.now(Clock.systemUTC());
- System.out.println("bz = " + bz); // 2021-05-27T09:17:06.952Z
- // 使用计算机默认的时区,创建日期时间
- ZonedDateTime now1 = ZonedDateTime.now();
- System.out.println("now1 = " + now1); //2021-05-
- 27 T17:
- 17:06.952 + 08:00[Asia / Shanghai]
- // 使用指定的时区创建日期时间
- ZonedDateTime now2 = ZonedDateTime.now(ZoneId.of("America/Marigot"));
- System.out.println("now2 = " + now2);
- }
- @Test
- public void test01(){
- //String userName = "张三";
- String userName = null;
- if(userName != null){
- System.out.println("字符串的长度:" + userName.length());
- }else{
- System.out.println("字符串为空");
- }
- }
- /**
- * Optional对象的创建方式
- */
- @Test
- public void test02(){
- // 第一种方式 通过of方法 of方法是不支持null的
- Optional<String> op1 = Optional.of("zhangsan");
- //Optional<Object> op2 = Optional.of(null);
- // 第二种方式通过 ofNullable方法 支持null
- Optional<String> op3 = Optional.ofNullable("lisi");
- Optional<Object> op4 = Optional.ofNullable(null);
- // 第三种方式 通过empty方法直接创建一个空的Optional对象
- Optional<Object> op5 = Optional.empty();
- }
- /**
- * Optional中的常用方法介绍
- * get(): 如果Optional有值则返回,否则抛出NoSuchElementException异常
- * get()通常和isPresent方法一块使用
- * isPresent():判断是否包含值,包含值返回true,不包含值返回false
- * orElse(T t):如果调用对象包含值,就返回该值,否则返回t
- * orElseGet(Supplier s):如果调用对象包含值,就返回该值,否则返回 Lambda表达式的返
- 回值
- */
- @Test
- public void test03(){
- Optional<String> op1 = Optional.of("zhangsan");
- Optional<String> op2 = Optional.empty();
- // 获取Optional中的值
- if(op1.isPresent()){
- String s1 = op1.get();
- System.out.println("用户名称:" +s1);
- }
- if(op2.isPresent()){
- System.out.println(op2.get());
- }else{
- System.out.println("op2是一个空Optional对象");
- }
- String s3 = op1.orElse("李四");
- System.out.println(s3);
- String s4 = op2.orElse("王五");
- System.out.println(s4);
- String s5 = op2.orElseGet(()->{
- return "Hello";
- });
- System.out.println(s5);
- }
- @Test
- public void test04(){
- Optional<String> op1 = Optional.of("zhangsan");
- Optional<String> op2 = Optional.empty();
- // 如果存在值 就做什么
- op1.ifPresent(s-> System.out.println("有值:" +s));
- op1.ifPresent(System.out::println);
- }
- /**
- * 自定义一个方法,将Person对象中的 name 转换为大写 并返回
- */
- @Test
- public void test05(){
- Person p = new Person("zhangsan",18);
- Optional<Person> op = Optional.of(p);
- String name = getNameForOptional(op);
- System.out.println("name="+name);
- }
- /**
- * 根据Person对象 将name转换为大写并返回
- * 通过Optional方式实现
- * @param op
- * @return
- */
- public String getNameForOptional(Optional<Person> op){
- if(op.isPresent()){
- String msg = //op.map(p -> p.getName())
- op.map(Person::getName)
- //.map(p -> p.toUpperCase())
- .map(String::toUpperCase)
- .orElse("空值");
- return msg;
- }
- return null;
- }
- /**
- * 根据Person对象 将name转换为大写并返回
- * @param person
- * @return
- */
- public String getName(Person person){
- if(person != null){
- String name = person.getName();
- if(name != null){
- return name.toUpperCase();
- }else{
- return null;
- }
- }else{
- return null;
- }
- }
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MyAnnotations {
- MyAnnotation[] value();
- }
- @Repeatable(MyAnnotations.class)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MyAnnotation {
- String value();
- }
- @MyAnnotation("test1")
- @MyAnnotation("test2")
- @MyAnnotation("test3")
- public class AnnoTest01 {
- @MyAnnotation("fun1")
- @MyAnnotation("fun2")
- public void test01(){
- }
- }
- /**
- * 解析重复注解
- * @param args
- */
- public static void main(String[] args) throws NoSuchMethodException {
- // 获取类中标注的重复注解
- MyAnnotation[] annotationsByType =
- AnnoTest01.class.getAnnotationsByType(MyAnnotation.class);
- for (MyAnnotation myAnnotation : annotationsByType) {
- System.out.println(myAnnotation.value());
- }
- // 获取方法上标注的重复注解
- MyAnnotation[] test01s = AnnoTest01.class.getMethod("test01")
- .getAnnotationsByType(MyAnnotation.class);
- for (MyAnnotation test01 : test01s) {
- System.out.println(test01.value());
- }
- }
- @Target(ElementType.TYPE_PARAMETER)
- public @interface TypeParam {
- }
- public class TypeDemo01 <@TypeParam T> {
- public <@TypeParam K extends Object> K test01(){
- return null;
- }
- }
- @Target(ElementType.TYPE_USE)
- public @interface NotNull {
- }
- public class TypeUseDemo01 {
- public @NotNull Integer age = 10;
- public Integer sum(@NotNull Integer a,@NotNull Integer b){
- return a + b;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。