CHAP21 - Modern-Java-in-Action/Online-Study GitHub Wiki
- ์๋ฐ 8์ ๊ธฐ๋ฅ๊ณผ ์๋ฐ 8์ด ํ๋ก๊ทธ๋๋ฐ ํ์์ ๊ฐ์ ธ์ฌ ๋ณํ
- ์๋ก์ด ์๋ฐ 9 ๋ชจ๋ ์์คํ
- 6๊ฐ์ ์ฃผ๊ธฐ์ ์ ์ง์ ์๋ฐ ๋ฆด๋ฆฌ์ค ์๋ช ์ฃผ๊ธฐ
- ์ฒซ ๋ฒ์งธ ์ ์ง์ ๋ฆด๋ฆฌ์ค ์๋ฐ 10
- ๋ฏธ๋ ์๋ฐ ๋ฒ์ ์ ์ถ๊ฐ๋๋ฆฌ๋ผ ๊ธฐ๋ํ๋ ๊ธฐ๋ฅ
- ์๋ฐ 8์ ์ถ๊ฐ๋ ๋๋ถ๋ถ ๊ธฐ๋ฅ -> ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ์ฝ๊ฒ ์ ์ฉํ ์ ์๋๋ก ๋์์ค
- ํฐ ๋ณํ๊ฐ ์๊ธด ์ด์
- ๊ฐ๋ณ CPU ์ฝ์ด ์๋๊ฐ ๋นจ๋ผ์ง๋ฉฐ ๋ณ๋ ฌ ์คํ์ ์ฝ๋ ์คํ ์๋ ํฅ์ ๊ฐ๋ฅ
- ๊ฐ๊ฒฐํ๊ฒ ๋ฐ์ดํฐ ์ปฌ๋ ์ ์ ์ฒ๋ฆฌํ๊ธฐ ์ํจ(๋ถ๋ณ ๊ฐ์ฒด, ๋ถ๋ณ ์ปฌ๋ ์ )
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์์ ์ง์ํ๋ ๋ฉ์๋๋ก ์ฝ๋ ๋ธ๋ก์ ์ ๋ฌํ๋ ๊ธฐ๋ฒ
// Apple์ Predicate์ test์ ํด๋นํ๋ isGreenApple, isHeavyApple ๊ตฌํ
public class Apple {
public boolean isGreenApple() {
return GREEN.equals(color);
}
public boolean isHeavyApple() {
return weight > 150;
}
}
// Predicate๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋๋ค
public List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> applePredicate) {
List<Apple> result = new ArrayList<>();
for(Apple apple : inventory) {
if(applePredicate.test(apple)) {
result.add(apple);
}
}
return result;
}
// stream์ filter๋ฅผ ์ด์ฉํ๋ฉด ๋ ๊ฐ๊ฒฐํ๊ฒ ํํ ๊ฐ๋ฅ
public List<Apple> filterApplesForStream(List<Apple> inventory, Predicate<Apple> applePredicate) {
return inventory.stream().filter(applePredicate::test).collect(Collectors.toList());
}
// ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์
List<Apple> greenApples = filterApples(inventory, Apple::isGreenApple);
List<Apple> heavyApples = filterApples(inventory, Apple::isHeavyApple);
๋๋ ๋๋ค ํํ์(lambda expression)์ ์ด์ฉํ์ฌ ํํ
List<Apple> redApples = filterApples(inventory, (Apple apple) -> RED.equals(apple.getColor()));
List<Apple> heavyApples2 = filterApples(inventory, (Apple apple) -> apple.getWeight() > 150);
- ์คํธ๋ฆผ API๋ ํ์ดํ๋ผ์ธ์ด๋ผ๋ ๊ฒ์ผ๋ฅธ ํ์์ ์ฐ์ฐ์ ์ฌ์ฉ
- ํฐ ๋ฐ์ดํฐ ์งํฉ์ผ์๋ก ์คํธ๋ฆผ์ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๋ฐฉ์์ด ํจ์จ์ ์ด๋ฉฐ, ๋ฉ๋ชจ๋ฆฌ ์บ์ ๋ฑ์ ๊ด์ ์์๋ ํ์ ํ์๋ฅผ ์ต์ํ ํ๋ ๊ฒ์ด ์ค์ํจ
- parallel ๋ฉ์๋๋ก ์คํธ๋ฆผ์ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌ
/*
* 1. ํตํ๋ณ๋ก ํธ๋์ญ์
์ ๊ทธ๋ฃนํํ ๋ค์์ ํด๋น ํตํ๋ก ์ผ์ด๋ ๋ชจ๋ ํธ๋์ญ์
ํฉ๊ณ๋ฅผ ๊ณ์ฐํ์์ค(Map<Currency, Integer>)
* 2. ํธ๋์ญ์
์ ๋น์ผ ํธ๋์ญ์
๊ณผ ์ ๋ ดํ ํธ๋์ญ์
๋ ๊ทธ๋ฃน์ผ๋ก ๋ถ๋ฅํ์์ค(Map<Boolean, List<Transaction>>)
*/
// Collection ๋ฒ์
Map<Currency, List<Transaction>> transactionsByCurrencies = new HashMap<>();
for (Transaction transaction : transactions) {
Currency currency = transaction.getCurrency();
List<Transaction> transactionsForCurrency = transactionsByCurrencies.get(currency);
if (transactionsForCurrency == null) {
transactionsForCurrency = new ArrayList<>();
transactionsByCurrencies.put(currency, transactionsForCurrency);
}
transactionsForCurrency.add(transaction);
}
// Stream(ํจ์ํ ํ๋ก๊ทธ๋๋ฐ) ๋ฒ์
Map<Currency, List<Transaction>> transactionsByCurrencies2 = transactions.stream().collect(groupingBy(Transaction::getCurrency));
- ์๋ฐ5์ Future ์ธํฐํ์ด์ค : ์ฌ๋ฌ ์์ ์ด ๋์์ ์คํ๋ ์ ์๋๋ก ๋ค๋ฅธ ์ค๋ ๋๋ ์ฝ์ด๋ก ์์ ์ ํ ๋น ๊ฐ๋ฅ(๋ฉํฐ์ฝ์ด ํ์ฉ)
- Future์ ๊ด๋ จ๋ ๊ณตํต ๋์์ธ ํจํด์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ผ๋ก ๊ฐ๊ฒฐํ๊ฒ ํํํ ์ ์๋๋ก thenCompose, thenCombine, allOf ๋ฑ์ ์ ๊ณต
- ๋ช ๋ นํ์์ ๋ฐ์ํ๋ ๋ถํ์ํ ์ฝ๋ ๊ฐ์ํจ๊ณผ
- T ํ์์ ๊ฐ์ ๋ฐํํ๊ฑฐ๋ Optional.empty(๊ฐ์ด ์์)๋ผ๋ ์ ์ ๋ฉ์๋๋ฅผ ๋ฐํํ ์ ์๋ Optional<T> ํด๋์ค
- ๊ฐ์ด ์์ ๋ ์๋ฌ๋ฅผ ๋ฐ์์ํฌ ์ ์๋ null ๋์ ์ ํด์ง ๋ฐ์ดํฐ ํ์์ ์ ๊ณต
// BEFORE
String name = null;
if(insurance != null){
name = insurance.getName();
}
// AFTER
Optional<Insurance> optInsurance = Optional.ofNullable(insurance);
Optional<String> name = optInsurance.map(Insurance::getName);
- ์ฐธ๊ณ : Java Optional ๋ฐ๋ฅด๊ฒ ์ฐ๊ธฐ
- ์๋ฐ9์ ๋ฆฌ์กํฐ๋ธ ์คํธ๋ฆผ, ๋ฆฌ์กํฐ๋ธ ๋น๊น ๊ธฐ๋ฐ ์ญ์๋ ฅ ํ๋กํ ํด ํ์คํ
- Flow API๋ ํธํ์ฑ์ ๋์ผ ์ ์๋๋ก ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๊ตฌํํ ์ ์๋ ๋ค ๊ฐ์ ์ธํฐํ์ด์ค Publisher, Subscriber, Subscription, Processor ํฌํจ
- ์ธํฐํ์ด์ค ์ค๊ณ์๊ฐ ๋ฉ์๋์ ๊ธฐ๋ณธ ๊ตฌํ์ ์ ๊ณต ๊ฐ๋ฅ
- ์ธํฐํ์ด์ค ๊ตฌํ ํด๋์ค๋ค์ด ์๋ก ์ถ๊ฐ๋ ๊ธฐ๋ฅ์ ๊ตฌํํ์ง ์์๋ ๋จ
// ์์๊ณผ ๊ด๋ จ๋ ๋ฌธ์ ๊ฐ ์์..
public interface A {
default void hello {
print A
}
}
public interface B extends A {
default void hello {
print B
}
}
public class C implements B, A {
public static void main(String... args) {
new C().hello(); // print B
}
}
- ์๋ฐ 9์์ ์ถ๊ฐ๋ ๋ด์ฉ
- ์คํธ๋ฆผ์ takeWhile, dropWhile ์ถ๊ฐ
- CompletableFuture์ completeOnTimeout ์ถ๊ฐ
- ๋ชจ๋ ์์คํ - module-info.java ํ์ผ ์ถ๊ฐ
- ๋ค๋ฅธ ๋ฆด๋ฆฌ์ค์ ๋นํด ๊ณผ๊ฑฐ ํธํ์ฑ์ ํด์ณค๋ค๋ ์๊ฒฌ
- ๋ชจ๋ํ๋ผ๋ ์ฅ์ ์ ์ป๊ธฐ ์ํ ๋ถ๊ฐํผํ ํฌ์
- ์๋ฐ ๋ชจ๋ ์์คํ
์ด ์ ๊ณตํ๋ ์ฅ์
- ์์ ์ ์ค์
- ๊ฐํ ์บก์ํ
- ๋ณด์์ฑ ๊ฐ์
- ์ฑ๋ฅ ๊ฐ์
- ํ์ฅ์ฑ
- ์๋ฐ7 : ๋ณ์๊ฐ ๋ฉ์๋๋ฅผ ์ ์ํ ๋ ์ปจํ ์คํธ๋ก ํ์์ ์ ์ถํ ์ ์๋ ์ํฉ์์๋ ์๋ต ๊ฐ๋ฅ
Map<String, List<String>> myMap = new HashMap<String, List<String>>();
-> Map<String, List<String>> myMap = new HashMap<>();
- ์๋ฐ10 : ๋ก์ปฌ๋ณ์ ์ ์ธ์ var๋ฅผ ์ด์ฉํ์ฌ ์ปดํ์ผ๋ฌ์๊ฒ ํ์ ์ ์ถ๋ก ํ๊ฒํ ์ ์๋๋ก ํจ
var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream(); // infers Stream<String>
var numbers = List.of(1, 2, 3, 4, 5);
for (var number : numbers) {
System.out.println(number);
}
for (var i = 0; i < numbers.size(); i++) {
System.out.println(numbers.get(i));
}
- String ํด๋์ค์ ์๋ก์ด ๋ฉ์๋
- isBlank, lines, strip, stripLeading, stripTrailing, repeat
String multilineString = "Baeldung helps \n \n developers \n explore Java.";
List<String> lines = multilineString.lines()
.filter(line -> !line.isBlank())
.map(String::strip)
.collect(Collectors.toList());
assertThat(lines).containsExactly("Baeldung helps", "developers", "explore Java.");
- Files ํด๋์ค์ readString, writeString ์ถ๊ฐ
Path filePath = Files.writeString(Files.createTempFile(tempDir, "demo", ".txt"), "Sample text");
String fileContent = Files.readString(filePath);
assertThat(fileContent).isEqualTo("Sample text");
- ์ปฌ๋์ ์ toArray() ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ก๋ฉํ๋ ๋ฉ์๋ ์ถ๊ฐ -> ์ํ๋ ํ์ ์ ๋ฐฐ์ด ์ ํ ๊ฐ๋ฅ
List<String> sampleList = Arrays.asList("Java", "Kotlin");
String[] sampleArray = sampleList.toArray(String[]::new);
- Predicate ์ธํฐํ์ด์ค์ not ๋ฉ์๋ ์ถ๊ฐ
- ์๋ฐ ์ปดํ์ผ์์ด ์คํ ๊ฐ๋ฅ
# before
$ javac HelloWorld.java
$ java HelloWorld
Hello Java 8!
# after
$ java HelloWorld.java
Hello Java 11
- ์ฐธ๊ณ : Java8๊ณผ Java11์ ํน์ง
์ง๊ธ๊น์ง ๋ฐฐ์์ด๋ผ๋ ์ฌํ์ ์ฆ๊ฒผ๊ธธ ๋ฐ๋๋ค.
๊ทธ๋ฆฌ๊ณ ์์ผ๋ก ์ผ์ด๋ ์๋ฐ์ ์งํ๋ฅผ ์ดํด๋ณด๋ ๋ฐ ๋ ๋ง์ ๊ด์ฌ์ด ์๊ฒผ๊ธธ ํฌ๋งํ๋ค