一、接口性能优化指南
1.代码是不是渣? 代码逻辑梳理与重构,去掉不必要的逻辑处理和外部查询
2.SQL是不是烂?所写SQL是否缺乏优化,比如涉及Full Scan全表扫描
3.需要进程缓存? 进程缓存:EhCache LoadingCache Caffeine
4.需要远程缓存? Redis,MemCache
二、函数式接口案例
@FunctionalInterface
public interface BiFunction<T,U,R> {
R apply(T t,U u);
}
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
@SneakyThrows
public static void main(String[] args) {
System.out.println(operator(10,21,(a,b)->a+b));
System.out.println(operator(10,2,(a,b)->a-b));
System.out.println(operator(8,4,(a,b)->a*b));
System.out.println(operator(10,2,(a,b)->a/b));
Consumer<String> consumer = obj -> {
System.out.println("发送短信" + obj);
};
consumer.accept("8888888");
send("11111111@qq.com", obj->{
System.out.println("发送邮件" + obj);
});
}
方法:
public static Integer operator(Integer a, Integer b, BiFunction<Integer, Integer, Integer> bf){
return bf.apply(a, b);
}
private static void send(String emailAndPhone, Consumer<String> consumer){
consumer.accept(emailAndPhone);
}
三、JDK9-JDK13新特性
1.jshell
从java9开始,jdk引⼊了交互式 REPL(Read-Eval-Print-Loop
,读取-求值-输出-循环)
官⽅⽂档:https://docs.oracle.com/en/java/javase/12/jshell/introduction-jshell.html#GUID-63
0F27C8-1195-4989-9F6B-2C51D46F52C8
2.增强try-with-resource
public static void main(String[] args) throws Exception {
String path = "/Desktop/t.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();
}
}
3.创建只读集合
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", "key2");
System.out.println(map);
4.局部变量推断var
//根据推断为 字符串类型
var strVar = "springboot";
System.out.println(strVar instanceof String);
//根据10L 推断long 类型
var longVar = 10L;
//根据 true推断 boolean 类型
var flag = true;
//var flag = Boolean.valueOf("true");
//System.out.println(flag instanceof Boolean);
// 推断 ArrayList<String>
var listVar = new ArrayList<String>();
System.out.println(listVar instanceof ArrayList);
// 推断 Stream<String>
var streamVar = Stream.of("aa", "bb", "cc");
System.out.println(streamVar instanceof Stream)
5.jdk11
后运⾏java程序(本地不会⽣成class⽂件)
public class Main {
public static void main(String[] args) throws Exception {
String text = "测试";
//String类新增repeat(int num) ⽅法,⽤于字符串循环输出
System.out.println(text.repeat(2));
}
}
6.增强switch
表达式
使⽤箭头函数,不⽤声明break
,会⾃动终⽌,⽀持多个值匹配,使⽤逗号分隔
public void testNewSwitch(int i){
switch(i){
case 0 -> {
System.out.println("zero");
System.out.println("这是多⾏语句");
}
case 1,11,111 -> System.out.println("one");
case 2 -> System.out.println("two");
default -> System.out.println("default");
}
}