java9 新機能

2017年9月22日「Java9」正式リリース

コレクション生成

public static void main(String[] args) {
    List<String> list = List.of("a", "b", "c", "d");        
    System.out.println(list);
}


クラスダイアモンド

List<String> list = new ArrayList<>();


try-with-resources

void testARM_Java9() throws IOException{
 BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt"));
 try (reader1) {
   System.out.println(reader1.readLine());
 }
}


takeWhile

  List<String> list = Arrays.asList("a", "b", "c", "d",); 
  String str = "c";
  list.stream().takeWhile(x -> x!= str).forEach(System.out::println);


dropWhile

List<String> list = Arrays.asList("a", "b", "c", "d",);
String str = "c";
list.stream().dropWhile(x -> x!= str).forEach(System.out::println);


 

ofNullable

    public static void main(String[] args) {
        List<String> list = Arrays.asList("Tokyo", "Oosaka", "Nagoya");
        Map<String, String> map = new HashMap<>() {
            {
                put("Tokyo", "東京");
                put("Osaka", "大阪");
                put("Nagoya", "名古屋");
            }
        };
         
        list.stream().flatMap(x -> Stream.ofNullable(map.get(x))).forEach(System.out::println);
    }


   

ifPresentOrElse

    public static void main(String[] args) {
        String str = null;
        Optional<String> value = Optional.ofNullable(str);
        value.ifPresentOrElse(
            System.out::println,
            () -> System.out.println("nullです"));
    }


 

or

    public static void main(String[] args) {
        String str = null;
        Optional<String> value = Optional.ofNullable(str);
        System.out.println(value.or(() -> Optional.of("nullです")).get());
    }


 

stream

    public static void main(String[] args) {
        List<String> list1 = List.of("Tokyo", "Osaka");
        List<String> list2 = null;
        
        Optional<List<String>> value1 = Optional.ofNullable(list1);
        Optional<List<String>> value2 = Optional.ofNullable(list2);
        
        value1.stream().flatMap(x -> x.stream()).forEach(System.out::println);
        value2.stream().flatMap(x -> x.stream()).forEach(System.out::println);
    }


   

Deprecated アノテーション

class ClassSample{
    private int num = 0;
    public ClassSample(int num){
        this.num = num;
    }
    @Deprecated(
        forRemoval = false, // 将来削除されるか?
        since = "0.9.1") // いつから非推奨に?
    public int get_num(){
        return num;
    }
    public int getNum(){
        return num;
    }
}
 
public class Main {
 
    public static void main(String[] args) {
 
        ClassSample cs = new ClassSample(1);
        System.out.println("numは " + cs.get_num() + "です");
        System.out.println("numは " + cs.getNum() + "です");
 
    }
 
}
class ClassSample{
    private int num = 0;
    public ClassSample(int num){
        this.num = num;
    }
    @Deprecated(
        forRemoval = false, // 将来削除されるか?
        since = "0.9.1") // いつから非推奨に?
    public int get_num(){
        return num;
    }
    public int getNum(){
        return num;
    }
}
 
public class Main {
 
    public static void main(String[] args) {
 
        ClassSample cs = new ClassSample(1);
        System.out.println("numは " + cs.get_num() + "です");
        System.out.println("numは " + cs.getNum() + "です");
 
    }
 
}


   

Interface内のprivateメソッド

public interface Card{
  private Long createCardID();
  private static void displayCardDetails();
}


 

Process APIの改善

 ProcessHandle currentProcess = ProcessHandle.current();
 System.out.println("Current Process Id: = " + currentProcess.getPid());


 

CompletableFuture APIの改善

Executor exe = CompletableFuture.delayedExecutor(50L, TimeUnit.SECONDS);


 

Optionalクラスの改善

Stream<Optional> emp = getEmployee(id)
Stream empStream = emp.flatMap(Optional::stream)