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();
(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());
(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);
(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()));
(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..."); } }
■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());
(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);
(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"; };
■Jdk11〜17、 追加のクラス又は新しい機能
(1)文本処理の更新
//Before 17 String str = "Hello World! " + " zhang 3 "+ " li 4 "; //After 17 String str = """ Hello World! zhang 3 li 4 """;
(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); } }
(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 {}
(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()); }
(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
end