You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ko/overviews/collections/maps.md
+11-9Lines changed: 11 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -108,9 +108,9 @@ language: ko
108
108
result
109
109
}
110
110
111
-
### Synchronized Sets and Maps ###
111
+
### 동기화된 집합과 맵 ###
112
112
113
-
To get a thread-safe mutable map, you can mix the `SynchronizedMap`trait trait into whatever particular map implementation you desire. For example, you can mix `SynchronizedMap` into `HashMap`, as shown in the code below. This example begins with an import of two traits, `Map` and `SynchronizedMap`, and one class, `HashMap`, from package `scala.collection.mutable`. The rest of the example is the definition of singleton object `MapMaker`, which declares one method, `makeMap`. The `makeMap`method declares its result type to be a mutable map of string keys to string values.
113
+
쓰레드 안전한 변경 가능한 맵을 만들고 싶다면 `SynchronizedMap`트레잇을 원하는 맵 구현에 끼워 넣으면 된다. 예를 들어 아래 코드처럼 `SynchronizedMap`을 `HashMap`에 끼워 넣을 수 있다. 아래 예제는 두 트레잇 `Map`과 `SynchronizedMap`, 그리고 클래스 `HashMap`을 패키지 `scala.collection.mutable`에서 임포트한다. 나머지 부분은 싱글턴 `MapMaker`를 만드는 것이다. 이 객체는 메소드 `makeMap`를 구현한다. `makeMap`메소드는 문자열에서 문자열로 맵핑하는 동기화된 해시맵을 반환한다.
114
114
115
115
import scala.collection.mutable.{Map,
116
116
SynchronizedMap, HashMap}
@@ -124,21 +124,21 @@ To get a thread-safe mutable map, you can mix the `SynchronizedMap` trait trait
124
124
}
125
125
}
126
126
127
-
<center>Mixing in the `SynchronizedMap` trait.</center>
127
+
<center>`SynchronizedMap`트레잇 끼워 넣기</center>
128
128
129
-
The first statement inside the body of `makeMap` constructs a new mutable `HashMap` that mixes in the `SynchronizedMap`trait:
129
+
`makeMap`의 첫 문장은 새로운 변경 가능한 `HashMap`을 만들고 `SynchronizedMap`트레잇을 끼워 넣는다.
130
130
131
131
new HashMap[String, String] with
132
132
SynchronizedMap[String, String]
133
133
134
-
Given this code, the Scala compiler will generate a synthetic subclass of `HashMap` that mixes in `SynchronizedMap`, and create (and return) an instance of it. This synthetic class will also override a method named `default`, because of this code:
134
+
스칼라 컴파일러는 이 코드를 보고 `SynchronizedMap`을 끼워 넣은 `HashMap`의 하위 클래스를 만들고, 그 클래스의 인스턴스를 만든다(그리고 반환한다). 이 합성 클래스는 아래와 같이 `default`라는 메소드를 재정의한다.
135
135
136
136
override def default(key: String) =
137
137
"Why do you want to know?"
138
138
139
-
If you ask a map to give you the value for a particular key, but it doesn't have a mapping for that key, you'll by default get a `NoSuchElementException`. If you define a new map class and override the `default`method, however, your new map will return the value returned by `default`when queried with a non-existent key. Thus, the synthetic `HashMap`subclass generated by the compiler from the code in the synchronized map code will return the somewhat curt response string, `"Why do you want to know?"`, when queried with a non-existent key.
139
+
사용자가 맵에게 어떤 키와 연관된 값을 물어봤는데 그런 연관이 맵에 존재하지 않는다면 기본 동작은 `NoSuchElementException` 예외를 발생시키는 것이다. 하지만 새 맵 클래스를 만들면서 `default`메소드를 재정의하면 존재하지 않는 키에 대한 질의가 들어올 때 `default` 메소드가 정의하는 값을 반환하게 된다. 따라서 컴파일러가 만든 동기화된 합성 `HashMap`하위 클래스는 존재하지 않는 키에 대한 질의를 받으면 `"Why do you want to know?"`라는 문자열을 반환한다.
140
140
141
-
Because the mutable map returned by the `makeMap`method mixes in the `SynchronizedMap`trait, it can be used by multiple threads at once. Each access to the map will be synchronized. Here's an example of the map being used, by one thread, in the interpreter:
141
+
`makeMap`메소드가 반환하는 변경 가능한 맵에 `SynchronizedMap`트레잇을 끼워 넣었기 때문에, 동시에 여러 쓰레드에서 이를 사용할 수 있다. 맵에 대한 억세스는 동기화될 것이다. 다음은 인터프리터상에서 한 쓰레드를 사용해 이 맵을 사용하는 예를 보여준다.
@@ -154,11 +154,13 @@ Because the mutable map returned by the `makeMap` method mixes in the `Synchroni
154
154
scala> capital("New Zealand")
155
155
res3: String = Wellington
156
156
157
-
You can create synchronized sets similarly to the way you create synchronized maps. For example, you could create a synchronized `HashSet` by mixing in the `SynchronizedSet` trait, like this:
157
+
동기화된 맵을 만드는 것과 비슷한 방식으로 동기화된 집합도 만들 수 있다. 예를 들어 `SynchronizedSet` 트레잇을 끼워 넣으면 동기화된 `HashSet`을 만들 수 있다. 다음과 같다.
158
158
159
159
import scala.collection.mutable
160
160
val synchroSet =
161
161
new mutable.HashSet[Int] with
162
162
mutable.SynchronizedSet[Int]
163
163
164
-
Finally, if you are thinking of using synchronized collections, you may also wish to consider the concurrent collections of `java.util.concurrent` instead.
164
+
마지막으로, 어떤 상황에서 동기화된 컬렉션을 사용하는 것을 고려하게 된다면, 그 상황이 `java.util.concurrent` 컬렉션을 필요로 하는 경우는 아닌지 한번 더 생각해 보도록 하라.
0 commit comments