高级农民工(H.F.H)
我思,故我在!(僕なら、僕ができる!)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

Start

 ■Jdk1.6〜8、追加のクラス又は新しい機能

(1)Lambda

复制代码
// JDK 1.6
new Thread(new Runnable() {
    public void run() {
        System.out.println("Hello, World!");
    }
}).start();

// JDK 8
new Thread(() -> System.out.println("Hello, World!")).start();
Sample
复制代码

(2)Stream API

复制代码
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");

// JDK 1.6
List<String> filteredNames = new ArrayList<>();
for (String name : names) {
    if (name.length() > 3) {
        filteredNames.add(name.toUpperCase());
    }
}

// JDK 8
List<String> filteredNames = names.stream()
    .filter(name -> name.length() > 3)
    .map(String::toUpperCase)
    .collect(Collectors.toList());
Sample
复制代码

(3)Java.time

复制代码
// JDK 1.6
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = sdf.parse("2020-01-01");
Date endDate = sdf.parse("2020-12-31");
long days = (endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24);

// JDK 8
LocalDate startDate = LocalDate.of(2020, 1, 1);
LocalDate endDate = LocalDate.of(2020, 12, 31);
long days = ChronoUnit.DAYS.between(startDate, endDate);
Sample
复制代码

(4)Optional

复制代码
// JDK 1.6
String value = map.get("key");
if (value != null) {
    System.out.println(value.toUpperCase());
}

// JDK 8
Optional<String> value = Optional.ofNullable(map.get("key"));
value.ifPresent(v -> System.out.println(v.toUpperCase()));
Sample
复制代码

(5)インタフェースのディフォルト関数(又は静的関数)を追加

复制代码
// JDK 1.6
public interface MyInterface {
    void doSomething();
}

// JDK 8
public interface MyInterface {
    void doSomething();

    default void doSomethingElse() {
        System.out.println("Doing something else...");
    }
}
Sample
复制代码

 

■Jdk8〜11、 追加のクラス又は新しい機能

(1)HttpClient APIの追加

・HttpClient:請求と応答の発送

・HttpRequest:請求

・HttpResponse:応答

・WebSocket:通信の処理

复制代码
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://example.com"))
        .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
Sample
复制代码

(2)Lambdaの更新(varの利用)

复制代码
// Before Java 11
BiConsumer<String, String> concat = (String a, String b) -> System.out.println(a + b);

// With Java 11
BiConsumer<String, String> concat = (var a, var b) -> System.out.println(a + b);
Sample
复制代码

(3)Switchの更新

复制代码
// Before Java 12
String day = "MONDAY";
String result;
switch (day) {
    case "MONDAY":
        result = "Start of the week";
        break;
    case "TUESDAY":
        result = "Second day";
        break;
    default:
        result = "Other day";
}

// With Java 12
String day = "MONDAY";
String result = switch (day) {
    case "MONDAY" -> "Start of the week";
    case "TUESDAY" -> "Second day";
    default -> "Other day";
};
Sample
复制代码

 

■Jdk11〜17、 追加のクラス又は新しい機能

(1)文本処理の更新

复制代码
//Before 17
String str = "Hello World! " +
                  " zhang 3 "+
                  " li 4 ";


//After 17
String str = """
                  Hello World!
                  zhang 3
                  li 4
                  """;
Sample
复制代码

(2)キー「record」の追加(Lombokの切り替え)

复制代码
public record SampleRecord(String name, String age) {
}

public class SampleMain {
    public static void main(String[] args) {
        SampleRecord record1 = new SampleRecord("zhang 3",12);
        SampleRecord record2 = new SampleRecord("li 4",15);
        SampleRecord record3 = new SampleRecord("wang 5",18);
    }
}
Sample
复制代码

(3)キー「Sealed」の追加

复制代码
sealed abstract class A
    permits B, C {}

final class B extends A {
    // ...
}

final class C extends A {
    // ...
}

// This class would not be allowed to extend Shape
// class D extends A {}
Sample
复制代码

(4)instanceofの更新

复制代码
Object obj = "Hello, World!";

// Before Java 16
if (obj instanceof String) {
    String str = (String) obj;
    System.out.println(str.length());
}

// With Java 16
if (obj instanceof String str) {
    System.out.println(str.length());
}
Sample
复制代码

(5)Switchの再更新

复制代码
sealed interface Shape permits Circle, Rectangle {}

record Circle(int radius) implements Shape {}
record Rectangle(int width, int height) implements Shape {}

Shape shape = new Circle(5);

// With Java 17
String result = switch (shape) {
    case Circle(var radius) -> "Circle with radius " + radius;
    case Rectangle(var width, var height) -> "Rectangle with width " + width + " and height " + height;
    default -> "Unknown shape";
};

System.out.println(result); // Circle with radius 5
Sample
复制代码

end

posted on   农民工024  阅读(16)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
历史上的今天:
2019-02-07 【JavaScript】EasyUIのForm的跨域提交问题解析
 
点击右上角即可分享
微信分享提示