1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Java11 備忘録

Posted at

自分用

1. ローカル変数の型推論

変数宣言

var number = 42;
var pi = 3.14;
var grade = 'A';
var isJavaFun = true;

コレクション

var myList = List.of("Java", "is", "awesome");
var myMap = Map.of("key1", 1, "key2", 2);

2. ストリームAPIの新機能

takeWhileメソッドとdropWhileメソッド

List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6);

// takeWhile: 条件を満たす間だけ要素を取得
List<Integer> result1 = numbers.stream()
                               .takeWhile(n -> n < 4)
                               .collect(Collectors.toList());
// 結果: [1, 2, 3]

// dropWhile: 条件を満たす間は要素をスキップ
List<Integer> result2 = numbers.stream()
                               .dropWhile(n -> n < 4)
                               .collect(Collectors.toList());
// 結果: [4, 5, 6]

3. HTTPクライアントの導入

HttpClient client = HttpClient.newHttpClient();

HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("https://api.example.com/data"))
        .GET()
        .build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

System.out.println("Response Code: " + response.statusCode());
System.out.println("Response Body: " + response.body());

4. メソッドの新機能

インターフェースのprivateメソッド

public interface MyInterface {
    default void publicMethod() {
        privateMethod();
        System.out.println("Public Method");
    }

    private void privateMethod() {
        System.out.println("Private Method");
    }
}

ローカルメソッド

public void myMethod() {
    String localVar = "Local Variable";

    // ローカルメソッド
    void printLocalVar() {
        System.out.println(localVar);
    }

    printLocalVar();
}

5. 文字列の新機能

文字列の連結

String firstName = "John";
String lastName = "Doe";

String fullName = firstName + " " + lastName; // "John Doe"

文字列の繰り返し

String repeatedText = "Java ".repeat(3); // "Java Java Java "

6. マルチスレッド処理

フォーク/ジョインフレームワーク

ForkJoinPool forkJoinPool = new ForkJoinPool();

int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

int result = forkJoinPool.invoke(new MyRecursiveTask(array, 0, array.length));

System.out.println("Sum: " + result);

7. モジュールシステム

モジュールの定義

module com.example.mymodule {
    requires java.base;
    exports com.example.mypackage;
}

モジュールの依存関係

module com.example.myapp {
    requires com.example.mymodule;
}

8. ガベージコレクションの新機能

Epsilonガベージコレクタ

// ガベージコレクションの無効化
java -XX:+UseEpsilonGC MyApp.java

Z Garbage Collector

// ZGCの有効化
java -XX:+UseZGC MyApp.java

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?