分享下整理的java基础内容。
一共从4个点介绍java8特性,接口默认方法和静态方法、Lambda表达式和函数接口、方法引用、Stream。
一、接口默认方法和静态方法 jdk8之前,interface中变量必须是public、static、final,方法必须是public、abstract
@FunctionalInterface
public interface Test8 {
*//jdk8 默认public、abstrace***
**void interMethod();
*//jdk8 提供默认方法***
**default String defMethod(){
return "defMethod";
}
*//jdk8 提供static方法,Jdk8Interface.del()直接调用***
**static void statMethod(){
System.*out*.println("statMethod");
}
}
二、Lambda表达式和函数接口
函数式接口是指仅仅只包含一个抽象方法的接口,每一个该类型的lambda表达式都会被匹配到这个抽象方法,接口添加 @FunctionalInterface 注解定义为函数式接口
*//传统方式***
Test8 test1 = new Test8() {
@Override
public void interMethod() {
System.*out*.println("interMethod");
}
};
System.*out*.println(test1.defMethod());
*//lambda表达式***
Test8 test2 = () -> System.*out*.println("interMethod");
System.*out*.println(test2.defMethod());
*//lamdba更精简方式***
System.*out*.println(((Test8) () -> System.*out*.println("lambda")).defMethod());
public static void main(String[] args) {
ArrayList<UserEntity> userList = new ArrayList<>();
userList.add(new UserEntity("小明", 32));
userList.add(new UserEntity("小红", 13));
userList.add(new UserEntity("小黑", 22));
userList.sort(new Comparator<UserEntity>() {
@Override
public int compare(UserEntity o1, UserEntity o2) {
return o1.getAge() - o2.getAge();
}
});
//Comparator是函数接口***
userList.sort((o1, o2) -> o1.getAge() - o2.getAge());
userList.forEach(s -> System.*out*.printf(s.getName() + "-" + s.getAge()));
}
三、方法引用
需要结合lambda表达式让代码变得更加精简。
1、静态方法引入 类名::(静态方法)名称
public class Test8Lambda {
public static void main(String[] args) {
YouCanInterface youCanInterface1 = (i, j) -> Test8Lambda.*staticGet*(i, j);
System.*out*.println(youCanInterface1.get(1, 2));
*/*
** Test8Lambda::staticGet;***
** (i,j) -> Test8Lambda.staticGet(i,j);***
** 参数列表和返回类型都一致,所以可以直接使用***
YouCanInterface youCanInterface2 = Test8Lambda::*staticGet*;
System.*out*.println(youCanInterface2.get(1, 2));
}
public static String staticGet(int a, int b) {
return "静态get" + a + b;
}
}
@FunctionalInterface
public interface YouCanInterface {
String get(int i,int j);
}
2、对象方法引入 类名::实例方法名称
public class Test8Lambda {
public static void main(String[] args) {
YouCanInterface youCanInterface1 = (Test8Lambda test8Lambda) -> test8Lambda.objectGet();
System.*out*.println(youCanInterface1.get(new Test8Lambda()));
YouCanInterface youCanInterface2 = Test8Lambda::objectGet;
System.*out*.println(youCanInterface2.get(new Test8Lambda()));
}
public String objectGet() {
return "方法引入";
}
}
3、实例方法引入 new对象 对象实例::方法引入
public class Test8Lambda {
public static void main(String[] args) {
Test8Lambda test8Lambda = new Test8Lambda();
YouCanInterface youCanInterface1 = () -> test8Lambda.getString();
System.*out*.println(youCanInterface1.get());
YouCanInterface youCanInterface2 = test8Lambda::getString;
System.*out*.println(youCanInterface2.get());
}
public String getString() {
return "实例化get";
}
}
4、构造函数引入 类名::new
public static void main(String[] args) {
YouCanInterface youCanInterface1 = () -> new UserEntity();
System.*out*.println(youCanInterface1.get().getName());
YouCanInterface youCanInterface2 = UserEntity::new;
System.*out*.println(youCanInterface2.get().getName());
}
四、Stream 非常方便精简的方式遍历集合实现过滤、去重、排序等
流并不存储数据,所以它不是一个数据结构,它也不会修改底层的数据源,它为了函数式编程而生。
惰性执行的,例如filter,map等都是延迟执行的。流的中间操作总是惰性的。
流有可能是无限的。虽然集合具有有限的大小,但流不需要。短路操作,如limit(n)或findFirst(),允许在有限的时间内完成对无限流的计算。
流还是消耗品。在流的生命周期中,流的元素只被访问一次。与迭代器一样,必须生成新的流来重新访问源的相同元素。被访问过的流会被关闭。
*/*
** *collect转换* *Set*
**/
public static void main(String[] args) {
ArrayList<UserEntity> userList = new ArrayList<>();
userList.add(new UserEntity("小明",32));
userList.add(new UserEntity("小红",13));
userList.add(new UserEntity("小计",22));
*/**
** 串行流stream 单线程***
** 并行流parallelStream 多线程 (效率更高)***
**/*
Stream<UserEntity> stream = userList.stream();
*//转换成set集合***
Set<UserEntity> setUserList = stream.collect(Collectors.*toSet*());
setUserList.forEach(userEntity -> {
System.*out*.print(userEntity.getName());
});
}
*/*****
** collect转换Map***
**/***
public static void main(String[] args) {
ArrayList<UserEntity> userList = new ArrayList<>();
userList.add(new UserEntity("小明",32));
userList.add(new UserEntity("小红",13));
userList.add(new UserEntity("小计",22));
*//list转map key:username,value:userEntity***
**Stream<UserEntity> stream = userList.stream();
*/*****
** new Function* *<UserEntity* *(list集合类型),String(key)>***
** 第二个参数为参数的类型***
**/***
Map<String,UserEntity> collect = stream.collect(Collectors.*toMap*(new Function<UserEntity, String>() {
@Override
public String apply(UserEntity userEntity) {
return userEntity.getName();
}
}, new Function<UserEntity, UserEntity>(){
@Override
public UserEntity apply(UserEntity userEntity) {
return userEntity;
}
}));
collect.forEach(new BiConsumer(){
@Override
public void accept(Object o, Object o2) {
System.*out*.printf(o+","+o2);
}
});
*//lambda写法***
stream.collect(Collectors.*toMap*(userEntity -> userEntity.getName(),userEntity -> userEntity));
collect.forEach((o,o2) -> System.*out*.printf(o+","+o2));
}
*/*****
** reduce求和
**/***
public static void main(String[] args) {
Stream<Integer> integerStream = Stream.*of*(10, 50, 30, 10);
System.*out*.printf("和:"+integerStream.reduce((a1,a2) -> a1+a2).get());
}
*/*****
** max最大
**/***
public static void main(String[] args) {
ArrayList<UserEntity> userList = new ArrayList<>();
userList.add(new UserEntity("小明",32));
userList.add(new UserEntity("小红",13));
userList.add(new UserEntity("小计",22));
Stream<UserEntity> stream = userList.stream();
System.*out*.printf("最大"+stream.max((o1,o2) -> o1.getAge() - o2.getAge()).get().getAge());
}
*/*****
** anyMatch是否存在
**/***
public static void main(String[] args) {
ArrayList<UserEntity> userList = new ArrayList<>();
userList.add(new UserEntity("小明",32));
userList.add(new UserEntity("小红",13));
userList.add(new UserEntity("小计",22));
Stream<UserEntity> stream = userList.stream();
System.*out*.print(stream.anyMatch( userEntity -> userEntity.getName().equals("小红")));
}
*/*****
** filter过滤
**/***
public static void main(String[] args) {
ArrayList<UserEntity> userList = new ArrayList<>();
userList.add(new UserEntity("小明",32));
userList.add(new UserEntity("小红",13));
userList.add(new UserEntity("小计",22));
Stream<UserEntity> stream = userList.stream();
stream.filter( userEntity -> userEntity.getAge() > 13 && !userEntity.getName().equals("小计")).forEach(userEntity -> System.*out*.println(userEntity.getName()));
}
*/*****
** limit限制
**/***
public static void main(String[] args) {
ArrayList<UserEntity> userList = new ArrayList<>();
userList.add(new UserEntity("小明",32));
userList.add(new UserEntity("小红",13));
userList.add(new UserEntity("小计",22));
userList.add(new UserEntity("小思",22));
userList.add(new UserEntity("小五",22));
Stream<UserEntity> stream = userList.stream();
*//从skip开始,limit结束***
**stream.skip(1).limit(2).forEach(userEntity -> System.*out*.print(userEntity.getName()));
}
先总结这么多吧,之后再总结。。。