|
3 | 3 | <!-- Appendix: Collection Topics --> |
4 | 4 | # 附录:集合主题 |
5 | 5 |
|
| 6 | +> 本附录是一些比[第十二章 集合]()中介绍的更高级的内容。 |
6 | 7 |
|
7 | 8 | <!-- Sample Data --> |
8 | 9 | ## 示例数据 |
9 | 10 |
|
| 11 | +这里创建一些样本数据用于集合示例。 以下数据将颜色名称与HTML颜色的RGB值相关联。请注意,每个键和值都是唯一的: |
| 12 | + |
| 13 | +```java |
| 14 | +// onjava/HTMLColors.java |
| 15 | +// Sample data for collection examples |
| 16 | +package onjava; |
| 17 | +import java.util.*; |
| 18 | +import java.util.stream.*; |
| 19 | +import java.util.concurrent.*; |
| 20 | + |
| 21 | +public class HTMLColors { |
| 22 | + public static final Object[][] ARRAY = { |
| 23 | + { 0xF0F8FF, "AliceBlue" }, |
| 24 | + { 0xFAEBD7, "AntiqueWhite" }, |
| 25 | + { 0x7FFFD4, "Aquamarine" }, |
| 26 | + { 0xF0FFFF, "Azure" }, |
| 27 | + { 0xF5F5DC, "Beige" }, |
| 28 | + { 0xFFE4C4, "Bisque" }, |
| 29 | + { 0x000000, "Black" }, |
| 30 | + { 0xFFEBCD, "BlanchedAlmond" }, |
| 31 | + { 0x0000FF, "Blue" }, |
| 32 | + { 0x8A2BE2, "BlueViolet" }, |
| 33 | + { 0xA52A2A, "Brown" }, |
| 34 | + { 0xDEB887, "BurlyWood" }, |
| 35 | + { 0x5F9EA0, "CadetBlue" }, |
| 36 | + { 0x7FFF00, "Chartreuse" }, |
| 37 | + { 0xD2691E, "Chocolate" }, |
| 38 | + { 0xFF7F50, "Coral" }, |
| 39 | + { 0x6495ED, "CornflowerBlue" }, |
| 40 | + { 0xFFF8DC, "Cornsilk" }, |
| 41 | + { 0xDC143C, "Crimson" }, |
| 42 | + { 0x00FFFF, "Cyan" }, |
| 43 | + { 0x00008B, "DarkBlue" }, |
| 44 | + { 0x008B8B, "DarkCyan" }, |
| 45 | + { 0xB8860B, "DarkGoldenRod" }, |
| 46 | + { 0xA9A9A9, "DarkGray" }, |
| 47 | + { 0x006400, "DarkGreen" }, |
| 48 | + { 0xBDB76B, "DarkKhaki" }, |
| 49 | + { 0x8B008B, "DarkMagenta" }, |
| 50 | + { 0x556B2F, "DarkOliveGreen" }, |
| 51 | + { 0xFF8C00, "DarkOrange" }, |
| 52 | + { 0x9932CC, "DarkOrchid" }, |
| 53 | + { 0x8B0000, "DarkRed" }, |
| 54 | + { 0xE9967A, "DarkSalmon" }, |
| 55 | + { 0x8FBC8F, "DarkSeaGreen" }, |
| 56 | + { 0x483D8B, "DarkSlateBlue" }, |
| 57 | + { 0x2F4F4F, "DarkSlateGray" }, |
| 58 | + { 0x00CED1, "DarkTurquoise" }, |
| 59 | + { 0x9400D3, "DarkViolet" }, |
| 60 | + { 0xFF1493, "DeepPink" }, |
| 61 | + { 0x00BFFF, "DeepSkyBlue" }, |
| 62 | + { 0x696969, "DimGray" }, |
| 63 | + { 0x1E90FF, "DodgerBlue" }, |
| 64 | + { 0xB22222, "FireBrick" }, |
| 65 | + { 0xFFFAF0, "FloralWhite" }, |
| 66 | + { 0x228B22, "ForestGreen" }, |
| 67 | + { 0xDCDCDC, "Gainsboro" }, |
| 68 | + { 0xF8F8FF, "GhostWhite" }, |
| 69 | + { 0xFFD700, "Gold" }, |
| 70 | + { 0xDAA520, "GoldenRod" }, |
| 71 | + { 0x808080, "Gray" }, |
| 72 | + { 0x008000, "Green" }, |
| 73 | + { 0xADFF2F, "GreenYellow" }, |
| 74 | + { 0xF0FFF0, "HoneyDew" }, |
| 75 | + { 0xFF69B4, "HotPink" }, |
| 76 | + { 0xCD5C5C, "IndianRed" }, |
| 77 | + { 0x4B0082, "Indigo" }, |
| 78 | + { 0xFFFFF0, "Ivory" }, |
| 79 | + { 0xF0E68C, "Khaki" }, |
| 80 | + { 0xE6E6FA, "Lavender" }, |
| 81 | + { 0xFFF0F5, "LavenderBlush" }, |
| 82 | + { 0x7CFC00, "LawnGreen" }, |
| 83 | + { 0xFFFACD, "LemonChiffon" }, |
| 84 | + { 0xADD8E6, "LightBlue" }, |
| 85 | + { 0xF08080, "LightCoral" }, |
| 86 | + { 0xE0FFFF, "LightCyan" }, |
| 87 | + { 0xFAFAD2, "LightGoldenRodYellow" }, |
| 88 | + { 0xD3D3D3, "LightGray" }, |
| 89 | + { 0x90EE90, "LightGreen" }, |
| 90 | + { 0xFFB6C1, "LightPink" }, |
| 91 | + { 0xFFA07A, "LightSalmon" }, |
| 92 | + { 0x20B2AA, "LightSeaGreen" }, |
| 93 | + { 0x87CEFA, "LightSkyBlue" }, |
| 94 | + { 0x778899, "LightSlateGray" }, |
| 95 | + { 0xB0C4DE, "LightSteelBlue" }, |
| 96 | + { 0xFFFFE0, "LightYellow" }, |
| 97 | + { 0x00FF00, "Lime" }, |
| 98 | + { 0x32CD32, "LimeGreen" }, |
| 99 | + { 0xFAF0E6, "Linen" }, |
| 100 | + { 0xFF00FF, "Magenta" }, |
| 101 | + { 0x800000, "Maroon" }, |
| 102 | + { 0x66CDAA, "MediumAquaMarine" }, |
| 103 | + { 0x0000CD, "MediumBlue" }, |
| 104 | + { 0xBA55D3, "MediumOrchid" }, |
| 105 | + { 0x9370DB, "MediumPurple" }, |
| 106 | + { 0x3CB371, "MediumSeaGreen" }, |
| 107 | + { 0x7B68EE, "MediumSlateBlue" }, |
| 108 | + { 0x00FA9A, "MediumSpringGreen" }, |
| 109 | + { 0x48D1CC, "MediumTurquoise" }, |
| 110 | + { 0xC71585, "MediumVioletRed" }, |
| 111 | + { 0x191970, "MidnightBlue" }, |
| 112 | + { 0xF5FFFA, "MintCream" }, |
| 113 | + { 0xFFE4E1, "MistyRose" }, |
| 114 | + { 0xFFE4B5, "Moccasin" }, |
| 115 | + { 0xFFDEAD, "NavajoWhite" }, |
| 116 | + { 0x000080, "Navy" }, |
| 117 | + { 0xFDF5E6, "OldLace" }, |
| 118 | + { 0x808000, "Olive" }, |
| 119 | + { 0x6B8E23, "OliveDrab" }, |
| 120 | + { 0xFFA500, "Orange" }, |
| 121 | + { 0xFF4500, "OrangeRed" }, |
| 122 | + { 0xDA70D6, "Orchid" }, |
| 123 | + { 0xEEE8AA, "PaleGoldenRod" }, |
| 124 | + { 0x98FB98, "PaleGreen" }, |
| 125 | + { 0xAFEEEE, "PaleTurquoise" }, |
| 126 | + { 0xDB7093, "PaleVioletRed" }, |
| 127 | + { 0xFFEFD5, "PapayaWhip" }, |
| 128 | + { 0xFFDAB9, "PeachPuff" }, |
| 129 | + { 0xCD853F, "Peru" }, |
| 130 | + { 0xFFC0CB, "Pink" }, |
| 131 | + { 0xDDA0DD, "Plum" }, |
| 132 | + { 0xB0E0E6, "PowderBlue" }, |
| 133 | + { 0x800080, "Purple" }, |
| 134 | + { 0xFF0000, "Red" }, |
| 135 | + { 0xBC8F8F, "RosyBrown" }, |
| 136 | + { 0x4169E1, "RoyalBlue" }, |
| 137 | + { 0x8B4513, "SaddleBrown" }, |
| 138 | + { 0xFA8072, "Salmon" }, |
| 139 | + { 0xF4A460, "SandyBrown" }, |
| 140 | + { 0x2E8B57, "SeaGreen" }, |
| 141 | + { 0xFFF5EE, "SeaShell" }, |
| 142 | + { 0xA0522D, "Sienna" }, |
| 143 | + { 0xC0C0C0, "Silver" }, |
| 144 | + { 0x87CEEB, "SkyBlue" }, |
| 145 | + { 0x6A5ACD, "SlateBlue" }, |
| 146 | + { 0x708090, "SlateGray" }, |
| 147 | + { 0xFFFAFA, "Snow" }, |
| 148 | + { 0x00FF7F, "SpringGreen" }, |
| 149 | + { 0x4682B4, "SteelBlue" }, |
| 150 | + { 0xD2B48C, "Tan" }, |
| 151 | + { 0x008080, "Teal" }, |
| 152 | + { 0xD8BFD8, "Thistle" }, |
| 153 | + { 0xFF6347, "Tomato" }, |
| 154 | + { 0x40E0D0, "Turquoise" }, |
| 155 | + { 0xEE82EE, "Violet" }, |
| 156 | + { 0xF5DEB3, "Wheat" }, |
| 157 | + { 0xFFFFFF, "White" }, |
| 158 | + { 0xF5F5F5, "WhiteSmoke" }, |
| 159 | + { 0xFFFF00, "Yellow" }, |
| 160 | + { 0x9ACD32, "YellowGreen" }, |
| 161 | + }; |
| 162 | + public static final Map<Integer,String> MAP = |
| 163 | + Arrays.stream(ARRAY) |
| 164 | + .collect(Collectors.toMap( |
| 165 | + element -> (Integer)element[0], |
| 166 | + element -> (String)element[1], |
| 167 | + (v1, v2) -> { // Merge function |
| 168 | + throw new IllegalStateException(); |
| 169 | + }, |
| 170 | + LinkedHashMap::new |
| 171 | + )); |
| 172 | + // Inversion only works if values are unique: |
| 173 | + public static <V, K> Map<V, K> |
| 174 | + invert(Map<K, V> map) { |
| 175 | + return map.entrySet().stream() |
| 176 | + .collect(Collectors.toMap( |
| 177 | + Map.Entry::getValue, |
| 178 | + Map.Entry::getKey, |
| 179 | + (v1, v2) -> { |
| 180 | + throw new IllegalStateException(); |
| 181 | + }, |
| 182 | + LinkedHashMap::new |
| 183 | + )); |
| 184 | + } |
| 185 | + public static final Map<String,Integer> |
| 186 | + INVMAP = invert(MAP); |
| 187 | + // Look up RGB value given a name: |
| 188 | + public static Integer rgb(String colorName) { |
| 189 | + return INVMAP.get(colorName); |
| 190 | + } |
| 191 | + public static final List<String> LIST = |
| 192 | + Arrays.stream(ARRAY) |
| 193 | + .map(item -> (String)item[1]) |
| 194 | + .collect(Collectors.toList()); |
| 195 | + public static final List<Integer> RGBLIST = |
| 196 | + Arrays.stream(ARRAY) |
| 197 | + .map(item -> (Integer)item[0]) |
| 198 | + .collect(Collectors.toList()); |
| 199 | + public static |
| 200 | + void show(Map.Entry<Integer,String> e) { |
| 201 | + System.out.format( |
| 202 | + "0x%06X: %s%n", e.getKey(), e.getValue()); |
| 203 | + } |
| 204 | + public static void |
| 205 | + show(Map<Integer,String> m, int count) { |
| 206 | + m.entrySet().stream() |
| 207 | + .limit(count) |
| 208 | + .forEach(e -> show(e)); |
| 209 | + } |
| 210 | + public static void show(Map<Integer,String> m) { |
| 211 | + show(m, m.size()); |
| 212 | + } |
| 213 | + public static |
| 214 | + void show(Collection<String> lst, int count) { |
| 215 | + lst.stream() |
| 216 | + .limit(count) |
| 217 | + .forEach(System.out::println); |
| 218 | + } |
| 219 | + public static void show(Collection<String> lst) { |
| 220 | + show(lst, lst.size()); |
| 221 | + } |
| 222 | + public static |
| 223 | + void showrgb(Collection<Integer> lst, int count) { |
| 224 | + lst.stream() |
| 225 | + .limit(count) |
| 226 | + .forEach(n -> System.out.format("0x%06X%n", n)); |
| 227 | + } |
| 228 | + public static void showrgb(Collection<Integer> lst) { |
| 229 | + showrgb(lst, lst.size()); |
| 230 | + } |
| 231 | + public static |
| 232 | + void showInv(Map<String,Integer> m, int count) { |
| 233 | + m.entrySet().stream() |
| 234 | + .limit(count) |
| 235 | + .forEach(e -> |
| 236 | + System.out.format( |
| 237 | + "%-20s 0x%06X%n", e.getKey(), e.getValue())); |
| 238 | + } |
| 239 | + public static void showInv(Map<String,Integer> m) { |
| 240 | + showInv(m, m.size()); |
| 241 | + } |
| 242 | + public static void border() { |
| 243 | + System.out.println( |
| 244 | + "******************************"); |
| 245 | + } |
| 246 | +} |
| 247 | +``` |
| 248 | + |
| 249 | +**MAP** 是使用Streams([第十四章 流式编程]())创建的。 二维数组 **ARRAY** 作为流传输到 **Map** 中,但请注意我们不仅仅是使用简单版本的 **Collectors.toMap()** 。 那个版本生成一个 **HashMap** ,它使用散列函数来控制对键的排序。 为了保留原来的顺序,我们必须将键值对直接放入 **TreeMap** 中,这意味着我们需要使用更复杂的 **Collectors.toMap()** 版本。这需要两个函数从每个流元素中提取键和值,就像简单版本的**Collectors.toMap()** 一样。 然后它需要一个*合并函数*(merge function),它解决了与同一个键相关的两个值之间的冲突。这里的数据已经预先审查过,因此绝不会发生这种情况,如果有的话,这里会抛出异常。最后,传递生成所需类型的空map的函数,然后用流来填充它。 |
| 250 | + |
| 251 | +**rgb()** 方法是一个便捷函数(convenience function),它接受颜色名称 **String** 参数并生成其数字RGB值。为此,我们需要一个反转版本的 **COLORS** ,它接受一个 **String**键并查找RGB的 **Integer** 值。 这是通过 **invert()** 方法实现的,如果任何 **COLORS** 值不唯一,则抛出异常。 |
| 252 | + |
| 253 | +我们还创建包含所有名称的 **LIST** ,以及包含十六进制表示法的RGB值的 **RGBLIST** 。 |
| 254 | + |
| 255 | +第一个 **show()** 方法接受一个 **Map.Entry** 并显示以十六进制表示的键,以便轻松地对原始 **ARRAY** 进行双重检查。 名称以 **show** 开头的每个方法都会重载两个版本,其中一个版本采用 **count** 参数来指示要显示的元素数量,第二个版本显示序列中的所有元素。 |
| 256 | + |
| 257 | +这里是一个基本的测试: |
| 258 | + |
| 259 | +```java |
| 260 | +// collectiontopics/HTMLColorTest.java |
| 261 | +import static onjava.HTMLColors.*; |
| 262 | + |
| 263 | +public class HTMLColorTest { |
| 264 | + static final int DISPLAY_SIZE = 20; |
| 265 | + public static void main(String[] args) { |
| 266 | + show(MAP, DISPLAY_SIZE); |
| 267 | + border(); |
| 268 | + showInv(INVMAP, DISPLAY_SIZE); |
| 269 | + border(); |
| 270 | + show(LIST, DISPLAY_SIZE); |
| 271 | + border(); |
| 272 | + showrgb(RGBLIST, DISPLAY_SIZE); |
| 273 | + } |
| 274 | +} |
| 275 | +/* Output: |
| 276 | +0xF0F8FF: AliceBlue |
| 277 | +0xFAEBD7: AntiqueWhite |
| 278 | +0x7FFFD4: Aquamarine |
| 279 | +0xF0FFFF: Azure |
| 280 | +0xF5F5DC: Beige |
| 281 | +0xFFE4C4: Bisque |
| 282 | +0x000000: Black |
| 283 | +0xFFEBCD: BlanchedAlmond |
| 284 | +0x0000FF: Blue |
| 285 | +0x8A2BE2: BlueViolet |
| 286 | +0xA52A2A: Brown |
| 287 | +0xDEB887: BurlyWood |
| 288 | +0x5F9EA0: CadetBlue |
| 289 | +0x7FFF00: Chartreuse |
| 290 | +0xD2691E: Chocolate |
| 291 | +0xFF7F50: Coral |
| 292 | +0x6495ED: CornflowerBlue |
| 293 | +0xFFF8DC: Cornsilk |
| 294 | +0xDC143C: Crimson |
| 295 | +0x00FFFF: Cyan |
| 296 | +****************************** |
| 297 | +AliceBlue 0xF0F8FF |
| 298 | +AntiqueWhite 0xFAEBD7 |
| 299 | +Aquamarine 0x7FFFD4 |
| 300 | +Azure 0xF0FFFF |
| 301 | +Beige 0xF5F5DC |
| 302 | +Bisque 0xFFE4C4 |
| 303 | +Black 0x000000 |
| 304 | +BlanchedAlmond 0xFFEBCD |
| 305 | +Blue 0x0000FF |
| 306 | +BlueViolet 0x8A2BE2 |
| 307 | +Brown 0xA52A2A |
| 308 | +BurlyWood 0xDEB887 |
| 309 | +CadetBlue 0x5F9EA0 |
| 310 | +Chartreuse 0x7FFF00 |
| 311 | +Chocolate 0xD2691E |
| 312 | +Coral 0xFF7F50 |
| 313 | +CornflowerBlue 0x6495ED |
| 314 | +Cornsilk 0xFFF8DC |
| 315 | +Crimson 0xDC143C |
| 316 | +Cyan 0x00FFFF |
| 317 | +****************************** |
| 318 | +AliceBlue |
| 319 | +AntiqueWhite |
| 320 | +Aquamarine |
| 321 | +Azure |
| 322 | +Beige |
| 323 | +Bisque |
| 324 | +Black |
| 325 | +BlanchedAlmond |
| 326 | +Blue |
| 327 | +BlueViolet |
| 328 | +Brown |
| 329 | +BurlyWood |
| 330 | +CadetBlue |
| 331 | +Chartreuse |
| 332 | +Chocolate |
| 333 | +Coral |
| 334 | +CornflowerBlue |
| 335 | +Cornsilk |
| 336 | +Crimson |
| 337 | +Cyan |
| 338 | +****************************** |
| 339 | +0xF0F8FF |
| 340 | +0xFAEBD7 |
| 341 | +0x7FFFD4 |
| 342 | +0xF0FFFF |
| 343 | +0xF5F5DC |
| 344 | +0xFFE4C4 |
| 345 | +0x000000 |
| 346 | +0xFFEBCD |
| 347 | +0x0000FF |
| 348 | +0x8A2BE2 |
| 349 | +0xA52A2A |
| 350 | +0xDEB887 |
| 351 | +0x5F9EA0 |
| 352 | +0x7FFF00 |
| 353 | +0xD2691E |
| 354 | +0xFF7F50 |
| 355 | +0x6495ED |
| 356 | +0xFFF8DC |
| 357 | +0xDC143C |
| 358 | +0x00FFFF |
| 359 | +*/ |
| 360 | +``` |
| 361 | + |
| 362 | +可以看到,使用 **LinkedHashMap** 确实能够保留 **HTMLColors.ARRAY** 的顺序。 |
10 | 363 |
|
11 | 364 | <!-- List Behavior --> |
12 | 365 | ## List表现 |
|
0 commit comments