ArrayList
import java.util.*; public class MyApp { public static void main(String[] args){ List<Integer> sales = new ArrayList<>(); sales.add(10); sales.add(20); sales.add(30); for (int i = 0; i < sales.size(); i++){ System.out.println(sales.get(i)); } sales.set(0, 100); sales.remove(2); for (Integer sale: sales){ System.out.println(sale); } } }
HashSet // 重複しない、順番が定まらない
L TreeSet, LinkedHashSet
public static void main(String[] args){ // HashSet<Integer> = new HashSet<>(); Set<Integer> sales = new HashSet<>(); sales.add(10); sales.add(20); sales.add(30); sales.add(10); System.out.println(sales.size()); for(Integer sale:sales){ System.out.println(sale); } sales.remove(30); for(Integer sale:sales){ System.out.println(sale); } }
HashMap
public class MyApp { public static void main(String[] args){ // HashMap: key value // HashMap<String, Integer> sales = new HashMap<>(); Map<String, Integer> sales = new HashMap<>(); sales.put("tom", 10); sales.put("bob", 20); sales.put("steve", 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()); } sales.put("tom", 100); sales.remove("steve"); for(Map.Entry<String, Integer> sale: sales.entrySet()){ System.out.println(sale.getKey() + ":" + sale.getValue()); } } }
StreamAPI
public static void main(String[] args){ List<Integer> sales = new ArrayList<>(Arrays.asList(12, 30, 22, 4, 9)); // for(Integer sale: sales){ // System.out.println(sale); // } sales .stream() .filter(e-> e % 3 == 0) .map(e-> "(" + e + ")") .forEach(System.out::println); }
LocalDateTime class
import java.time.*; import java.time.format.DateTimeFormatter; public class MyApp { public static void main(String[] args){ LocalDateTime d = LocalDateTime.now(); System.out.println(d.getYear()); 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)); } }