函数式接口
- 函数式接口只能有一个抽象方法,
- 函数式接口可以有默认的方法default方法和static方法,但抽象方法只能有一个
- 函数式接口可以用**@FunctionalInterface**注解进行标注
- 可以重写Object内置的方法,比如equals,hashcode等等
lambda表达式
跟js的箭头函数很像。在函数式接口的简化过程中**(参数)->{…方法体}**
匿名内部类的简写方式
方法引入
- 静态方法引入: 类名::方法名
- 对象方法引入: 类名 :: 实例方法
- 实例方法引入: new 对象 对象实例::方法引入
- 构造函数引入: 类名::new
注意点:方法引入参数和返回类型必须和函数接口的参数列表和返回类型必须保持一致。
示例
public static void staticMethod(Integer a){
System.out.println("我是静态方法:"+a);
}
public String getString(String s){
return "hello world"+s;
}
@Test
public void test5(){
//静态方法引入
((MessageInterface) TestStream::staticMethod).get(5);
//实例方法引入
StringInterface stringInterface = new TestStream() :: getString;
System.out.println(stringInterface.get("你好"));
//构造函数引入
ObjInterface objInterface = User :: new;
System.out.println(objInterface.get("张三",19));
}
}
@FunctionalInterface
interface ObjInterface{
User get(String username,int age);
}
@FunctionalInterface
interface StringInterface{
String get(String str);
}
@FunctionalInterface
interface MessageInterface{
void get(int a);
}
stream流
使用stream的步骤如下:
6. 创建stream;
7. 通过一个或多个中间操作(intermediate operations)将初始stream转换为另一个stream;(filter,distint.sorted,limit…)
8. 通过中止操作(terminal operation)获取结果;该操作触发之前的懒操作的执行,中止操作后,该stream关闭,不能再使用了;(forEach,collect,…)
示例
public class TestStream {
@Test
public void test01(){
List<User> userList = new ArrayList<>();
userList.add(new User("xiaoming",23));
userList.add(new User("xiaowang",45));
userList.add(new User("xiaozhang",65));
User xiaohei = new User("xiaohei", 15);
User xiaohong = new User("xiaohei", 15);
System.out.println(xiaohei.equals(xiaohong));
userList.add(xiaohei);
// userList.add(xiaohei);
//
// Stream<User> stream =
//
//转换set
Set<User> userSet = userList.stream().
collect(Collectors.toSet());
userSet.forEach(System.out::println);
//
//转换map
Map<String, User> userMap = userList.stream().
collect(Collectors.toMap(User::getUsername, user -> user));
userMap.forEach((s, user) -> System.out.println(s+","+user));
//加和
User sum = userList.stream()
.reduce((user, user2) -> new User("sum", user.getAge() + user2.getAge())).get();
System.out.println(sum.getAge());
//最大值,最小值
User user = userList.stream().max((o1,o2)->o1.getAge()-o2.getAge()).get();
System.out.println(user);
//匹配
boolean b = userList.stream()
.allMatch(user1 -> "xiaoming".equals(user1.getUsername()));
System.out.println(b);
//过滤
List<User> collect = userList.stream().
filter(user2 -> user2.getAge() > 20).
collect(Collectors.toList());
collect.forEach(System.out::println);
//分页
userList.stream().skip(2).limit(2).forEach(System.out::println);
//排序
userList.stream().
sorted(((o1, o2) -> o1.getAge() - o2.getAge())).
forEach(System.out::println);
}
}
class User{
private String username;
private int age;
public User() {
}
public User(String username, int age) {
this.username = username;
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
// User user = (User) o;
// return age == user.age &&
// Objects.equals(username, user.username);
if (o instanceof User){
return (username.equals(((User)o).username) && age == ((User)o).age);
}else {
return false;
}
}
@Override
public int hashCode() {
return username.hashCode();
}
}
Optional
api
String username = ".........";
//ofNullable可以传递空值,of不允许传控制
Optional<String> op = Optional.ofNullable(username);
//isPresent判断是否空指
boolean present = op.isPresent();
System.out.println(present);
//设定默认值
String hello = Optional.ofNullable(username).orElse("hello");
System.out.println(hello);
//如果存在即打印
op.ifPresent(System.out::println);