JDK9常用Jshell
- 什么是jshell?
从java9开始,jdk引入了交互式 REPL(Read-Eval-Print-Loop,读取-求值-输出-循环)
官方文档
- 常用命令
- 帮助命令
/help
/help intro
- 列出输入的源
/list
- 编辑某个源
/edit
- 删除
/drop
- 退出jshell命令
/exit
- 重置
/reset
- 查看历史编辑
/history
- 自动化补齐功能
Tab键
JDK9私有方法
- 什么是jdk9新增的接口私有方法
JDK8新增了静态方法和默认方法,但是不支持私有方法
jdk9中新增了私有方法
public interface OrderPay {
void pay();
default void defaultPay() {
privateMethod();
}
//接口的私有方法可以在JDK9中使用
private void privateMethod() {
System.out.println("调用接口的私有方法");
}
}
public class OrderPayImpl implements OrderPay {
@Override
public void pay() {
System.out.println("我实现了接口");
}
}
public static void main(String[] args) throws Exception {
OrderPay orderPay = new OrderPayImpl();
orderPay.defaultPay();
orderPay.pay();
}
- 注意点
- 接口中的静态态法不能被实现类继承和子接口继承,但是接口中的非静态的默认方法可以被实现类继承
- 例如List.of() 方法,ArrayList虽然继承了List,但是不能用ArrayList.of()方法
- 类的静态方法可以被继承
JDK9之增强try-with-resource
- 什么是try-with-resource
在JDK7中,新增了try-with-resources语句,可以在try后的括号中初始化资源,可以实现资 源自动关闭
OutputStream out = new FileOutputStream(filepath);
try(OutputStream temp = out;) {
temp.write((filepath+"可以学习java架构课程").getBytes());
}catch (Exception e){
e.printStackTrace();
}
- 什么是增强try-with-resource
在JDK9中,改进了try-with-resources语句,在try外进行初始化,在括号内引用,即可实现 资源自动关闭,多个变量则用分号进行分割
不需要声明资源 out 就可以使用它,并得到相同的结果
public static void main(String[] args) throws Exception {
String path = "/Users/vincent/Desktop/text.txt";
test(path);
}
private static void test(String filepath) throws FileNotFoundException {
OutputStream out = new FileOutputStream(filepath);
try (out) {
out.write((filepath + "可以学习java架构课程").getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
JDK9的Stream和集合API
- 什么是只读集合
集合只能读取,不能增加或者删除里面的元素
- JDK9之前创建只读集合
List<String> list = new ArrayList<>();
list.add("SpringBoot课程");
list.add("架构课程");
list.add("微服务SpringCloud课程");
//设置为只读List集合
list = Collections.unmodifiableList(list);
System.out.println(list);
Set<String> set = new HashSet<>();
set.add("Mysql教程");
set.add("Linux服务器教程");
set.add("Git教程");
//设置为只读Set集合
set = Collections.unmodifiableSet(set);
System.out.println(set);
Map<String, String> map = new HashMap<>();
map.put("key1", "课程1");
map.put("key2", "课程2");
//设置为只读Map集合
map = Collections.unmodifiableMap(map);
System.out.println(map);
- JDK9后创建只读集合
查看of()源码
List<String> list = List.of("SpringBoot课程", "架构课程", "微服务SpringCloud课程");
System.out.println(list);
Set<String> set = Set.of("Mysql教程", "Linux服务器教程", "Git教程");
System.out.println(set);
Map<String, String> map = Map.of("key1", "课程1", "key2", "课程2");
System.out.println(map);
- JDK8里面新增的Stream流式编程,方便了很多数据的处理
jdk9里面新增了部分API
- takeWhile
有序的集合:从 Stream 中获取一部分数据, 返回从头开始的尽可能多的元素, 直到遇到第一个false结果,如果第一个值不满足断言条件,将返回一个空的 Stream
List<String> list =List.of("springboot","java","html","","git").stream().takeWhile(obj-
>!obj.isEmpty()).collect(Collectors.toList());
//无序集合,返回元素不固定,暂无实际使用场景
Set<String> set =Set.of("springboot","java","html","","git").stream().takeWhile(obj-
>!obj.isEmpty()).collect(Collectors.toList());
- dropWhile
与 takeWhile相反,返回剩余的元素,和takeWhile方法形成互补
List<String> list =List.of("springboot","java","html","","git").stream().dropWhile(obj-
>!obj.isEmpty()).collect(Collectors.toList());
无序Stream里面也无实际使用场景