interface
public interface JDK8Interface {
void thisMethod();
default void defaultMethod(){
System.out.println("jdk8接口默认方法");
}
static void staticMethod(){
System.out.println("jdk8接口静态方法");
}
}
public class JDK8InterfcaeImpl implements JDK8Interface {
@Override
public void thisMethod() {
System.out.println("实现接口");
}
public static void main(String[] args) {
JDK8InterfcaeImpl jdk8Interfcae = new JDK8InterfcaeImpl();
jdk8Interfcae.thisMethod();
jdk8Interfcae.defaultMethod();
JDK8Interface.staticMethod();
}
}
函数接口
1.一个接口只允许一个抽象方法
2.在函数接口中允许定义Object类 中的方法
3.在函数接口中 允许定义默认和静态方法
4.添加@FunctionalInterface 标识该接口未函数接口
@FunctionalInterface
public interface JDK8Interface {
void thisMethod();
default void defaultMethod(){
System.out.println("jdk8接口默认方法");
}
static void staticMethod(){
System.out.println("jdk8接口静态方法");
}
/**
* 重新Object中的toString方法
* @return
*/
String toString();
}
Lambda表达式
lambda前提是依赖于函数接口
语法 ()->{}
无参数
public interface JDK8InterfaceLambda {
void noParamLambdaMethod();
}
public class TextJDK8InterfaceLambda {
public static void main(String[] args) {
((JDK8InterfaceLambda)()->{
System.out.println("lambda表达式依赖于函数接口");
}).noParamLambdaMethod();
}
}
有参数
public interface JDK8InterfaceLambda {
void noParamLambdaMethod(String var1,String var2);
}
public class TextJDK8InterfaceLambda {
public static void main(String[] args) {
//第一种写法
((JDK8InterfaceLambda)(var1,var2)->{
System.out.println(var1);
System.out.println(var2);
}).noParamLambdaMethod("参数一","参数二");
//第二种写法
JDK8InterfaceLambda jdk8InterfaceLambda = (var3,var4)->{
System.out.println(var3);
System.out.println(var4);
};
jdk8InterfaceLambda.noParamLambdaMethod("参数三","参数四");
}
}
Stream流相关操作
public class TestStream {
public static void main(String[] args) {
List<UserEntity> list = new ArrayList<>();
UserEntity user = new UserEntity("张三",20);
UserEntity user1 = new UserEntity("隶属",21);
UserEntity user2 = new UserEntity("鬼道",18);
UserEntity user3 = new UserEntity("斯莱穆",30);
UserEntity user4 = new UserEntity("斯莱穆",30);
list.add(user);list.add(user1);list.add(user2);list.add(user3);list.add(user4);
/**
* 创建stream流有两种方式 1.串行流 stream() 2.并行流parallelStream()
* stream() 单线程的 并行流parallelStream() 是多线程的
*/
//1.串行流
Stream<UserEntity> stream = list.stream();
//2.并行流
Stream<UserEntity> userEntityStream = list.parallelStream();
/**
* 将stream流转化为set集合 如果想要set去重得重写 equals()方法
*/
Set<UserEntity> setUserEntity = stream.collect(Collectors.toSet());
System.out.println("set集合遍历");
setUserEntity.forEach(userEntity -> {
System.out.println(userEntity);
});
/**
* 将stream流转化为map集合
*/
System.out.println("map集合遍历");
Stream<UserEntity> mapStream = list.stream();
//有重复key时下面这种方式报错 Duplicate key UserEntity{name='斯莱穆', age=30}
//Map<String, UserEntity> maps = mapStream.collect(Collectors.toMap(userEntity -> userEntity.getName(), userEntity -> userEntity));
//处理重复key
Map<String, UserEntity> maps = mapStream.collect(Collectors.toMap(userEntity -> userEntity.getName(), userEntity -> userEntity,(k1, k2) -> k1));
maps.forEach((k,v)->{
System.out.println(k+"-->"+v);
});
/**
* stream流求和
*/
//返回其元素为指定值的顺序流
System.out.println("对指定值进行求和");
Stream<Integer> integerStream = Stream.of(10, 50, 60, 70);
// integerStream.forEach(integer -> System.out.println(integer)); //注:一个stream 只能使用一次
// 求和
Optional<Integer> reduce = integerStream.reduce((a, a2) -> a + a2);
System.out.println("求和结果"+reduce.get());
System.out.println("对list中的age进行求和");
Stream<UserEntity> reduceStream = list.stream();
Optional<UserEntity> sum = reduceStream.reduce((a, a1) -> {
//对user的age求和
UserEntity userEntity = new UserEntity();
Integer age = a.getAge() + a1.getAge();
userEntity.setAge(age);
userEntity.setName("sum");
return userEntity;
});
System.out.println("求和结果"+sum.get());
/**
* 求list集合中 年龄最大和 最小的元素
*/
System.out.println("求取集合中最大值");
Stream<UserEntity> maxstream = list.stream();
Optional<UserEntity> max = maxstream.max((a, b) -> {
return a.getAge()-b.getAge();
}); // 这一段lambda表达式 后面会做替换需要了解如何使用请继续往下看
System.out.println("最大值"+max.get());
System.out.println("求取集合中最小值");
Stream<UserEntity> minStream = list.stream();
Optional<UserEntity> min = minStream.min((a, b) -> {
return a.getAge() - b.getAge();
});
System.out.println("最小值"+min.get());
/**
* match 条件匹配
*/
Stream<UserEntity> anyMatchStream = list.stream();
System.out.println("anyMatch条件匹配开始");
boolean anyMatch = anyMatchStream.anyMatch(t -> {
return t.getAge() == 20;
}); //anyMatch 表示只要其中一条能够被配上 就返回true
System.out.println("条件匹配:"+anyMatch);
System.out.println("allMatch条件匹配开始");
Stream<UserEntity> allMatchStream = list.stream();
boolean allMatch = allMatchStream.allMatch(t->t.getAge()==20);//allMatch 表示全部匹配成功 才返回true
System.out.println("条件匹配:"+allMatch);
System.out.println("noneMatch条件匹配开始");
Stream<UserEntity> noneMatchStream = list.stream();
boolean noneMatch = noneMatchStream.noneMatch(t -> t.getAge() == 20);//noneMatch 表示全部都匹配不上才返回true
System.out.println("条件匹配:"+noneMatch);
/**
* stream流过滤器
*/
System.out.println("########stream流过滤器");
Stream<UserEntity> streamFilter = list.stream();
Stream<UserEntity> userEntityStreamFilter = streamFilter.filter(t -> t.getAge()>26);
userEntityStreamFilter.forEach(userEntity -> System.out.println("年龄大于26:"+userEntity));
/**
* stream 对数据分页
*/
System.out.println("#########stream对数据分页 limit从头开始");
Stream<UserEntity> limitStream = list.stream();
Stream<UserEntity> limitStreamUserEntitys = limitStream.limit(4);
limitStreamUserEntitys.forEach(userEntity -> {
System.out.println("####################"+userEntity);
});
Stream<UserEntity> skipStream = list.stream();
System.out.println("#########stream对数据分页 skip 跳过");
Stream<UserEntity> skip = skipStream.skip(1).limit(4);
skip.forEach(s->System.out.println("####################"+s));
/**
* stream流排序
*/
System.out.println("根据年龄由小到大排序:");
Stream<UserEntity> sortedStream = list.stream();
Stream<UserEntity> sorted = sortedStream.sorted((a, a2) -> a.getAge() - a2.getAge());
sorted.forEach(s-> System.out.println(s));
System.out.println("根据年龄由大到小排序:");
Stream<UserEntity> sortedStream2 = list.stream();
Stream<UserEntity> sorted1 = sortedStream2.sorted((a, a1) -> a1.getAge() - a.getAge());
sorted1.forEach(s-> System.out.println(s));
}
}
函数引用
静态方法引用
语法:类名::(静态方法)方法名称
//函数接口
@FunctionalInterface
public interface FunctionInterface {
void getInteface(int a);
}
public class FunctionReference {
public static void main(String[] args) {
FunctionInterface functionInterface= FunctionReference::cc; //个人理解:相当于 把下面的cc 静态方法 当作了 FunctionInterface 接口getInteface()的实现
functionInterface.getInteface(1);
}
private static void cc(int a) {
System.out.println(a);
}
}
实例方法引用
语法:先new对象 对象实例::实例方法名称
//函数接口
@FunctionalInterface
public interface FunctionInterface {
void getInteface(int a);
}
public class FunctionReference {
public static void main(String[] args) {
FunctionReference reference = new FunctionReference();
FunctionInterface functionInterface= reference::cc;
functionInterface.getInteface(1);
}
private void cc(int a) {
System.out.println(a);
}
}
构造方法引用
语法:类名::new4
@FunctionalInterface
public interface FunctionInterface {
FunctionReference getInteface();
}
public class FunctionReference {
FunctionReference(){}
public static void main(String[] args) {
FunctionInterface functionReference = FunctionReference::new;
}
}
对象方法引用
@FunctionalInterface
public interface FunctionInterface {
void getInteface(FunctionReference functionReference);
}
public class FunctionReference {
FunctionReference(){}
public void getClassReference(){
System.out.println("对象方法引用");
}
public static void main(String[] args) {
FunctionReference functionReference = new FunctionReference();
FunctionInterface functionInterface = FunctionReference::getClassReference;
functionInterface.getInteface(functionReference);
}
}
Optional
ofNullable() 允许传递一个空值,返回Optional
of() 不允许传递空值,返回Optional
这里设置空串的话是可以取到空串的
String userName = null;
//Optional<String> s = Optional.of(userName); //会报错,不允许传空值
Optional<String> s1 = Optional.ofNullable(userName);
// System.out.println(s.get());
// System.out.println(s1.get()); //在没有设值的时候会报错
isPresent() 如果为null 返回false 如果不为null 返回ture;
boolean present = s1.isPresent();
如果为空设置默认值
String cc = s1.ofNullable(userName).orElse("cc");
Optional过滤
public static void main(String[] args) {
String userName = "cc";
Optional<String> s1 = Optional.ofNullable(userName).filter(t-> t.equals("cc"));
System.out.println(s1);
boolean present = s1.isPresent();
System.out.println(present);
String userName1 = "pp";
Optional<String> s2 = Optional.ofNullable(userName1).filter(t -> "cc".equals(t));
System.out.println(s2);
boolean present2 = s2.isPresent();
System.out.println(present2);
Optional.ofNullable(userName1).ifPresent(t->{
System.out.println(t);
});
}