Java 構文5

HashMap

import java.util.*;

public class MyApp{
  public static void main(String[] args){
      HashMap<String, Integer> sales = new HashMap<>();

      sales.put("tom", 10);
      sales.put("john", 20);
      sales.put("mason", 30);

          System.out.println(sales.get("tom"));
          System.out.println(sales.size());

          for (Map.Entry<String, Integer> sale: sales.entrySet()){
            System.out.println(sale.getKey() + ":" + sale.getValue());
          }
    }
}

StreamAPI

import java.util.*;

public class MyApp{
  public static void main(String[] args){

      List<Integer> sales = new ArrayList<>(Arrays.asList(12, 30, 22, 4, 9));
      sales
       .stream()
       // 中間処理
       .filter(e -> e % 3 == 0)
       // 終端処理
       .map(e -> "(" + e+ ")")
       .forEach(System.out::println);
    }
}

LocalDateTime

import java.time.*;
import java.time.format.DateTimeFormatter;

public class MyApp{
  public static void main(String[] args){
      LocalDateTime d = LocalDateTime.now();
      // LocalDateTime d = LocalDateTime.of(2017, 1, 1, 10, 10, 10);
      // LocalDateTime d = LocalDateTime.parse("2016-12-12T10:10:10");

      System.out.println(d.getYear());
      System.out.println(d.getMonth());
      System.out.println(d.getMonth().getValue());

      System.out.println(d.plusMonths(2).minusDays(3));
      DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy!MM!dd!");
      System.out.println(d.format(dtf));
    }
}