Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package norainu;
- import java.io.FileOutputStream;
- import java.io.OutputStreamWriter;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.time.LocalDateTime;
- import java.time.Year;
- import java.time.temporal.ChronoField;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Optional;
- import java.util.stream.Stream;
- public class Main {
- public static void main(String[] args) throws Exception {
- System.out.println("aaaaa");
- Test t = new Test() {
- @Override
- public String getGreeting(String name) {
- if (name.isEmpty()) {
- return "Hello nobody!";
- }
- return "Hello " + name + "!";
- }
- };
- System.out.println(t.getGreeting("aaaa"));
- // ラムダ
- Test t2 = (name) -> {
- if (name.isEmpty()) {
- return "h n";
- }
- return "h " + name;
- };
- System.out.println(t2.getGreeting("nnnn"));
- TestDefaultMethod tf = new TestDefaultMethod() {
- };
- System.out.println(tf.getName());
- TestDefaultMethod tf2 = new TestDefaultMethodImpl();
- System.out.println(tf2.getName());
- // nullable
- System.out.println(getN("4").orElse("default").toUpperCase());
- // 型推論
- var h = "あああああ";
- System.out.println(h);
- // date and time api
- // 従来
- Calendar cal = Calendar.getInstance();
- System.out.println(cal.get(Calendar.YEAR));
- System.out.println(Year.now());
- LocalDateTime d = LocalDateTime.now();
- //年月日時分秒を指定
- LocalDateTime d1 = LocalDateTime.of(2015, 12, 15, 23, 30, 59);
- System.out.println(d.getYear());
- System.out.println(d.getMonth());
- System.out.println(d.getDayOfMonth());
- System.out.println(d.getHour());
- System.out.println(d.getMinute());
- System.out.println(d.getSecond());
- System.out.println(d.getNano());
- System.out.println(d.get(ChronoField.YEAR));
- // Stream
- // 従来
- java.util.List<String> list = Arrays.asList("C", "C++", "Java", "Scala", "Ruby");
- int count = 0;
- for (String elem : list) {
- if (elem.startsWith("C")) {
- count += elem.length();
- }
- }
- var count2 = list.stream()
- .filter(s -> s.startsWith("C"))
- .mapToInt(s -> s.length())
- .sum();
- System.out.println(count2);
- // Stream with line
- Path path = Paths.get("C:/tmp/a.txt");
- try (Stream<String> lines = Files.lines(path); FileOutputStream out = new FileOutputStream("C:/tmp/a3.txt")) {
- OutputStreamWriter ow = new OutputStreamWriter(out, "UTF-8");
- // lines.forEach(System.out::println);
- lines.forEach((line -> {
- try {
- ow.write(line + "\n");
- } catch (Exception e) {
- // ここに適切な例外処理
- }
- }));
- ow.flush();
- }
- }
- private static Optional<String> getN(String key) {
- Map<String, String> dMap = new HashMap<>();
- dMap.put("1", "1111");
- dMap.put("2", "222");
- return Optional.ofNullable(dMap.get(key));
- }
- }
- // ラムダ系
- @FunctionalInterface
- interface Test {
- public String getGreeting(String name);
- }
- // デフォルトメソッド
- // っていうかこんなの許したら初心者混乱するのでは?
- interface TestDefaultMethod {
- public default String getName() {
- return "s";
- }
- }
- class TestDefaultMethodImpl implements TestDefaultMethod {
- }
Add Comment
Please, Sign In to add comment