随手记录第十二话 -- JDK8-21版本的新增特性记录(Lambda,var,switch,instanceof,record,virtual虚拟线程等)
创始人
2024-11-14 11:04:44
0

本文主要用于记录jdk8以来的新增特性及使用方法!

1.Java8 Lambda表达式(8)

1.1 方法引用

		List list = List.of("1", "2", "3");         list.forEach(i -> System.out.println(i));         //方法引用         list.forEach(System.out::println); 

1.2 接口支持默认方法

public interface MyTest {          default void defaultMethod(){         System.out.println("default Method");     } } 

1.3 Stream API流式API

		List list = List.of("1", "2", "3");         //过滤再循环         list.stream().filter(i -> !"1".equals(i)).forEach(System.out::println);         //过滤再输出         List list1 = list.stream().filter(i -> !"1".equals(i)).toList();         System.out.println(JSONObject.toJSONString(list1)); //["2","3"]         //过滤再转map         List> list2 = list.stream().filter(i -> !"1".equals(i)).map(s -> {             Map map = new HashMap<>();             map.put("a_" + s, s);             return map;         }).toList();         System.out.println(JSONObject.toJSONString(list2)); //[{"a_2":"2"},{"a_3":"3"}]         //转类型         List list3 = list.stream().map(Long::valueOf).toList();         System.out.println(JSONObject.toJSONString(list3)); //[1,2,3] 

1.4 Map新增且实用的方法

		Map map = new HashMap<>();         map.put("a", 1);         map.put("b", 2);         //foreach         map.forEach((key, value) -> System.out.println(key + ": " + value));         //getOrDefault 取值为空返回默认值         int c = map.getOrDefault("c", 22);         System.out.println(c); //22         //replace 如果存在值替换当前key的值         map.replace("b", 10);         System.out.println(map); //{a=1, b=10}         //替换时带匹配值         map.replace("b", 10, 20);         System.out.println(map); //{a=1, b=20}         //replaceAll 可对每个key或者value做操作         map.replaceAll((key, value) -> value + 1);         System.out.println(map); //{a=2, b=21}         //putIfAbsent key不存在才存入         map.putIfAbsent("a", 11);         map.putIfAbsent("c", 3);         System.out.println(map); //{a=2, b=21, c=3}         //computeIfAbsent 如果值不存在 则执行后面的函数         map.computeIfAbsent("d", key -> 4);         System.out.println(map); //{a=2, b=21, c=3, d=4}         //computeIfPresent 如果存在且值非空 可重新计算         map.computeIfPresent("b", (key, value) -> value + 10);         System.out.println(map); //{a=2, b=31, c=3, d=4}         //compute 使用函数计算存入         map.compute("b", (key, value) -> (value == null) ? 40 : value + 1);         map.compute("e", (key, value) -> (value == null) ? 40 : value + 1);         System.out.println(map); //{a=2, b=32, c=3, d=4, e=40}         //merge         map.merge("b", 1, (oldValue, newValue) -> oldValue + newValue);         System.out.println(map); //{a=2, b=33, c=3, d=4, e=40}          //remove 当指定key和值时才删除         map.remove("a", 1);         System.out.println(map); //{a=2, b=33, c=3, d=4, e=40}         map.remove("a", 2);         System.out.println(map); //{b=33, c=3, d=4, e=40}          //of ofEntries 快捷创建 但映射不可变         Map of = Map.of("a", 1, "b", 2);         System.out.println(of); //{b=2, a=1}         Map ofEntries = Map.ofEntries(                 Map.entry("a",1),                 Map.entry("b",2)         );         System.out.println(ofEntries); //{b=2, a=1} //        of.put("a",3); //不管新增删除还是修改 俊辉报错 

2. java11 var httpClient相关(11)

		var list = List.of("1", "2", "3");         list.forEach((var e) -> System.out.println(e));          String str = " ";         System.out.println(str.isBlank()); //true         System.out.println(str.isEmpty()); //false  		//java.net.http         JSONObject object = new JSONObject();         object.put("fromId", 11234);         var httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(6)).build();         HttpRequest httpRequest = HttpRequest.newBuilder(URI.create("http://192.168.168.199:9000/xxxxxx"))         //post body请求 //                .setHeader("Content-Type","application/json; charset=utf-8") //                .POST(HttpRequest.BodyPublishers.ofString(object.toJSONString()))                 .build();         HttpResponse httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofString());         System.out.println(httpResponse.body());           //okhttp3 http请求 //        Request request = new Request.Builder() //                .url(noticeUrl)                 .get() //                .post(RequestBody.create(object.toJSONString(), MediaType.parse("application/json; charset=utf-8"))) //                .build(); //        Response response = Web3jUtils.httpClient.newCall(request).execute(); //        JSONObject jsonObject = JSONObject.parseObject(response.body().string()); //        log.warn("发现提现消息:{},{}", object.toJSONString(), jsonObject.toJSONString()); 

3.新表达式和模块匹配(12-15)

3.1 switch 多行文本 instanceof

		int day = 1;         switch (day){             case 2 -> System.out.println(false);             case 1 -> System.out.println(true);             default -> System.out.println("未知的!");         } 		//多行文本         String str = """                 text                 text                 """;  		//instanceof         if(str instanceof String s1){             System.out.println(s1.toUpperCase(Locale.ROOT));         } 

4.记录 密封类 虚拟线程(16-21)

  • 记录
public record Point(int x, int y) {} 
  • 密封类
public abstract sealed class Shape permits Circle, Square {} public final class Circle extends Shape {} public final class Square extends Shape {} 
  • 虚拟线程
Thread.ofVirtual().start(() -> {     System.out.println("Hello from virtual thread"); });  //使用Executors 创建和管理 ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); for (int i = 0; i < 10; i++) {     executor.submit(() -> {         System.out.println("Hello from virtual thread!");     }); } executor.shutdown(); 

以上就是本文的全部内容了!

上一篇:随手记录第十一话 – PHP + yii2 初体验
下一篇:随手记录第十三话 – 关于Python自动化部署神器fabric的应用与差异

非学无以广才,非志无以成学。

上一篇:Kibana安装

下一篇:JAVA中的隐式参数super

相关内容

热门资讯

透视软件!aapoker真的假... 透视软件!aapoker真的假的(透视)辅助(一贯真的是有挂)1)aapoker真的假的辅助挂:进一...
透视肯定"sohoo... 透视肯定"sohoo poker辅助"一直存在有挂(透视)透明教程(有挂脚本)1、完成sohoo p...
透视脚本!wpk软件是正规的吗... 透视脚本!wpk软件是正规的吗,真是真的是有挂(透视)靠谱教程(有挂技巧)1)wpk软件是正规的吗辅...
透视软件!aapoker能控制... 透视软件!aapoker能控制牌吗(透视)脚本(切实有挂)1、aapoker能控制牌吗机器人多个强度...
透视透视"拱趴大菠萝... 透视透视"拱趴大菠萝机器人"其实是有挂(透视)必胜教程(有挂揭秘)1、用户打开应用后不用登录就可以直...
透视ai!wepoker怎么设... 透视ai!wepoker怎么设置透视,起初真的是有挂(透视)必赢教程(有挂辅助)1、wepoker怎...
透视肯定!aa poker透视... 透视肯定!aa poker透视软件(透视)透视方法(都是真的有挂)1、该软件可以轻松地帮助玩家将aa...
透视总结"hhpok... 透视总结"hhpoker辅助"都是存在有挂(透视)插件教程(有挂插件)1、hhpoker辅助透视辅助...
透视安装!wepoker透视脚... 透视安装!wepoker透视脚本下载,一贯真的是有挂(透视)微扑克教程(有挂详情)1、实时wepok...
透视中牌率!aapoker透视... 透视中牌率!aapoker透视怎么用(透视)透视插件(总是真的是有挂)1、起透看视 aapoker透...