Skip to content

Commit

Permalink
Merge branch 'depromeet:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
c0dep1ayer authored Aug 27, 2023
2 parents de4ac48 + f4846f5 commit 6f5df28
Show file tree
Hide file tree
Showing 15 changed files with 1,277 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# item19.์ƒ์†์„ ๊ณ ๋ คํ•ด ์„ค๊ณ„ํ•˜๊ณ  ๋ฌธ์„œํ™” ํ•˜๋ผ, ๊ทธ๋Ÿฌ์ง€ ์•Š์•˜๋‹ค๋ฉด ์ƒ์†์„ ๊ธˆ์ง€ํ•˜๋ผ

### ์ƒ์†์„ ๊ณ ๋ คํ•œ ์„ค๊ณ„์™€ ๋ฌธ์„œํ™”
์ƒ์†์šฉ ํด๋ž˜์Šค๋Š”
- ์žฌ์ •์˜ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ๋‚ด๋ถ€์ ์œผ๋กœ ์–ด๋–ป๊ฒŒ ์ด์šฉํ•˜๋Š”์ง€(์ž๊ธฐ์‚ฌ์šฉ) ๋ฌธ์„œ๋กœ ๋‚จ๊ฒจ์•ผ ํ•œ๋‹ค.

- ์žฌ์ •์˜ ๊ฐ€๋Šฅ = public ๊ณผ protected ๋ฉ”์„œ๋“œ ์ค‘ final ์ด ์•„๋‹Œ ๋ชจ๋“  ๋ฉ”์„œ๋“œ
### Implementation Requirements
API ๋ฌธ์„œ์˜ ๋ฉ”์„œ๋“œ ์„ค๋ช… ๋์—์„œ Implementation Requirements ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ ˆ์„ ์ข…์ข… ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

- ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ๋™์ž‘ ๋ฐฉ์‹์„ ์„ค๋ช…ํ•˜๋Š” ๊ณณ
- @implSpec ํƒœ๊ทธ โ†’ javadoc ๋„๊ตฌ๊ฐ€ ์ƒ์„ฑํ•ด์คŒ
- ํด๋ž˜์Šค๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ƒ์†ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‚ด๋ถ€ ๊ตฌํ˜„ ๋ฐฉ์‹์„ ์„ค๋ช…ํ•ด์•ผ ํ•œ๋‹ค.

### protected ๋กœ ๋…ธ์ถœํ•  ๋ฉ”์„œ๋“œ์˜ ๊ฒฐ์ •
- ์ˆ˜๋Š” ๊ฐ€๋Šฅํ•œ ์ ์–ด์•ผ ํ•œ๋‹ค.
- 3๊ฐœ ์ •๋„ ์‹ค์ œ ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๋Š” ๊ฒƒ์ด ์œ ์ผํ•˜๋‹ค.
- ์ œ 3 ์ž์˜ ์ž‘์„ฑ์œผ๋กœ๋„ ๊ฒ€์ฆํ•ด์•ผ ํ•œ๋‹ค.
- ๊ผญ ํ•„์š”ํ•œ protected ๋ฉค๋ฒ„๋ฅผ ๋†“์ณค๋‹ค๋ฉด ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ๊ทธ ๋นˆ์ž๋ฆฌ๊ฐ€ ํ™•์—ฐํžˆ ๋“œ๋Ÿฌ๋‚œ๋‹ค.
- ํ•˜์œ„ ํด๋ž˜์Šค๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋งŒ๋“ค ๋•Œ๊นŒ์ง€ ์ „ํ˜€ ์“ฐ์ด์ง€ ์•Š๋Š” protected ๋ฉค๋ฒ„๋Š” private ์ด์–ด์•ผ ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๋‹ค.
- ๋„ˆ๋ฌด ์ ๊ฒŒ ๋…ธ์ถœํ•ด์„œ ์ƒ์†์œผ๋กœ ์–ป๋Š” ์ด์ ๋งˆ์ € ์—†์• ์ง€ ์•Š๋„๋ก ์ฃผ์˜ ํ•œ๋‹ค

### ์ƒ์†์šฉ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋Š” ์ง/๊ฐ„์ ‘์ ์œผ๋กœ ์žฌ์ •์˜ ๊ฐ€๋Šฅ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์•ˆ๋œ๋‹ค.
private, final, static ๋ฉ”์„œ๋“œ๋Š” ์žฌ์ •์˜๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ˆ ์ƒ์„ฑ์ž์—์„œ ์•ˆ์‹ฌํ•˜๊ณ  ํ˜ธ์ถœํ•ด๋„ ๋œ๋‹ค.

- ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ณด๋‹ค ๋จผ์ € ์‹คํ–‰๋œ๋‹ค.
- ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ•œ ๋ฉ”์„œ๋“œ๊ฐ€, ํ•˜์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—์„œ ์ดˆ๊ธฐํ™” ํ•˜๋Š” ๊ฐ’์— ์˜์กด โ†’ ์˜ค๋™์ž‘
```java

public class Super {
// ์ž˜๋ชป๋œ ์˜ˆ - ์ƒ์„ฑ์ž๊ฐ€ ์žฌ์ •์˜ ๊ฐ€๋Šฅ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•จ.
public Super() {
overrideMe();
}
public void overrideMe() { }
}

public final class Sub extends Super {
// ์ดˆ๊ธฐํ™” ๋˜์ง€ ์•Š์€ final ํ•„๋“œ, ์ƒ์„ฑ์ž์—์„œ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค.
private final Instant instant;

Sub() {
instant = Instant.now();
}
// ์žฌ์ •์˜ ๊ฐ€๋Šฅ ๋ฉ”์„œ๋“œ, ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœํ•œ๋‹ค.
@Override public void overrideMe() {
System.out.println(instant); // null ์ถœ๋ ฅ
}

public static void main(String[] args) {
Sub sub = new Sub();
sub.overrideMe();
}
}
```
System.out.println(instant); ์ด ์‹คํ–‰ ๋  ๋•Œ, NullPointerException ์ด ๋ฐœ์ƒ ํ•˜์ง€๋งŒ println ์ด null ์ž…๋ ฅ๋„ ๋ฐ›์•„ ๋“ค์ด๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๋ฅผ ๋˜์ง€์ง€ ์•Š์•˜๋‹ค.

### ์ƒ์†์„ ๊ธˆ์ง€ํ•˜๋Š” ๋ฐฉ๋ฒ•
1. final ํด๋ž˜์Šค ์„ ์–ธ
2. ๋ชจ๋“  ์ƒ์„ฑ์ž๋ฅผ private ์ด๋‚˜ package-private ์œผ๋กœ ์„ ์–ธ, public ์ •์  ํŒฉํ„ฐ๋ฆฌ
3. ๋ž˜ํผ ํด๋ž˜์Šค ํŒจํ„ด

Set, List, Map ๊ฐ™์ด ํ•ต์‹ฌ๊ธฐ๋Šฅ์„ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ๊ณ , ํด๋ž˜์Šค๊ฐ€ ๊ทธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค๋ฉด ์ƒ์†์„ ๊ธˆ์ง€ํ•ด๋„ ๊ฐœ๋ฐœํ•˜๋Š” ๋ฐ ์•„๋ฌด๋Ÿฐ ์–ด๋ ค์›€์ด ์—†์„ ๊ฒƒ์ด๋‹ค.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
### โ˜๏ธ ํƒœ๊ทธ ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค
ํƒœ๊ทธ ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค๋ž€, **ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๊ฐ€ ๋‘ ๊ฐ€์ง€ ์ด์ƒ์˜ ์˜๋ฏธ๋ฅผ ํ‘œํ˜„** ๊ฐ€๋Šฅํ•  ๋•Œ, ๊ทธ์ค‘ ํ˜„์žฌ ํ‘œํ˜„ํ•˜๋Š” ์˜๋ฏธ๋ฅผ ํƒœ๊ทธ ๊ฐ’์œผ๋กœ ์•Œ๋ ค์ฃผ๋Š” ํด๋ž˜์Šค์ด๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ณด์ž.


```java
class Figure {
enum Shape { RECTANGLE, CIRCLE }; // TAG

// ํƒœ๊ทธ ํ•„๋“œ - ํ˜„์žฌ ๋ชจ์–‘์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
final Shape shape;

// ๋‹ค์Œ ํ•„๋“œ๋“ค์€ ๋ชจ์–‘์ด ์‚ฌ๊ฐํ˜•(RECTANGLE)์ผ ๋•Œ๋งŒ ์“ฐ์ธ๋‹ค.
double length;
double width;

// ๋‹ค์Œ ํ•„๋“œ๋Š” ๋ชจ์–‘์ด ์›(CIRCLE)์ผ ๋•Œ๋งŒ ์“ฐ์ธ๋‹ค.
double radius;

// ์›์šฉ ์ƒ์„ฑ์ž
Figure(double radius) {
shape = Shape.CIRCLE;
this.radius = radius;
}

// ์‚ฌ๊ฐํ˜•์šฉ ์ƒ์„ฑ์ž
Figure(double length, double width) {
shape = Shape.RECTANGLE;
this.length = length;
this.width = width;
}

double area() {
switch(shape) {
case RECTANGLE:
return length * width;
case CIRCLE:
return Math.PI * (radius * radius);
default:
throw new AssertionError(shape);
}
}
}
```

#### ํƒœ๊ทธ ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค ๋‹จ์ 

1. ์—ด๊ฑฐ ํƒ€์ž… ์„ ์–ธ, ํƒœ๊ทธ ํ•„๋“œ, `switch` ๋ฌธ ๋“ฑ ์“ธ๋ฐ์—†๋Š” ์ฝ”๋“œ๋กœ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง„๋‹ค.
2. ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ์œ„ํ•œ ์ฝ”๋“œ๋„ ํ•ด๋‹นํ•˜์ง€ ์•Š๋Š”๋ฐ ํ•ญ์ƒ ์กด์žฌํ•˜๋‹ˆ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งŽ์ด ๋จน๋Š”๋‹ค.
3. ํ•„๋“œ๋“ค์„ ๋ถˆ๋ณ€์„ ์œ„ํ•ด `final` ๋กœ ์„ ์–ธํ•˜๋ ค๋ฉด ํ•ด๋‹น ์˜๋ฏธ์— ์“ฐ์ด์ง€ ์•Š๋Š” ํ•„๋“œ๋“ค๊นŒ์ง€ ์ƒ์„ฑ์ž์—์„œ ์ดˆ๊ธฐํ™”ํ•ด์•ผํ•œ๋‹ค.
4. ๋˜ ๋‹ค๋ฅธ ์˜๋ฏธ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด `switch` ๋ฌธ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.
5. ์ธ์Šคํ„ด์Šค์˜ ํƒ€์ž…๋งŒ์œผ๋กœ๋Š” ํ˜„์žฌ ๋‚˜ํƒ€๋‚ด๋Š” ์˜๋ฏธ๋ฅผ ์•Œ ๊ธธ์ด ์ „ํ˜€ ์—†๋‹ค.

> ์ฆ‰, ํƒœ๊ทธ ๋‹ฌ๋ฆฐ ํด๋ž˜์Šค๋Š” ์žฅํ™ฉํ•˜๊ณ , ์˜ค๋ฅ˜๋ฅผ ๋‚ด๊ธฐ ์‰ฝ๊ณ , ๋น„ํšจ์œจ์ ์ด๋‹ค.
### โ˜๏ธ ํด๋ž˜์Šค ๊ณ„์ธต ๊ตฌ์กฐ
์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ, ํด๋ž˜์Šค ๊ณ„์ธต๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•˜๋Š” ์„œ๋ธŒํƒ€์ดํ•‘ (subtyping)์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํด๋ž˜์Šค ๊ณ„์ธต ๊ตฌ์กฐ๋Š”, **์ถ”์ƒ ํด๋ž˜์Šค์™€ ์ถ”์ƒ ๋ฉ”์„œ๋“œ**๋ฅผ ํ†ตํ•ด **ํƒ€์ž… ํ•˜๋‚˜๋กœ ๋‹ค์–‘ํ•œ ์˜๋ฏธ์˜ ๊ฐ์ฒด๋ฅผ ํ‘œํ˜„**ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.


1. ๊ณ„์ธต ๊ตฌ์กฐ์˜ ๋ฃจํŠธ๊ฐ€ ๋  ์ถ”์ƒ ํด๋ž˜์Šค ์ •์˜ํ•œ๋‹ค.
```java
abstract class Figure {
abstract double area();
}
```
+ ํƒœ๊ทธ ๊ฐ’์— ๋”ฐ๋ผ **๋™์ž‘์ด ๋‹ฌ๋ผ์ง€๋Š” ๋ฉ”์„œ๋“œ**๋“ค์„ ๋ฃจํŠธ ํด๋ž˜์Šค์˜ **์ถ”์ƒ ๋ฉ”์„œ๋“œ**๋กœ ์„ ์–ธ
+ ํƒœ๊ทธ ๊ฐ’์— ์ƒ๊ด€์—†์ด **๋™์ž‘์ด ์ผ์ •ํ•œ ๋ฉ”์„œ๋“œ์™€ ๊ณตํ†ต ๋ฉ”์„œ๋“œ**๋“ค์„ ๋ฃจํŠธ ํด๋ž˜์Šค์— **์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ**๋กœ ์„ ์–ธ


2. ๋ฃจํŠธ ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•œ ๊ตฌ์ฒด ํด๋ž˜์Šค๋ฅผ ์˜๋ฏธ๋ณ„๋กœ ํ•˜๋‚˜์”ฉ ์ง€์ •ํ•œ๋‹ค.

```java
class Circle extends Figure {
final double radius;

Circle(double radius) { this.radius = radius; }

@Override double area() { return Math.PI * (radius * radius); }
}
```

```java
class Rectangle extends Figure {
final double length;
final double width;

Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override double area() { return length * width; }
}
```
+ ๋ฃจํŠธ ํด๋ž˜์Šค๊ฐ€ ์ •์˜ํ•œ ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ์ž ์˜๋ฏธ์— ๋งž๊ฒŒ ๊ตฌํ˜„

#### ํด๋ž˜์Šค ๊ณ„์ธต ๊ตฌ์กฐ ์žฅ์ 

1. ๊ฐ ์˜๋ฏธ๋ฅผ ๋…๋ฆฝ๋œ ํด๋ž˜์Šค์— ๋‹ด์•„ ๊ด€๋ จ ์—†๋˜ ๋ฐ์ดํ„ฐ ํ•„๋“œ๋“ค์„ ์ œ๊ฑฐํ–ˆ๋‹ค.
2. ๋‚จ์•„ ์žˆ๋Š” ํ•„๋“œ๋“ค์€ ๋ชจ๋‘ `final` ๋กœ ์„ ์–ธํ•ด ๋ถˆ๋ณ€์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
3. ๊ฐ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ๋ชจ๋“  ํ•„๋“œ๋ฅผ ๋‚จ๊น€์—†์ด ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์ถ”์ƒ ๋ฉ”์„œ๋“œ๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ–ˆ๋Š”์ง€ ์ปดํŒŒ์ผ๋Ÿฌ ๋‹จ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
3. ๋ฃจํŠธ ํด๋ž˜์Šค์˜ ์ฝ”๋“œ๋ฅผ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š๊ณ  ๋…๋ฆฝ์ ์œผ๋กœ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# item24.๋ฉค๋ฒ„ ํด๋ž˜์Šค๋Š” ๋˜๋„๋ก static ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค๋ผ

### ์ค‘์ฒฉ ํด๋ž˜์Šค (Nested Class) : ์ž์‹ ์„ ๋‘˜๋Ÿฌ์‹ผ ๋ฐ”๊นฅ ํด๋ž˜์Šค์—์„œ๋งŒ ์“ฐ์ด๋Š” ํด๋ž˜์Šค
- ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค (static member class)
- ๋น„์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค
- ์ต๋ช… ํด๋ž˜์Šค
- ์ง€์—ญ ํด๋ž˜์Šค

### ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค
- ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ static ํด๋ž˜์Šค
> ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ private ๋ฉค๋ฒ„์—๋„ ๋ฐ”๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ. ์ด์™ธ์—๋Š” ์ผ๋ฐ˜ ํด๋ž˜์Šค์™€ ๊ฐ™๋‹ค.

- private ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค : ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋•Œ
```java
public class Person {
private String firstName;
private String lastName;

private static class Computer { // private, public
private String name;
private int price;

public Computer(String name, int price) {
this.name = name;
this.price = price;
}

public int getPrice() {
return price;
}
}
}
```

- public ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค : ๋ฐ”๊นฅ ํด๋ž˜์Šค์™€ ํ•จ๊ป˜ ์“ฐ์ผ ๋•Œ๋งŒ ์œ ์šฉํ•œ public ๋„์šฐ๋ฏธ ํด๋ž˜์Šค
```java
public class Calculator {
public enum Operation1 {
PLUS(Integer::sum),
MINUS((x, y) -> x - y);

private BiFunction<Integer, Integer, Integer> calculate;

Operation1(BiFunction<Integer, Integer, Integer> calculate) {
this.calculate = calculate;
}

public BiFunction<Integer, Integer, Integer> getFunction() {
return calculate;
}
}


public int Sum(int x, int y) {
return Operation1.PLUS.getFunction().apply(x, y);
}
}
```

```java
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.Sum(1, 2)); // 3

System.out.println(Calculator.Operation1.PLUS); // PLUS
}
```

<br>
<br>

### ๋น„์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค
- ๋น„์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋Š” ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์™€ ์•”๋ฌต์ ์œผ๋กœ ์—ฐ๊ฒฐ
- ๋”ฐ๋ผ์„œ, ๋น„์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ๋ฉ”์„œ๋“œ์—์„œ ์ •๊ทœํ™”๋œ this ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๊นฅ ์ธ์Šคํ„ด์Šค์˜ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ.
> ์ •๊ทœํ™”๋œ this == ํด๋ž˜์Šค๋ช….this
- ๋น„ ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ์‹ธ์„œ ๋งˆ์น˜ ๋‹ค๋ฅธ ํด๋ž˜์Šค์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ํ•˜๋Š” ๋ทฐ์ธ ์–ด๋Œ‘ํ„ฐ์—์„œ ์ฃผ๋กœ ์“ฐ์ธ๋‹ค.
```java
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, java.io.Serializable {
// View class support
class Values extends AbstractCollection<V> {
public Iterator<V> iterator() {
return new ValueIterator(getFirstEntry());
}

public int size() {
return TreeMap.this.size();
}

public boolean contains(Object o) {
return TreeMap.this.containsValue(o);
}

public boolean remove(Object o) {
for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) {
if (valEquals(e.getValue(), o)) {
deleteEntry(e);
return true;
}
}
return false;
}

public void clear() {
TreeMap.this.clear();
}

public Spliterator<V> spliterator() {
return new ValueSpliterator<>(TreeMap.this, null, null, 0, -1, 0);
}
}


class EntrySet extends AbstractSet<Map.Entry<K,V>> {
public Iterator<Map.Entry<K,V>> iterator() {
return new EntryIterator(getFirstEntry());
}

public boolean contains(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
Object value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
return p != null && valEquals(p.getValue(), value);
}

public boolean remove(Object o) {
if (!(o instanceof Map.Entry))
return false;
Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
Object value = entry.getValue();
Entry<K,V> p = getEntry(entry.getKey());
if (p != null && valEquals(p.getValue(), value)) {
deleteEntry(p);
return true;
}
return false;
}
}
}
```
- TreeMap.this, getFirstEntry() ๋Š” ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์žˆ์–ด์•ผ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
- ๋”ฐ๋ผ์„œ ๋น„์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์•ˆ์— ๊ด€๊ณ„์ •๋ณด๊ฐ€ ์ €์žฅ๋˜์–ด ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๊ณ , ์ƒ์„ฑ ์‹œ๊ฐ„๋„ ๋” ๊ฑธ๋ฆฐ๋‹ค.
- ๋˜ํ•œ ๋ฐ”๊นฅ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ GC ๊ฐ€ ํšŒ์ˆ˜ํ•˜์ง€ ๋ชปํ•œ๋‹ค.
```java
public Set<Map.Entry<K,V>> entrySet() {
EntrySet es = entrySet;
return (es != null) ? es : (entrySet = new EntrySet());
}
```
์œ„์˜ ์ฝ”๋“œ์—์„œ ๋น„์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค EntrySet์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์•”๋ฌต์ ์œผ๋กœ ๋ฐ”๊นฅ ํด๋ž˜์Šค์™€ ๋ฉค๋ฒ„ ํด๋ž˜์Šค๊ฐ„์˜ ์—ฐ๊ฒฐ๋˜๋Š” ๊ด€๊ณ„๊ฐ€ ๋น„์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค ์•ˆ์— ๋งŒ๋“ค์–ด์ง€๊ณ ,
Map ๊ฐ์ฒด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๊ณณ์ด ์—†๋”๋ผ๋„ ์ด ๊ด€๊ณ„๋•Œ๋ฌธ์— GC ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ๋ชปํ•œ๋‹ค.

<br>

### ๊ฒฐ๋ก 
- ๊ฐœ๋…์ƒ ์ค‘์ฒฉ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”๊นฅ ์ธ์Šคํ„ด์Šค์™€ ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.
- ๋ฉค๋ฒ„ ํด๋ž˜์Šค์—์„œ ๋ฐ”๊นฅ ์ธ์Šคํ„ด์Šค์— ์ ‘๊ทผํ•  ์ผ์ด ์—†๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด static ์„ ๋ถ™์—ฌ์„œ ์ •์  ๋ฉค๋ฒ„ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค์ž.
Loading

0 comments on commit 6f5df28

Please sign in to comment.