Contenu connexe
Similaire à Java9を迎えた今こそ!Java本格(再)入門 (20)
Java9を迎えた今こそ!Java本格(再)入門
- 2. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
2
Java
Chapter 7
Chapter 8
Chapter 9
Chapter 12
Acroquest Technology
- 2017 3
2Twitter: @omochiya
-
- 5. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1.Introduction
2.Java 1.4 -> 5.0
3.Java 5.0 -> 7
4.Java 7 -> 8
5.Java 8 -> 9
6.Java 9 -> Future
5
- 8. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
8
CSV File
2017-09-25,100,123456,Acroquest,
2017-10-02,100,-28980, ,
2017-10-03,100,-17712, ,
2017-10-20,100,3000000,gihyo,
2017-10-25,100,200000,Acroquest,
, , , ,
etc
- 10. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
10
List read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List list = new ArrayList();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = df.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
list.add(tx);
}
} catch (ParseException e) {
throw new RuntimeException("failed to parse date", e);
} catch (IOException e) {
throw new RuntimeException("failed to read file: " + fileName, e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
// ignore
}
}
return list;
}
CSV
- 11. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
11
List read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List list = new ArrayList();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "UTF-8"));
CSV
- 12. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
2/3
12
String line;
while ((line = reader.readLine()) != null) {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = df.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
list.add(tx);
}
CSV
- 13. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
3/3
13
} catch (ParseException e) {
throw new RuntimeException("failed to parse date", e);
} catch (IOException e) {
throw new RuntimeException("failed to read file: " + fileName, e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
// ignore
}
}
return list;
}
finally close
CSV
- 14. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction
14
class Transaction {
Date date;
String accountId;
Integer amount;
String name;
String note;
public Transaction() {
}
public Transaction(Date date, String accountId, Integer amount, String name, String note) {
this.date = date;
this.accountId = accountId;
this.amount = amount;
this.name = name;
this.note = note;
}
public String toString() {
return date + " " + accountId + " " + amount + " " + name + " " + note;
}
}
CSV
- 15. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction 1/2
15
class Transaction {
Date date;
String accountId;
Integer amount;
String name;
String note;
public Transaction() {
}
CSV
- 16. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction 2/2
16
public Transaction(Date date, String accountId, Integer amount,
String name, String note) {
this.date = date;
this.accountId = accountId;
this.amount = amount;
this.name = name;
this.note = note;
}
public String toString() {
return date + " " + accountId + " " + amount + " " + name + " " + note;
}
}
CSV
- 17. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
-
17
List list = read(fileName);
int sum = 0;
for (int i = 0; i < list.size(); i++) {
Transaction tx = (Transaction) list.get(i);
sum += tx.amount;
}
System.out.println(sum);
CSV
- 18. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
- 6
18
List list = read(fileName);
for (int i = 0; i < list.size(); i++) {
Transaction tx = (Transaction) list.get(i);
Calendar cal = Calendar.getInstance();
cal.setTime(tx.date);
if (cal.get(Calendar.MONTH) == 5) {
System.out.println(tx);
}
}
0
CSV
- 22. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
22
List read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List list = new ArrayList();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "UTF-8"));
CSV
- 23. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
23
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<Transaction>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "UTF-8"));
CSV
- 24. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
24
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<Transaction>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "UTF-8"));
Generics
→
→
CSV
- 25. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
-
25
List list = read(fileName);
int sum = 0;
for (int i = 0; i < list.size(); i++) {
Transaction tx = (Transaction) list.get(i);
sum += tx.amount;
}
System.out.println(sum);
CSV
- 26. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
-
26
List<Transaction> list = read(fileName);
int sum = 0;
for (Transaction tx : list) {
sum += tx.amount;
}
System.out.println(sum);
CSV
- 27. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
-
27
List<Transaction> list = read(fileName);
int sum = 0;
for (Transaction tx : list) {
sum += tx.amount;
}
System.out.println(sum);
CSV
for
→
- 29. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction 2/2
29
public Transaction(Date date, String accountId, Integer amount,
String name, String note) {
this.date = date;
this.accountId = accountId;
this.amount = amount;
this.name = name;
this.note = note;
}
public String toString() {
return date + " " + accountId + " " + amount + " " + name + " " + note;
}
}
CSV
- 30. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction 2/2
30
public Transaction(Date date, String accountId, Integer amount,
String name, String note) {
this.date = date;
this.accountId = accountId;
this.amount = amount;
this.name = name;
this.note = note;
}
@Override
public String toString() {
return date + " " + accountId + " " + amount + " " + name + " " + note;
}
}
CSV
- 31. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction 2/2
31
public Transaction(Date date, String accountId, Integer amount,
String name, String note) {
this.date = date;
this.accountId = accountId;
this.amount = amount;
this.name = name;
this.note = note;
}
@Override
public String toString() {
return date + " " + accountId + " " + amount + " " + name + " " + note;
}
}
→ Override
CSV
- 36. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
36
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<Transaction>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fileName), "UTF-8"));
CSV
- 37. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
37
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader =
Files.newBufferedReader(Paths.get(fileName),
StandardCharsets.UTF_8)) {
CSV
- 38. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
38
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader =
Files.newBufferedReader(Paths.get(fileName),
StandardCharsets.UTF_8)) {
CSV
- 39. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
39
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader =
Files.newBufferedReader(Paths.get(fileName),
StandardCharsets.UTF_8)) {
try-with-resoureces
try ( ) close
→
→
CSV
- 41. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
41
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader =
Files.newBufferedReader(Paths.get(fileName),
StandardCharsets.UTF_8)) {
Files nio2
CSV
- 42. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1/3
42
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader =
Files.newBufferedReader(Paths.get(fileName),
StandardCharsets.UTF_8)) {
String Charset
StandardCharsets
→
CSV
- 43. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
3/3
43
} catch (ParseException e) {
throw new RuntimeException("failed to parse date", e);
} catch (IOException e) {
throw new RuntimeException("failed to read file: " + fileName, e);
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
// ignore
}
}
return list;
}
CSV
- 44. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
3/3
44
} catch (ParseException | IOException e) {
throw new RuntimeException("failed to parse date or read file”, e);
}
return list;
}
catch
CSV
- 51. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction toString
51
class Transaction {
Date date;
String accountId;
Integer amount;
String name;
String note;
public Transaction() {
}
public Transaction(Date date, String accountId, Integer amount, String name, String note)
{
this.date = date;
this.accountId = accountId;
this.amount = amount;
this.name = name;
this.note = note;
}
CSV
- 52. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction toString
52
class Transaction {
LocalDate date;
String accountId;
Integer amount;
String name;
String note;
public Transaction() {
}
public Transaction(LocalDate date, String accountId, Integer amount, String name, String
note) {
this.date = date;
this.accountId = accountId;
this.amount = amount;
this.name = name;
this.note = note;
}
CSV
- 53. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
Transaction toString
53
class Transaction {
LocalDate date;
String accountId;
Integer amount;
String name;
String note;
public Transaction() {
}
public Transaction(LocalDate date, String accountId, Integer amount, String name, String
note) {
this.date = date;
this.accountId = accountId;
this.amount = amount;
this.name = name;
this.note = note;
}
Date and Time API
→LocalDate
LocalDateTime LocalTime
OffsetDateTime ZonedDateTime
CSV
- 55. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
55
List<Transaction> read(String fileName) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName),
StandardCharsets.UTF_8)) {
String line;
while ((line = reader.readLine()) != null) {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = df.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
list.add(tx);
}
CSV
- 56. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
56
List<Transaction> read(String fileName) {
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = LocalDate.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
list.add(tx);
}
CSV
- 57. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
57
List<Transaction> read(String fileName) {
// SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = LocalDate.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
list.add(tx);
}
UTF-8 Charset
ISO 8601
CSV
- 59. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
- 6
59
List<Transaction> list = read(fileName);
for (Transaction tx : list) {
Calendar cal = Calendar.getInstance();
cal.setTime(tx.date);
if (cal.get(Calendar.MONTH) == 5) {
System.out.println(tx);
}
}
CSV
- 60. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
- 6
60
List<Transaction> list = read(fileName);
for (Transaction tx : list) {
if (tx.date.getMonth() == Month.JUNE) {
System.out.println(tx);
}
}
CSV
- 61. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
- 6
61
List<Transaction> list = read(fileName);
for (Transaction tx : list) {
if (tx.date.getMonth() == Month.JUNE) {
System.out.println(tx);
}
}
enum
enum
→ 0
CSV
- 66. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
66
List<Transaction> list = new ArrayList<>();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = LocalDate.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
list.add(tx);
}
} catch (ParseException | IOException e) {
throw new RuntimeException("failed to parse date or read file”, e);
}
return list;
CSV
- 67. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
67
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
return stream
.map(line -> {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = LocalDate.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
return tx;
})
.collect(Collectors.toList());
} catch (ParseException | IOException e) {
throw new UncheckedIOException("failed to read file", e);
}
CSV
- 68. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
68
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
return stream
.map(line -> {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = LocalDate.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
return tx;
})
.collect(Collectors.toList());
} catch (ParseException | IOException e) {
throw new UncheckedIOException("failed to read file", e);
}
Stream
List
CSV
String Transaction
- 69. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
69
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
return stream
.map(line -> {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = LocalDate.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
return tx;
})
.collect(Collectors.toList());
} catch (ParseException | IOException e) {
throw new UncheckedIOException("failed to read file", e);
}
Lambda
CSV
- 70. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1
70
//
List<Transaction> list = read(fileName);
//
for (Transaction tx : list) {
System.out.println(tx);
}
//
int sum = 0;
for (Transaction tx : list) {
sum += tx.amount;
}
System.out.println(sum);
CSV
- 71. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1
71
//
read(fileName).stream()
.forEach(System.out::println);
//
int sum = read(fileName).stream()
.mapToInt(tx -> tx.amount)
.sum();
System.out.println(sum);
CSV
- 72. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1
72
//
read(fileName).stream()
.forEach(System.out::println);
//
int sum = read(fileName).stream()
.mapToInt(tx -> tx.amount)
.sum();
System.out.println(sum);
int
CSV
- 73. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1
73
//
read(fileName).stream()
.forEach(System.out::println);
//
int sum = read(fileName).stream()
.mapToInt(tx -> tx.amount)
.sum();
System.out.println(sum);
Lambda
CSV
- 74. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
2
74
// 6
for (Transaction tx : list) {
if (tx.date.getMonth() == Month.JUNE) {
System.out.println(tx);
}
}
CSV
- 75. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
2
75
// 6
read(fileName).stream()
.filter(tx -> tx.date.getMonth() == Month.JUNE)
.forEach(System.out::println);
CSV
- 76. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
2
76
// 6
read(fileName).stream()
.filter(tx -> tx.date.getMonth() == Month.JUNE)
.forEach(System.out::println);
6
filter
CSV
- 77. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
2
77
// 6
read(fileName).stream()
.filter(tx -> tx.date.getMonth() == Month.JUNE)
.forEach(System.out::println);
Lambda
- 90. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
90
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
return stream
.map(line -> {
String[] s = line.split(",");
Transaction tx = new Transaction();
tx.date = LocalDate.parse(s[0]);
tx.accountId = s[1];
tx.amount = Integer.valueOf(s[2]);
tx.name = s[3];
tx.note = s[4];
return tx;
})
.collect(Collectors.toList());
} catch (ParseException | IOException e) {
throw new UncheckedIOException("failed to read file", e);
}
String Transaction
List
- 91. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
1
91
//
read(fileName).stream()
.forEach(System.out::println);
//
int sum = read(fileName).stream()
.mapToInt(tx -> tx.amount)
.sum();
System.out.println(sum);
int
- 92. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
2
92
// 6
read(fileName).stream()
.filter(tx -> tx.date.getMonth() == Month.JUNE)
.forEach(System.out::println);
6
filter
- 102. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
JShell
102
//
jshell> List<Integer> intList = List.of(1, 2, 3);
intList ==> [1, 2, 3]
jshell> List<String> strList = List.of("aaa,", "bbb", “ccc");
strList ==> [aaa,, bbb, ccc]
jshell> intList.add(4);
| java.lang.UnsupportedOperationException thrown:
| at ImmutableCollections.uoe (ImmutableCollections.java:71)
( )
- 103. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
JShell
103
//
jshell> Map<Integer, String> map =
Map.of(1, "aaa", 2, "bbb", 3, “ccc");
map ==> {1=aaa, 2=bbb, 3=ccc}
jshell> map.put(4, "ddd");
| java.lang.UnsupportedOperationException thrown:
| at ImmutableCollections.uoe (ImmutableCollections.java:71)
( )
- 106. Copyright © Acroquest Technology Co., Ltd. All rights reserved.
#ccc_g3
106
//
var str = “Hello”;
final var MAXIMUM = 100L;
// NG
var array = {1, 2, 3};
var list = new ArrayList<>();