This repository was archived by the owner on Mar 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollections_41.html
More file actions
167 lines (167 loc) · 6.44 KB
/
collections_41.html
File metadata and controls
167 lines (167 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<meta name="generator" content=
"HTML Tidy for Mac OS X (vers 25 March 2009), see www.w3.org" />
<title>Scala 2.8 コレクション API -- 等価性</title>
<link rel="stylesheet" type="text/css" href="guide.css" />
</head>
<body dir="ltr">
<table width="100%" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#99CCFF"><a href="collections_42.html"><img border="0"
alt="ビュー" src="next.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_0.html"><img border="0"
alt="トップ" src="up.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_40.html"><img border="0"
alt="性能特性" src="previous.png" /></a></td>
<td align="center" bgcolor="#99CCFF" width="100%"><b>等価性</b></td>
<td bgcolor="#99CCFF" align="center" class="tocref"><a href=
"collections_49.html">目次</a></td>
</tr>
</table>
<blockquote style=
"border-left: 1px solid gray; font-family: Century, Times, 'Times New Roman', 'MS Gothic', serif; padding-left: 1em;">
最新版は <a href="http://docs.scala-lang.org/ja/overviews/collections/equality.html">Scala Documentation</a> に移行しました。
</blockquote>
<h1>等価性</h1>
<p>
コレクションライブラリは等価性 (equality) とハッシング (hashing) に関して統一的な方法を取る。
おおまかに言えば、まず、コレクションは集合、マップ、列の三種に大別される。別カテゴリのコレクションは常に不等だと判定される。例えば、<tt>Set(1,</tt> <tt>2,</tt> <tt>3)</tt>
と <tt>List(1,</tt> <tt>2,</tt> <tt>3)</tt>
は同じ要素を格納するが、不等だ。一方、同カテゴリ内ではコレクション内の要素が全く同じ要素から成る (列の場合は、同じ順序の同じ要素)
場合のみ等価だと判定される。例えば、<tt>List(1,</tt> <tt>2,</tt> <tt>3)</tt> <tt>==</tt> <tt>Vector(1,</tt> <tt>2,</tt> <tt>3)</tt>
であり、<tt>HashSet(1,</tt> <tt>2)</tt> <tt>==</tt> <tt>TreeSet(2,</tt> <tt>1)</tt>
だ。</p>
<p>
コレクションが可変であるか不変であるかは、等価性の判定には関わらない。可変コレクションに関しては、等価性判定が実行された時点での要素の状態が用いられる。これは、可変コレクションが追加されたり削除されたりする要素によって、別のコレクションと等価であったり不等であったりすることを意味する。これはハッシュマップのキーとして可変コレクションを使用した場合、落とし穴となりうる。具体例としては:</p>
<div class="quote">
<table cellspacing="1" cellpadding="0">
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">import</font> collection.mutable.{</tt><tt><font color="#660099">HashMap</font>, <font color="#660099">ArrayBuffer</font>}</tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">import collection.mutable.{</font></tt><tt><font color="#590000">HashMap, ArrayBuffer}</font></tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> buf = <font color=
"#660099">ArrayBuffer</font>(<font color=
"#000000">1</font>, <font color=
"#000000">2</font>, <font color="#000000">3</font>)</tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">buf: scala.collection.mutable.</font></tt><tt><font color="#590000">ArrayBuffer[Int] = </font></tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">ArrayBuffer(1, 2, 3)</font></tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> <font color=
"#0000E5">val</font> map = <font color=
"#660099">HashMap</font>(buf -> <font color=
"#000000">3</font>)</tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">map: scala.collection.mutable.</font></tt><tt><font color="#590000">HashMap[scala.collection.</font></tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">mutable.</font></tt><tt><font color=
"#590000">ArrayBuffer[Int],Int] = Map((ArrayBuffer(1, 2, 3),3))</font></tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> map(buf)</tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">res13: Int = 3</font></tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> buf(<font color=
"#000000">0</font>) += <font color=
"#000000">1</font></tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt>scala> map(buf)</tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">java.util.</font></tt><tt><font color=
"#590000">NoSuchElementException: key not found: </font></tt></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td colspan="99" align="left"><tt><font color=
"#590000">ArrayBuffer(2, 2, 3) </font></tt></td>
</tr>
</table>
</div>
<p>この例では、最後から二番目の行において配列 <tt>xs</tt>
のハッシュコードが変わったため、最後の行の選択は恐らく失敗に終わる。ハッシュコードによる検索は <tt>xs</tt>
が格納されていた元の位置とは別の場所を探しているからだ。</p>
<p>続いては、<a href="collections_42.html">ビュー</a></p>
<hr />
<table width="100%" cellpadding="0" cellspacing="2">
<tr>
<td bgcolor="#99CCFF"><a href="collections_42.html"><img border="0"
alt="ビュー" src="next.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_0.html"><img border="0"
alt="トップ" src="up.png" /></a></td>
<td bgcolor="#99CCFF"><a href="collections_40.html"><img border="0"
alt="性能特性" src="previous.png" /></a></td>
<td align="center" bgcolor="#99CCFF" width="100%"><b>等価性</b></td>
<td bgcolor="#99CCFF" align="center" class="tocref"><a href=
"collections_49.html">目次</a></td>
</tr>
</table>
</body>
</html>