Skip to content

Commit

Permalink
Merge pull request #315 from hwanghe159/item44
Browse files Browse the repository at this point in the history
[#44][2κΈ°] ν‘œμ€€ ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜λΌ
  • Loading branch information
ksy90101 authored Oct 30, 2021
2 parents 08ba4f9 + b8def6e commit 10f5469
Showing 1 changed file with 187 additions and 0 deletions.
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)
- ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€μ˜ μœ„μΉ˜λ₯Ό λ‹€λ₯΄κ²Œ ν•΄μ„œ μ˜€λ²„λ‘œλ”©ν•˜μž.

0 comments on commit 10f5469

Please sign in to comment.