Guest User

Untitled

a guest
Aug 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. package norainu;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.OutputStreamWriter;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.time.LocalDateTime;
  9. import java.time.Year;
  10. import java.time.temporal.ChronoField;
  11. import java.util.Arrays;
  12. import java.util.Calendar;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import java.util.Optional;
  16. import java.util.stream.Stream;
  17.  
  18. public class Main {
  19.  
  20. public static void main(String[] args) throws Exception {
  21. System.out.println("aaaaa");
  22. Test t = new Test() {
  23. @Override
  24. public String getGreeting(String name) {
  25. if (name.isEmpty()) {
  26. return "Hello nobody!";
  27. }
  28. return "Hello " + name + "!";
  29.  
  30. }
  31. };
  32. System.out.println(t.getGreeting("aaaa"));
  33. // ラムダ
  34. Test t2 = (name) -> {
  35. if (name.isEmpty()) {
  36. return "h n";
  37. }
  38. return "h " + name;
  39. };
  40. System.out.println(t2.getGreeting("nnnn"));
  41. TestDefaultMethod tf = new TestDefaultMethod() {
  42. };
  43. System.out.println(tf.getName());
  44. TestDefaultMethod tf2 = new TestDefaultMethodImpl();
  45. System.out.println(tf2.getName());
  46.  
  47. // nullable
  48. System.out.println(getN("4").orElse("default").toUpperCase());
  49.  
  50. // 型推論
  51. var h = "あああああ";
  52. System.out.println(h);
  53.  
  54. // date and time api
  55. // 従来
  56. Calendar cal = Calendar.getInstance();
  57. System.out.println(cal.get(Calendar.YEAR));
  58.  
  59. System.out.println(Year.now());
  60. LocalDateTime d = LocalDateTime.now();
  61. //年月日時分秒を指定
  62. LocalDateTime d1 = LocalDateTime.of(2015, 12, 15, 23, 30, 59);
  63.  
  64. System.out.println(d.getYear());
  65. System.out.println(d.getMonth());
  66. System.out.println(d.getDayOfMonth());
  67. System.out.println(d.getHour());
  68. System.out.println(d.getMinute());
  69. System.out.println(d.getSecond());
  70. System.out.println(d.getNano());
  71. System.out.println(d.get(ChronoField.YEAR));
  72.  
  73. // Stream
  74. // 従来
  75. java.util.List<String> list = Arrays.asList("C", "C++", "Java", "Scala", "Ruby");
  76.  
  77. int count = 0;
  78. for (String elem : list) {
  79. if (elem.startsWith("C")) {
  80. count += elem.length();
  81. }
  82. }
  83. var count2 = list.stream()
  84. .filter(s -> s.startsWith("C"))
  85. .mapToInt(s -> s.length())
  86. .sum();
  87. System.out.println(count2);
  88.  
  89. // Stream with line
  90. Path path = Paths.get("C:/tmp/a.txt");
  91. try (Stream<String> lines = Files.lines(path); FileOutputStream out = new FileOutputStream("C:/tmp/a3.txt")) {
  92. OutputStreamWriter ow = new OutputStreamWriter(out, "UTF-8");
  93. // lines.forEach(System.out::println);
  94. lines.forEach((line -> {
  95. try {
  96. ow.write(line + "\n");
  97. } catch (Exception e) {
  98. // ここに適切な例外処理
  99. }
  100. }));
  101. ow.flush();
  102. }
  103.  
  104. }
  105.  
  106. private static Optional<String> getN(String key) {
  107. Map<String, String> dMap = new HashMap<>();
  108. dMap.put("1", "1111");
  109. dMap.put("2", "222");
  110. return Optional.ofNullable(dMap.get(key));
  111. }
  112. }
  113.  
  114. // ラムダ系
  115. @FunctionalInterface
  116. interface Test {
  117. public String getGreeting(String name);
  118. }
  119.  
  120. // デフォルトメソッド
  121. // っていうかこんなの許したら初心者混乱するのでは?
  122. interface TestDefaultMethod {
  123. public default String getName() {
  124. return "s";
  125. }
  126. }
  127.  
  128. class TestDefaultMethodImpl implements TestDefaultMethod {
  129.  
  130. }
Add Comment
Please, Sign In to add comment