-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #315 from hwanghe159/item44
[#44][2κΈ°] νμ€ ν¨μν μΈν°νμ΄μ€λ₯Ό μ¬μ©νλΌ
- Loading branch information
Showing
1 changed file
with
187 additions
and
0 deletions.
There are no files selected for viewing
187 changes: 187 additions & 0 deletions
187
7μ₯/44_νμ€ ν¨μν μΈν°νμ΄μ€λ₯Ό μ¬μ©νλΌ_ν©μ€νΈ.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
## μμ΄ν 44. νμ€ ν¨μν μΈν°νμ΄μ€λ₯Ό μ¬μ©νλΌ | ||
|
||
### ν¨μν μΈν°νμ΄μ€λ₯Ό μλ‘ μ μνκΈ° 보λ€λ κΈ°λ³Έ ν¨μν μΈν°νμ΄μ€λ₯Ό μ¬μ©νμ | ||
|
||
`LinkedHashMap`μ μ΄μ©νμ¬ μΊμλ₯Ό ꡬννλ€κ³ μκ°ν΄λ³΄μ. | ||
|
||
- `removeEldestEntry`λ₯Ό overrideνλ©΄ μΊμλ‘ μ¬μ©ν μ μλ€. | ||
|
||
```java | ||
public class Cache<K, V> extends LinkedHashMap<K, V> { | ||
|
||
private final int limit; | ||
|
||
public Cache(int limit) { | ||
this.limit = limit; | ||
} | ||
|
||
@Override | ||
protected boolean removeEldestEntry(Entry<K, V> eldest) { | ||
return size() > this.limit; | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
Cache<String, Integer> cache = new Cache<>(3); | ||
cache.put("1", 1); | ||
cache.put("2", 2); | ||
cache.put("3", 3); | ||
cache.put("4", 4); | ||
System.out.println(String.join(", ", cache.keySet())); // 2, 3, 4 | ||
``` | ||
|
||
- ν¨μν μΈν°νμ΄μ€λ‘ ꡬνν μλ μλ€. | ||
|
||
```java | ||
public class Cache<K, V> extends LinkedHashMap<K, V> { | ||
|
||
private final EldestEntryRemovalFunction<K, V> eldestEntryRemovalFunction; | ||
|
||
public Cache(EldestEntryRemovalFunction<K, V> eldestEntryRemovalFunction) { | ||
this.eldestEntryRemovalFunction = eldestEntryRemovalFunction; | ||
} | ||
|
||
@Override | ||
protected boolean removeEldestEntry(Entry<K, V> eldest) { | ||
return eldestEntryRemovalFunction.remove(this, eldest); | ||
} | ||
} | ||
``` | ||
|
||
```java | ||
@FunctionalInterface | ||
public interface EldestEntryRemovalFunction<K, V> { | ||
boolean remove(Map<K, V> map, Map.Entry<K, V> eldest); | ||
} | ||
``` | ||
|
||
```java | ||
Cache<String, Integer> cache = new Cache<>((map, eldest) -> map.size() > 3); | ||
cache.put("1", 1); | ||
cache.put("2", 2); | ||
cache.put("3", 3); | ||
cache.put("4", 4); | ||
System.out.println(String.join(", ", cache.keySet())); // 2, 3, 4 | ||
``` | ||
|
||
-> νμ§λ§ ν¨μν μΈν°νμ΄μ€λ₯Ό κ΅³μ΄ μλ‘ λ§λ€ νμκ° μλ€. κΈ°μ‘΄μ κ²μ μ μ¬μ©νλ©΄ λλ€. | ||
|
||
- BiPredicate μ¬μ© | ||
|
||
```java | ||
public class Cache<K, V> extends LinkedHashMap<K, V> { | ||
|
||
private final BiPredicate<Map<K, V>, Map.Entry<K, V>> biPredicate; | ||
|
||
public Cache(BiPredicate<Map<K, V>, Map.Entry<K, V>> biPredicate) { | ||
this.biPredicate = biPredicate; | ||
} | ||
|
||
@Override | ||
protected boolean removeEldestEntry(Entry<K, V> eldest) { | ||
return biPredicate.test(this, eldest); | ||
} | ||
} | ||
// λλ¨Έμ§ λμΌ | ||
``` | ||
|
||
### κΈ°λ³Έ ν¨μν μΈν°νμ΄μ€ | ||
|
||
#### `UnaryOperator<T>` : μΈμ 1κ°, μΈμμ νμ == λ°ν νμ | ||
|
||
- ν¨μ μκ·Έλμ² : `T apply(T t)` | ||
- μ : `String::toLowerCase` | ||
- λ³ν | ||
- κΈ°λ³Ένμ μ© `DoubleUnaryOperator`, `IntUnaryOperator`, `LongUnaryOperator` | ||
|
||
#### `BinaryOperator<T>` : μΈμ 2κ°, μΈμμ νμ == λ°ν νμ | ||
|
||
- ν¨μ μκ·Έλμ² : `T apply(T t1, T t2)` | ||
- μ : `BigInteger::add` | ||
- λ³ν | ||
- κΈ°λ³Ένμ μ© `DoubleBinaryOperator`, `IntBinaryOperator`, `LongBinaryOperator` | ||
|
||
#### `Predicate<T>` : μΈμ 1κ°, λ°ννμ == boolean | ||
|
||
- ν¨μ μκ·Έλμ² : `boolean test(T t)` | ||
- μ : `Collection::isEmpty` | ||
- λ³ν | ||
- κΈ°λ³Ένμ μ© `DoublePredicate`, `IntPredicate`, `LongPredicate` | ||
- μΈμλ₯Ό 2κ°λ°κ³ boolean λ°ν `BiPredicate<T, U>` | ||
|
||
#### `Function<T,R>` : μΈμμ νμ != λ°ν νμ | ||
|
||
- ν¨μ μκ·Έλμ² : `R apply(T t)` | ||
- μ : `Arrays::asList` | ||
- λ³ν | ||
- μ λ ₯μ κΈ°λ³Ένμ , μΆλ ₯μ Rνμ : `DoubleFunction<R>`, `IntFunction<R>`, `LongFunction<R>` | ||
- μ λ ₯κ³Ό μΆλ ₯ λͺ¨λ κΈ°λ³Ένμ (`..To..Function`) : `LongToIntFunction`, `DoubleToLongFunction` λ±λ±... | ||
- μΆλ ₯μ΄ κΈ°λ³Ένμ : `ToDoubleFunction<T>`, `ToIntFunction<T>`, `ToLongFunction<T>` | ||
- μΈμλ₯Ό 2κ°λ°κ³ Rνμ λ°ν `BiFunction<T,U,R>` | ||
- μΈμλ₯Ό 2κ°λ°κ³ κΈ°λ³Ένμ λ°ν `ToDoubleBiFunction<T,U>`, `ToIntBiFunction<T,U>`, `ToLongBiFunction<T,U>` | ||
|
||
#### `Supplier<T>` : μΈμ X, λ°ν O | ||
|
||
- ν¨μ μκ·Έλμ² : `T get()` | ||
- μ : `Instant::now` | ||
- λ³ν | ||
- κΈ°λ³Ένμ μ© `DoubleSupplier`, `IntSupplier`, `LongSupplier`, `BooleanSupplier` | ||
|
||
#### `Consumer<T>` : μΈμ 1κ°, λ°ν X | ||
|
||
- ν¨μ μκ·Έλμ² : `void accept(T t)` | ||
- μ : `System.out::println` | ||
- λ³ν | ||
- κΈ°λ³Ένμ μ© `DoubleConsumer`, `IntConsumer`, `LongConsumer` | ||
- μΈμ 2κ°λ°λ `BiConsumer<T, U>` | ||
- Tνμ , κΈ°λ³Ένμ λ°λ `ObjDoubleConsumer<T>`, `ObjIntConsumer<T>`, `ObjLongConsumer<T>` | ||
|
||
### κΈ°λ³Έ ν¨μν μΈν°νμ΄μ€μ λ°μ±λ κΈ°λ³Έ νμ μ λ£μ΄ μ¬μ©νμ§ λ§λΌ | ||
|
||
- μ) | ||
- `Function<Integer, Double>` 보λ€λ `IntToDoubleFunction` | ||
- `Supplier<Long>` 보λ€λ `LongSupplier` | ||
- λ±λ± ... | ||
|
||
### νμ€ ν¨μν μΈν°νμ΄μ€ λμ μ§μ ꡬνν΄μΌ ν λλ μλ€ | ||
|
||
```java | ||
@FunctionalInterface | ||
public interface Comparator<T> { | ||
int compare(T o1, T o2); | ||
//μ΄ν μλ΅ | ||
} | ||
``` | ||
|
||
1. μμ£Ό μ°μ΄λ©°, μ΄λ¦ μμ²΄κ° μ©λλ₯Ό λͺ νν μ€λͺ ν΄μ€λ€ | ||
- μ : ꡬ쑰μ μΌλ‘λ `ToIntBiFunction<T,U>`μ κ°μ§λ§ `Comparator<T>`λΌλ μ΄λ¦μ΄ ν¨μ¬ λͺ ννλ€ | ||
2. λ°λμ λ°λΌμΌ νλ κ·μ½μ΄ μλ€ | ||
- μ : `compare()` λ λ°λΌμΌ νλ κ·μ½μ΄ λ§λ€ | ||
3. μ μ©ν λν΄νΈ λ©μλλ₯Ό μ 곡ν μ μλ€ | ||
- μ : `Comparator<T>`λ `reversed()`, `thenComparing()` λ±λ±μ λ©μλλ₯Ό μ 곡νλ€ | ||
|
||
μ΄ μ€ νλ μ΄μμ λ§μ‘±νλ€λ©΄ μ μ© ν¨μν μΈν°νμ΄μ€λ₯Ό ꡬνν΄μΌ νλ 건 μλμ§ κ³ λ―Όν΄μΌ νλ€. | ||
|
||
### ν¨μν μΈν°νμ΄μ€λ₯Ό μ§μ ꡬνν λμ μ£Όμμ¬ν | ||
|
||
- `@FunctionalInterface`λ₯Ό λΆμ¬μΌ νλ€ | ||
|
||
- ν΄λΉ μΈν°νμ΄μ€κ° λλ€μ©μΌλ‘ μ€κ³λ κ²μμ λͺ ννκ² μλ €μ€λ€ | ||
- ν΄λΉ μΈν°νμ΄μ€κ° μΆμ λ©μλλ₯Ό μ€μ§ νλλ§ κ°μ§κ³ μμ΄μΌ μ»΄νμΌμ΄ λλλ‘ νλ€ -> λκ΅°κ° μ€μλ‘ λ©μλλ₯Ό μΆκ°ν μ μκ² λ§μμ€λ€ | ||
|
||
- μλ‘ λ€λ₯Έ ν¨μν μΈν°νμ΄μ€λ₯Ό κ°μ μμΉμ μΈμλ‘ λ°λ λ©μλλ€μ μ€λ²λ‘λ©νλ©΄ μλλ€. | ||
|
||
```java | ||
public interface ExecutorService extends Executor { | ||
// ... | ||
|
||
<T> Future<T> submit(Callable<T> task); | ||
Future<?> submit(Runnable task); | ||
|
||
// ... | ||
} | ||
``` | ||
|
||
- μ¬λ°λ₯Έ λ©μλλ₯Ό μλ €μ£ΌκΈ° μν΄ νλ³νμ ν΄μΌ ν λκ° λ§μ΄ μκΈ΄λ€ (μμ΄ν 52) | ||
- ν¨μν μΈν°νμ΄μ€μ μμΉλ₯Ό λ€λ₯΄κ² ν΄μ μ€λ²λ‘λ©νμ. |