文章目录
内容介绍
- Properties集合
- Lambda表达式
- 函数式接口
- Stream流
目标
- 能够理解函数式编程相对于面向对象的优点
- 能够掌握Lambda表达式的标准格式
- 能够掌握Lambda表达式的省略格式与规则
- 能够使用Consumer<T>函数式接口
- 能够使用Predicate<T>函数式接口
- 能够理解流与集合相比的优点
一、Properties集合【重点】
1.1 Properties集合回顾
/*
java.util.Properties集合是Hashtable的子类,双列集合
主要使用的是与String相关的方法
常用方法:
public Object setProperty(String k,String v): 保存一个键值对(属性名=属性值)
public String getProperty(String key): 根据属性名获取属性值
public Set<String> stringPropertyNames(): 获取所有属性对应的Set集合
java.lang.Systen类:与系统相关的工具类
静态方法:
public static Properties getProperties(): 获取系统相关的属性名和属性值
*/
public class Demo01Properties {
public static void main(String[] args) {
// Stream类调用静态方法,获取Properties集合对象,
Properties props = System.getProperties();
// 获取Properties集合对象的所有属性对应的Set集合
Set<String> propertyNames = props.stringPropertyNames();
// 增强for遍历
for (String propertyName : propertyNames) {
// Properties集合对象调用getProperty方法,根据键获取值
String propertyValue = props.getProperty(propertyName);
System.out.println(propertyName + "::" + propertyName);
}
// 调用method方法
method();
}
// Properties集合的基本使用
public static void method() {
Properties props = new Properties();
// 添加键值对
props.setProperty("name","张三");
props.setProperty("age","28");
props.setProperty("gender","男");
// 获取Properties集合对象的所有属性对应的Set集合
Set<String> propertyNames = props.stringPropertyNames();
// 遍历集合
for (String propertyName : propertyNames) {
// Properties集合对象调用getProperty方法,根据键获取值
String propertyValue = props.getProperty(propertyName);
System.out.println(propertyName + "::" + propertyValue);
}
}
}
运行结果:
1.2 Properties集合的load方法【重要】
/*
Properties集合
与IO流相关的成员方法(必须有Properties对象调用)
load方法内部,指挥InputStream/Reader的子类对象,读取文件,load方法内部把获取到的文件内容,
进行处理,处理成键值对的形式,存储到Properties集合对象中,至于如何处理的,我们先不关心
public void load(InputStream inStream): 从字节输入流中读取键值对
参数:
InputStream isStream: 抽象父类,传递子类FileInputStream对象
public void load(Reader reader):从字符输入流中去读键值对
参数:
Reader reader:抽象父类,传递子类FileReader对象
使用步骤:
1.load方法加载的文件,要求扩展名(怕配置信息).properties,一般存储到stc根目录下,
数据存储格式:属性名 = 属性值,注释:用#
2.创建Properties集合对象
3.创建InputStream/Reader的子类对象,绑定源文件
4.Properties集合对象调用load方法,传递InputStream/Reader的子类对象
把文件内容以键值对的方式加载到Properties集合对象中
5.遍历集合
*/
public class Demo02Properties {
public static void main(String[] args) throws IOException {
// 2.创建Properties集合对象
Properties props = new Properties();
// 3.创建文件输入流对象,绑定源文件
InputStream is = new FileInputStream("d10_Lambda\\src\\config.properties");
// 4.Properties集合对象调用load方法,把文件内容以键值对的方式加载到Properties集合对象中
props.load(is);
// 5.遍历集合
for (String PropertyName : props.stringPropertyNames()) {
String propertyValue = props.getProperty(PropertyName);
System.out.println(PropertyName + "=" + propertyValue);
}
}
}
运行结果:
1.3 Properties集合的store方法[了解]
/*
Properties集合与IO流相关的成员方法(必须由Properties对象调用)
store方法内部,指挥OutputStream/Writer的子类对象,把Properties集合内容键值对内容,
以键值对的方式写入到目标文件中,至于如何处理的,我们不关心
public void store(OutputStream outStream,String comments):
把Properties集合对象中的键值对保存到文件中
参数:
OutputStream outStream:抽象父类,传递子类FileOutputStream对象
String comments:给properties文件的说明信息,可以直接写null
public void store(Writer writer,String comments):
把Properties集合对象中的键值对保存到文件中
参数:
Writer writer:抽象父类,传递子类FileWriter对象
String comments:给properties文件的说明信息,可以直接写null
*/
public class Demo03Properties {
public static void main(String[] args) throws IOException {
// 2.创建Properties集合对象
Properties props = new Properties();
// 3.创建InputStream的子类对象,绑定源文件
InputStream is = new FileInputStream("d10_Lambda\\src\\config.properties");
// 4.Properties集合对象调用load方法传递InputStream的子类对象,把文件内容以键值对的方式加载到Properties集合对象中
props.load(is);
// 5.遍历集合
for (String PropertyName : props.stringPropertyNames()) {
// Properties集合对象调用getProperty方法,根据键获取值
String propertyValue = props.getProperty(PropertyName);
if ("age".equals(PropertyName) && Integer.parseInt(propertyValue) == 18) {
// age属性增加2岁
props.setProperty(PropertyName,Integer.parseInt(propertyValue) + 2 + "");
System.out.println("添加成功");
}
}
// 创建OutputStream的子类对象,绑定目标文件
OutputStream os = new FileOutputStream("d10_Lambda\\src\\config.properties");
// Properties集合对象调用store方法,把Porperties集合对象中的键值对加载到文件中
props.store(os,null);
}
}
原数据:
运行结果:
二、Lambda表达式
2.1 函数式编程思想和Lambda表达式体验
/*
函数式思想则尽量忽略面向对象的复杂语法——强调做什么,而不是以什么形式做。
lambda表达式书写规则:
使用匿名内部类方式中,只保留覆盖重写后的方法的()和{},在()和{}之间,添加->,其他全部省略
可以推导,就是可以省略
行为参数化
lambda表达式是对匿名内部类的简化书写格式
*/
public class Demo01Lambda {
public static void main(String[] args) {
// 1.匿名内部类的方式创建并开启线程
// 通过匿名内部类对象的方式,给Thread对象,传一段代码执行
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("线程任务AAA开启....");
}
}).start();
// 2.Lambda表达式的方式
new Thread(()->{
System.out.println("线程任务BBB开启....");
}).start();
}
}
运行结果:
2.2 Lambda表达式的标准格式
学习目标: 能够掌握Lambda表达式的标准格式
/*
Lambda表达式的标准格式
1.Lambda表达式的组成:
(1)一个箭头: ->
(2)一些参数: ()内部可以写参数
(3)一些代码: {}中的内容
2.标准格式:
(参数类型 参数名称) -> {代码语句}
3.格式解释:
(1)小括号:就是以前定义方法的(),内部写的是方法参数列表
没有参数,也不能省略!!
(2)箭头->:代表方法的参数传递
(3)大括号{}:就是以前定义方法的{},代表方法体,方法的功能代码
*/
2.3 Lambda表达式有参数有返回值及省略方式
/*
Lambda表达式的省略格式
1.数据类型可以省略:
(Person p) 简化为 (p)
(Person p1,Person p2) 简化为 (p1,p2)
2.如果只有一个参数,()可以省略
(Person p) 简化为 (p) 再简化为 p
3.如果{}中只有一句话,那么{}和分号和return,全部可以省略
注意:要么全部省略,要么全部暴露
4.->:永远都不能省略
Collections工具类,实现自定义排序
public static <T> void sort(List<T> list,Comparator<T> comp):
将集合list中元素按照指定规则comp排序
参数:
1.List<T> list:接口,传递实现类对象 ArrayList,LinkedList
2.Comparator<T> comp:接口,传递实现类对象,匿名内部类
抽象方法:
public int compare(T o1, T o2):用来指定排序规则的
第一个参数 - 第二个参数:升序
第二个参数 - 第一个参数:降序
参数T是引用类型,不能直接键
举例:
按照Person对象age属性,从小到大排序
public int conmpare(Person o1, Person o2) {
return o1.getAge() - o2,getAge();
}
按照Student对象 age属性,从大到小排序
public int compare(Person o1, Person o2) {
return o2.getAge() - o1,getAge();
}
List集合存储多个Person对象,完成对List集合的排序,按照年龄排序
*/
public class Demo02Lambda {
public static void main(String[] args) {
// 创建List集合对象,并添加数据
List<Person> list = new