当前位置: 首页 > news >正文

java项目总结8

1.方法引用

1.方法引用概述

注意注意:

1.引用出必须是函数式接口

2.被引用的方法必须已经存在

3.被引用方法的型参和返回值需要跟抽象方法保持一致

4.被引方法的功能要满足当前需求

Arrays.sort(arr,Main::subtraction);

Main是该类的名称,::是方法引用符

     //方法引用Integer[] arr={4,3,1,6,2,7,8,5};Arrays.sort(arr,run1::subtraction);System.out.println(Arrays.toString(arr));}public static int subtraction(int num1,int num2){return num2-num1;}}

2.引用静态方法

1.方法需要已经存在
2.方法的形参和返回值需要跟抽象方法的形参和返回值保持一致

3.方法的功能需要把形参的字符串转换成整数

未利用方法引用

方法引用如下:引用方法parseInt

    ArrayList<String> list10 = new ArrayList<>();Collections.addAll(list10, "1","4","3","2","6");list10.stream().map(Integer::parseInt).forEach(s->System.out.println(s));

3.引用成员方法

格式:对象::成员方法

1.其他类:其他类对象::方法名

2.本类:this::方法名(引用出处不能是父类方法)

3.父类:super::方法名

1.引用其他成员方法

另一个类所定义的方法如/*线以下所示

第五行使用时候需要(对象,方法);这里的对象是在另一个类,所有需要new erer();

public class Main {public static void main(String[] args) {ArrayList<String> list1=new ArrayList<>();Collections.addAll(list1,"张ad","刘df","张ty","周kdh","fjd");list1.stream().filter(new erer()::stringpd).forEach(s -> System.out.println(s));}}/*      另一个类的方法如下public class erer {public boolean stringpd(String s){return s.startsWith("张")&&s.length()==3;}
}*/

2.引用本类成员方法

注意:静态方法无this(所以要用本类的对象new Main())

​
public class Main {public static void main(String[] args) {ArrayList<String> list1=new ArrayList<>();Collections.addAll(list1,"张ad","刘df","张ty","周kdh","fjd");list1.stream().filter(new Main()::stringpd).forEach(s -> System.out.println(s));}}public class erer {public boolean stringpd(String s){return s.startsWith("张")&&s.length()==3;}
}​

3.引用父类成员方法!

拓展:(挺难的)

4.引用构造方法

稿子或者前提:

未引用构造方法:

//匿名内部类    List<Student> newlist1 = list1.stream().map(new Function<String, Student>() {@Overridepublic Student apply(String s) {String[] arr = s.split(",");String name = arr[0];int age = Integer.parseInt(arr[1]);return new Student(name, age);}}).collect(Collectors.toList());//lambdaList<Student> new2list1 = list1.stream().map(s -> new Student(s.split(",")[0], Integer.parseInt(s.split(",")[1]))).collect(Collectors.toList());

这里要修改Student的录入方式,将(String name,int age)变为(String str)name和age在str中获得。

     //引用构造方法List<Student> new3list1 = list1.stream().map(Student::new).collect(Collectors.toList());System.out.println(new3list1);//其中Strudent这个类里面要加上public Student(String str) {String[] arr = str.split(",");this.name = arr[0];this.age = Integer.parseInt(arr[1]);}

引用构造方法:

5.其他调用方式

1.使用类名引用成员方式

格式:类名::成员方法

范例:String::substring

特有规则:

所以下面才成立:

注意:第一个参数决定了我能引用那个类中的方法。如上图第一个参数是String,引用的类是String。

2.引用数组的构造方式

格式:数据类型[ ]::new

范例:int[ ]::new

细节:数组的类型,需要跟流中数据的类型保持一致。

总结:

6.实战演练

都挺难的!!!

1.转成自定义对象并收集到数组

如下:

Student[] arr的那一行中的第一个Student::new是指引用构造方法,Student[]是指引用数组的构造方法

2.获取部分属性并收集到数组

如下:

技巧:

2.异常

1.

http://www.lryc.cn/news/394343.html

相关文章:

  • 【Nvidia+AI相机】涂布视觉检测方案专注提高锂电池质量把控标准
  • Spring Cloud Alibaba - Sentinel 分布式系统流量哨兵
  • 文件存储的方法一
  • 数据结构/作业/2024/7/7
  • 隔离级别-隔离级别中的锁协议、隔离级别类型、隔离级别的设置、隔离级别应用
  • 【数据结构与算法】希尔排序
  • 【机器学习】(基础篇一) —— 什么是机器学习
  • VitePress安装部署
  • Spring的事务传播机制和隔离级别
  • 华为路由器静态路由配置(eNSP模拟实验)
  • antd实现简易相册,zdppy+vue3+antd实现前后端分离相册
  • PIP换源的全面指南
  • 陶建辉当选 GDOS 全球数据库及开源峰会荣誉顾问
  • Drools开源业务规则引擎(二)- Drools规则语言(DRL)
  • PTA甲级1005:Spell It Right
  • Vue笔记11-Composition API的优势
  • rancher管理多个集群
  • 某大会的影响力正在扩大,吞噬了整个数据库世界!
  • PostgreSQL主从复制:打造高可用数据库架构的秘籍
  • Fast R-CNN(论文阅读)
  • 视觉语言模型:融合视觉与语言的未来
  • 【CSAPP】-linklab实验
  • UE C++ 多镜头设置缩放 平移
  • 代码随想录Day69(图论Part05)
  • 53-1 内网代理3 - Netsh端口转发(推荐)
  • 慧哥充电桩开源平台小程序、PC后、手机商户端历经2年的无数次迭代。
  • 四、(1)网络爬虫入门及准备工作(爬虫及数据可视化)
  • 2024华为OD机试真题-分月饼-(C++/Python)-C卷D卷-200分
  • Git 查看提交历史
  • 力扣双指针算法题目:快乐数