接口性能优化指南、函数式接口案例、JDK9-JDK13新特性

一、接口性能优化指南

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");
        }
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值