82 |
|
* <p>This implementation returns <tt>entrySet().size()</tt>. |
83 |
|
*/ |
84 |
|
public int size() { |
85 |
< |
return entrySet().size(); |
85 |
> |
return entrySet().size(); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
91 |
|
* <p>This implementation returns <tt>size() == 0</tt>. |
92 |
|
*/ |
93 |
|
public boolean isEmpty() { |
94 |
< |
return size() == 0; |
94 |
> |
return size() == 0; |
95 |
|
} |
96 |
|
|
97 |
|
/** |
107 |
|
* @throws NullPointerException {@inheritDoc} |
108 |
|
*/ |
109 |
|
public boolean containsValue(Object value) { |
110 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
111 |
< |
if (value==null) { |
112 |
< |
while (i.hasNext()) { |
113 |
< |
Entry<K,V> e = i.next(); |
114 |
< |
if (e.getValue()==null) |
115 |
< |
return true; |
116 |
< |
} |
117 |
< |
} else { |
118 |
< |
while (i.hasNext()) { |
119 |
< |
Entry<K,V> e = i.next(); |
120 |
< |
if (value.equals(e.getValue())) |
121 |
< |
return true; |
122 |
< |
} |
123 |
< |
} |
124 |
< |
return false; |
110 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
111 |
> |
if (value==null) { |
112 |
> |
while (i.hasNext()) { |
113 |
> |
Entry<K,V> e = i.next(); |
114 |
> |
if (e.getValue()==null) |
115 |
> |
return true; |
116 |
> |
} |
117 |
> |
} else { |
118 |
> |
while (i.hasNext()) { |
119 |
> |
Entry<K,V> e = i.next(); |
120 |
> |
if (value.equals(e.getValue())) |
121 |
> |
return true; |
122 |
> |
} |
123 |
> |
} |
124 |
> |
return false; |
125 |
|
} |
126 |
|
|
127 |
|
/** |
138 |
|
* @throws NullPointerException {@inheritDoc} |
139 |
|
*/ |
140 |
|
public boolean containsKey(Object key) { |
141 |
< |
Iterator<Map.Entry<K,V>> i = entrySet().iterator(); |
142 |
< |
if (key==null) { |
143 |
< |
while (i.hasNext()) { |
144 |
< |
Entry<K,V> e = i.next(); |
145 |
< |
if (e.getKey()==null) |
146 |
< |
return true; |
147 |
< |
} |
148 |
< |
} else { |
149 |
< |
while (i.hasNext()) { |
150 |
< |
Entry<K,V> e = i.next(); |
151 |
< |
if (key.equals(e.getKey())) |
152 |
< |
return true; |
153 |
< |
} |
154 |
< |
} |
155 |
< |
return false; |
141 |
> |
Iterator<Map.Entry<K,V>> i = entrySet().iterator(); |
142 |
> |
if (key==null) { |
143 |
> |
while (i.hasNext()) { |
144 |
> |
Entry<K,V> e = i.next(); |
145 |
> |
if (e.getKey()==null) |
146 |
> |
return true; |
147 |
> |
} |
148 |
> |
} else { |
149 |
> |
while (i.hasNext()) { |
150 |
> |
Entry<K,V> e = i.next(); |
151 |
> |
if (key.equals(e.getKey())) |
152 |
> |
return true; |
153 |
> |
} |
154 |
> |
} |
155 |
> |
return false; |
156 |
|
} |
157 |
|
|
158 |
|
/** |
169 |
|
* @throws NullPointerException {@inheritDoc} |
170 |
|
*/ |
171 |
|
public V get(Object key) { |
172 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
173 |
< |
if (key==null) { |
174 |
< |
while (i.hasNext()) { |
175 |
< |
Entry<K,V> e = i.next(); |
176 |
< |
if (e.getKey()==null) |
177 |
< |
return e.getValue(); |
178 |
< |
} |
179 |
< |
} else { |
180 |
< |
while (i.hasNext()) { |
181 |
< |
Entry<K,V> e = i.next(); |
182 |
< |
if (key.equals(e.getKey())) |
183 |
< |
return e.getValue(); |
184 |
< |
} |
185 |
< |
} |
186 |
< |
return null; |
172 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
173 |
> |
if (key==null) { |
174 |
> |
while (i.hasNext()) { |
175 |
> |
Entry<K,V> e = i.next(); |
176 |
> |
if (e.getKey()==null) |
177 |
> |
return e.getValue(); |
178 |
> |
} |
179 |
> |
} else { |
180 |
> |
while (i.hasNext()) { |
181 |
> |
Entry<K,V> e = i.next(); |
182 |
> |
if (key.equals(e.getKey())) |
183 |
> |
return e.getValue(); |
184 |
> |
} |
185 |
> |
} |
186 |
> |
return null; |
187 |
|
} |
188 |
|
|
189 |
|
|
201 |
|
* @throws IllegalArgumentException {@inheritDoc} |
202 |
|
*/ |
203 |
|
public V put(K key, V value) { |
204 |
< |
throw new UnsupportedOperationException(); |
204 |
> |
throw new UnsupportedOperationException(); |
205 |
|
} |
206 |
|
|
207 |
|
/** |
226 |
|
* @throws NullPointerException {@inheritDoc} |
227 |
|
*/ |
228 |
|
public V remove(Object key) { |
229 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
230 |
< |
Entry<K,V> correctEntry = null; |
231 |
< |
if (key==null) { |
232 |
< |
while (correctEntry==null && i.hasNext()) { |
233 |
< |
Entry<K,V> e = i.next(); |
234 |
< |
if (e.getKey()==null) |
235 |
< |
correctEntry = e; |
236 |
< |
} |
237 |
< |
} else { |
238 |
< |
while (correctEntry==null && i.hasNext()) { |
239 |
< |
Entry<K,V> e = i.next(); |
240 |
< |
if (key.equals(e.getKey())) |
241 |
< |
correctEntry = e; |
242 |
< |
} |
243 |
< |
} |
244 |
< |
|
245 |
< |
V oldValue = null; |
246 |
< |
if (correctEntry !=null) { |
247 |
< |
oldValue = correctEntry.getValue(); |
248 |
< |
i.remove(); |
249 |
< |
} |
250 |
< |
return oldValue; |
229 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
230 |
> |
Entry<K,V> correctEntry = null; |
231 |
> |
if (key==null) { |
232 |
> |
while (correctEntry==null && i.hasNext()) { |
233 |
> |
Entry<K,V> e = i.next(); |
234 |
> |
if (e.getKey()==null) |
235 |
> |
correctEntry = e; |
236 |
> |
} |
237 |
> |
} else { |
238 |
> |
while (correctEntry==null && i.hasNext()) { |
239 |
> |
Entry<K,V> e = i.next(); |
240 |
> |
if (key.equals(e.getKey())) |
241 |
> |
correctEntry = e; |
242 |
> |
} |
243 |
> |
} |
244 |
> |
|
245 |
> |
V oldValue = null; |
246 |
> |
if (correctEntry !=null) { |
247 |
> |
oldValue = correctEntry.getValue(); |
248 |
> |
i.remove(); |
249 |
> |
} |
250 |
> |
return oldValue; |
251 |
|
} |
252 |
|
|
253 |
|
|
286 |
|
* @throws UnsupportedOperationException {@inheritDoc} |
287 |
|
*/ |
288 |
|
public void clear() { |
289 |
< |
entrySet().clear(); |
289 |
> |
entrySet().clear(); |
290 |
|
} |
291 |
|
|
292 |
|
|
316 |
|
* method will not all return the same set. |
317 |
|
*/ |
318 |
|
public Set<K> keySet() { |
319 |
< |
if (keySet == null) { |
320 |
< |
keySet = new AbstractSet<K>() { |
321 |
< |
public Iterator<K> iterator() { |
322 |
< |
return new Iterator<K>() { |
323 |
< |
private Iterator<Entry<K,V>> i = entrySet().iterator(); |
324 |
< |
|
325 |
< |
public boolean hasNext() { |
326 |
< |
return i.hasNext(); |
327 |
< |
} |
328 |
< |
|
329 |
< |
public K next() { |
330 |
< |
return i.next().getKey(); |
331 |
< |
} |
332 |
< |
|
333 |
< |
public void remove() { |
334 |
< |
i.remove(); |
335 |
< |
} |
319 |
> |
if (keySet == null) { |
320 |
> |
keySet = new AbstractSet<K>() { |
321 |
> |
public Iterator<K> iterator() { |
322 |
> |
return new Iterator<K>() { |
323 |
> |
private Iterator<Entry<K,V>> i = entrySet().iterator(); |
324 |
> |
|
325 |
> |
public boolean hasNext() { |
326 |
> |
return i.hasNext(); |
327 |
> |
} |
328 |
> |
|
329 |
> |
public K next() { |
330 |
> |
return i.next().getKey(); |
331 |
> |
} |
332 |
> |
|
333 |
> |
public void remove() { |
334 |
> |
i.remove(); |
335 |
> |
} |
336 |
|
}; |
337 |
< |
} |
337 |
> |
} |
338 |
|
|
339 |
< |
public int size() { |
340 |
< |
return AbstractMap.this.size(); |
341 |
< |
} |
342 |
< |
|
343 |
< |
public boolean isEmpty() { |
344 |
< |
return AbstractMap.this.isEmpty(); |
345 |
< |
} |
346 |
< |
|
347 |
< |
public void clear() { |
348 |
< |
AbstractMap.this.clear(); |
349 |
< |
} |
350 |
< |
|
351 |
< |
public boolean contains(Object k) { |
352 |
< |
return AbstractMap.this.containsKey(k); |
353 |
< |
} |
354 |
< |
}; |
355 |
< |
} |
356 |
< |
return keySet; |
339 |
> |
public int size() { |
340 |
> |
return AbstractMap.this.size(); |
341 |
> |
} |
342 |
> |
|
343 |
> |
public boolean isEmpty() { |
344 |
> |
return AbstractMap.this.isEmpty(); |
345 |
> |
} |
346 |
> |
|
347 |
> |
public void clear() { |
348 |
> |
AbstractMap.this.clear(); |
349 |
> |
} |
350 |
> |
|
351 |
> |
public boolean contains(Object k) { |
352 |
> |
return AbstractMap.this.containsKey(k); |
353 |
> |
} |
354 |
> |
}; |
355 |
> |
} |
356 |
> |
return keySet; |
357 |
|
} |
358 |
|
|
359 |
|
/** |
372 |
|
* method will not all return the same collection. |
373 |
|
*/ |
374 |
|
public Collection<V> values() { |
375 |
< |
if (values == null) { |
376 |
< |
values = new AbstractCollection<V>() { |
377 |
< |
public Iterator<V> iterator() { |
378 |
< |
return new Iterator<V>() { |
379 |
< |
private Iterator<Entry<K,V>> i = entrySet().iterator(); |
380 |
< |
|
381 |
< |
public boolean hasNext() { |
382 |
< |
return i.hasNext(); |
383 |
< |
} |
384 |
< |
|
385 |
< |
public V next() { |
386 |
< |
return i.next().getValue(); |
387 |
< |
} |
388 |
< |
|
389 |
< |
public void remove() { |
390 |
< |
i.remove(); |
391 |
< |
} |
375 |
> |
if (values == null) { |
376 |
> |
values = new AbstractCollection<V>() { |
377 |
> |
public Iterator<V> iterator() { |
378 |
> |
return new Iterator<V>() { |
379 |
> |
private Iterator<Entry<K,V>> i = entrySet().iterator(); |
380 |
> |
|
381 |
> |
public boolean hasNext() { |
382 |
> |
return i.hasNext(); |
383 |
> |
} |
384 |
> |
|
385 |
> |
public V next() { |
386 |
> |
return i.next().getValue(); |
387 |
> |
} |
388 |
> |
|
389 |
> |
public void remove() { |
390 |
> |
i.remove(); |
391 |
> |
} |
392 |
|
}; |
393 |
|
} |
394 |
|
|
395 |
< |
public int size() { |
396 |
< |
return AbstractMap.this.size(); |
397 |
< |
} |
398 |
< |
|
399 |
< |
public boolean isEmpty() { |
400 |
< |
return AbstractMap.this.isEmpty(); |
401 |
< |
} |
402 |
< |
|
403 |
< |
public void clear() { |
404 |
< |
AbstractMap.this.clear(); |
405 |
< |
} |
406 |
< |
|
407 |
< |
public boolean contains(Object v) { |
408 |
< |
return AbstractMap.this.containsValue(v); |
409 |
< |
} |
410 |
< |
}; |
411 |
< |
} |
412 |
< |
return values; |
395 |
> |
public int size() { |
396 |
> |
return AbstractMap.this.size(); |
397 |
> |
} |
398 |
> |
|
399 |
> |
public boolean isEmpty() { |
400 |
> |
return AbstractMap.this.isEmpty(); |
401 |
> |
} |
402 |
> |
|
403 |
> |
public void clear() { |
404 |
> |
AbstractMap.this.clear(); |
405 |
> |
} |
406 |
> |
|
407 |
> |
public boolean contains(Object v) { |
408 |
> |
return AbstractMap.this.containsValue(v); |
409 |
> |
} |
410 |
> |
}; |
411 |
> |
} |
412 |
> |
return values; |
413 |
|
} |
414 |
|
|
415 |
|
public abstract Set<Entry<K,V>> entrySet(); |
439 |
|
* @return <tt>true</tt> if the specified object is equal to this map |
440 |
|
*/ |
441 |
|
public boolean equals(Object o) { |
442 |
< |
if (o == this) |
443 |
< |
return true; |
442 |
> |
if (o == this) |
443 |
> |
return true; |
444 |
|
|
445 |
< |
if (!(o instanceof Map)) |
446 |
< |
return false; |
447 |
< |
Map<K,V> m = (Map<K,V>) o; |
448 |
< |
if (m.size() != size()) |
449 |
< |
return false; |
445 |
> |
if (!(o instanceof Map)) |
446 |
> |
return false; |
447 |
> |
Map<K,V> m = (Map<K,V>) o; |
448 |
> |
if (m.size() != size()) |
449 |
> |
return false; |
450 |
|
|
451 |
|
try { |
452 |
|
Iterator<Entry<K,V>> i = entrySet().iterator(); |
453 |
|
while (i.hasNext()) { |
454 |
|
Entry<K,V> e = i.next(); |
455 |
< |
K key = e.getKey(); |
455 |
> |
K key = e.getKey(); |
456 |
|
V value = e.getValue(); |
457 |
|
if (value == null) { |
458 |
|
if (!(m.get(key)==null && m.containsKey(key))) |
468 |
|
return false; |
469 |
|
} |
470 |
|
|
471 |
< |
return true; |
471 |
> |
return true; |
472 |
|
} |
473 |
|
|
474 |
|
/** |
489 |
|
* @see Set#equals(Object) |
490 |
|
*/ |
491 |
|
public int hashCode() { |
492 |
< |
int h = 0; |
493 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
494 |
< |
while (i.hasNext()) |
495 |
< |
h += i.next().hashCode(); |
496 |
< |
return h; |
492 |
> |
int h = 0; |
493 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
494 |
> |
while (i.hasNext()) |
495 |
> |
h += i.next().hashCode(); |
496 |
> |
return h; |
497 |
|
} |
498 |
|
|
499 |
|
/** |
509 |
|
* @return a string representation of this map |
510 |
|
*/ |
511 |
|
public String toString() { |
512 |
< |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
513 |
< |
if (! i.hasNext()) |
514 |
< |
return "{}"; |
515 |
< |
|
516 |
< |
StringBuilder sb = new StringBuilder(); |
517 |
< |
sb.append('{'); |
518 |
< |
for (;;) { |
519 |
< |
Entry<K,V> e = i.next(); |
520 |
< |
K key = e.getKey(); |
521 |
< |
V value = e.getValue(); |
522 |
< |
sb.append(key == this ? "(this Map)" : key); |
523 |
< |
sb.append('='); |
524 |
< |
sb.append(value == this ? "(this Map)" : value); |
525 |
< |
if (! i.hasNext()) |
526 |
< |
return sb.append('}').toString(); |
527 |
< |
sb.append(", "); |
528 |
< |
} |
512 |
> |
Iterator<Entry<K,V>> i = entrySet().iterator(); |
513 |
> |
if (! i.hasNext()) |
514 |
> |
return "{}"; |
515 |
> |
|
516 |
> |
StringBuilder sb = new StringBuilder(); |
517 |
> |
sb.append('{'); |
518 |
> |
for (;;) { |
519 |
> |
Entry<K,V> e = i.next(); |
520 |
> |
K key = e.getKey(); |
521 |
> |
V value = e.getValue(); |
522 |
> |
sb.append(key == this ? "(this Map)" : key); |
523 |
> |
sb.append('='); |
524 |
> |
sb.append(value == this ? "(this Map)" : value); |
525 |
> |
if (! i.hasNext()) |
526 |
> |
return sb.append('}').toString(); |
527 |
> |
sb.append(", "); |
528 |
> |
} |
529 |
|
} |
530 |
|
|
531 |
|
/** |
568 |
|
* @since 1.6 |
569 |
|
*/ |
570 |
|
public static class SimpleEntry<K,V> |
571 |
< |
implements Entry<K,V>, java.io.Serializable |
571 |
> |
implements Entry<K,V>, java.io.Serializable |
572 |
|
{ |
573 |
< |
private static final long serialVersionUID = -8499721149061103585L; |
573 |
> |
private static final long serialVersionUID = -8499721149061103585L; |
574 |
|
|
575 |
< |
private final K key; |
576 |
< |
private V value; |
575 |
> |
private final K key; |
576 |
> |
private V value; |
577 |
|
|
578 |
|
/** |
579 |
|
* Creates an entry representing a mapping from the specified |
582 |
|
* @param key the key represented by this entry |
583 |
|
* @param value the value represented by this entry |
584 |
|
*/ |
585 |
< |
public SimpleEntry(K key, V value) { |
586 |
< |
this.key = key; |
585 |
> |
public SimpleEntry(K key, V value) { |
586 |
> |
this.key = key; |
587 |
|
this.value = value; |
588 |
< |
} |
588 |
> |
} |
589 |
|
|
590 |
|
/** |
591 |
|
* Creates an entry representing the same mapping as the |
593 |
|
* |
594 |
|
* @param entry the entry to copy |
595 |
|
*/ |
596 |
< |
public SimpleEntry(Entry<? extends K, ? extends V> entry) { |
597 |
< |
this.key = entry.getKey(); |
596 |
> |
public SimpleEntry(Entry<? extends K, ? extends V> entry) { |
597 |
> |
this.key = entry.getKey(); |
598 |
|
this.value = entry.getValue(); |
599 |
< |
} |
599 |
> |
} |
600 |
|
|
601 |
< |
/** |
602 |
< |
* Returns the key corresponding to this entry. |
603 |
< |
* |
604 |
< |
* @return the key corresponding to this entry |
605 |
< |
*/ |
606 |
< |
public K getKey() { |
607 |
< |
return key; |
608 |
< |
} |
609 |
< |
|
610 |
< |
/** |
611 |
< |
* Returns the value corresponding to this entry. |
612 |
< |
* |
613 |
< |
* @return the value corresponding to this entry |
614 |
< |
*/ |
615 |
< |
public V getValue() { |
616 |
< |
return value; |
617 |
< |
} |
618 |
< |
|
619 |
< |
/** |
620 |
< |
* Replaces the value corresponding to this entry with the specified |
621 |
< |
* value. |
622 |
< |
* |
623 |
< |
* @param value new value to be stored in this entry |
624 |
< |
* @return the old value corresponding to the entry |
625 |
< |
*/ |
626 |
< |
public V setValue(V value) { |
627 |
< |
V oldValue = this.value; |
628 |
< |
this.value = value; |
629 |
< |
return oldValue; |
630 |
< |
} |
631 |
< |
|
632 |
< |
/** |
633 |
< |
* Compares the specified object with this entry for equality. |
634 |
< |
* Returns {@code true} if the given object is also a map entry and |
635 |
< |
* the two entries represent the same mapping. More formally, two |
636 |
< |
* entries {@code e1} and {@code e2} represent the same mapping |
637 |
< |
* if<pre> |
638 |
< |
* (e1.getKey()==null ? |
639 |
< |
* e2.getKey()==null : |
640 |
< |
* e1.getKey().equals(e2.getKey())) |
641 |
< |
* && |
642 |
< |
* (e1.getValue()==null ? |
643 |
< |
* e2.getValue()==null : |
644 |
< |
* e1.getValue().equals(e2.getValue()))</pre> |
645 |
< |
* This ensures that the {@code equals} method works properly across |
646 |
< |
* different implementations of the {@code Map.Entry} interface. |
647 |
< |
* |
648 |
< |
* @param o object to be compared for equality with this map entry |
649 |
< |
* @return {@code true} if the specified object is equal to this map |
650 |
< |
* entry |
651 |
< |
* @see #hashCode |
652 |
< |
*/ |
653 |
< |
public boolean equals(Object o) { |
654 |
< |
if (!(o instanceof Map.Entry)) |
655 |
< |
return false; |
656 |
< |
Map.Entry e = (Map.Entry)o; |
657 |
< |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
658 |
< |
} |
659 |
< |
|
660 |
< |
/** |
661 |
< |
* Returns the hash code value for this map entry. The hash code |
662 |
< |
* of a map entry {@code e} is defined to be: <pre> |
663 |
< |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
664 |
< |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
665 |
< |
* This ensures that {@code e1.equals(e2)} implies that |
666 |
< |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
667 |
< |
* {@code e1} and {@code e2}, as required by the general |
668 |
< |
* contract of {@link Object#hashCode}. |
669 |
< |
* |
670 |
< |
* @return the hash code value for this map entry |
671 |
< |
* @see #equals |
672 |
< |
*/ |
673 |
< |
public int hashCode() { |
674 |
< |
return (key == null ? 0 : key.hashCode()) ^ |
675 |
< |
(value == null ? 0 : value.hashCode()); |
676 |
< |
} |
601 |
> |
/** |
602 |
> |
* Returns the key corresponding to this entry. |
603 |
> |
* |
604 |
> |
* @return the key corresponding to this entry |
605 |
> |
*/ |
606 |
> |
public K getKey() { |
607 |
> |
return key; |
608 |
> |
} |
609 |
> |
|
610 |
> |
/** |
611 |
> |
* Returns the value corresponding to this entry. |
612 |
> |
* |
613 |
> |
* @return the value corresponding to this entry |
614 |
> |
*/ |
615 |
> |
public V getValue() { |
616 |
> |
return value; |
617 |
> |
} |
618 |
> |
|
619 |
> |
/** |
620 |
> |
* Replaces the value corresponding to this entry with the specified |
621 |
> |
* value. |
622 |
> |
* |
623 |
> |
* @param value new value to be stored in this entry |
624 |
> |
* @return the old value corresponding to the entry |
625 |
> |
*/ |
626 |
> |
public V setValue(V value) { |
627 |
> |
V oldValue = this.value; |
628 |
> |
this.value = value; |
629 |
> |
return oldValue; |
630 |
> |
} |
631 |
> |
|
632 |
> |
/** |
633 |
> |
* Compares the specified object with this entry for equality. |
634 |
> |
* Returns {@code true} if the given object is also a map entry and |
635 |
> |
* the two entries represent the same mapping. More formally, two |
636 |
> |
* entries {@code e1} and {@code e2} represent the same mapping |
637 |
> |
* if<pre> |
638 |
> |
* (e1.getKey()==null ? |
639 |
> |
* e2.getKey()==null : |
640 |
> |
* e1.getKey().equals(e2.getKey())) |
641 |
> |
* && |
642 |
> |
* (e1.getValue()==null ? |
643 |
> |
* e2.getValue()==null : |
644 |
> |
* e1.getValue().equals(e2.getValue()))</pre> |
645 |
> |
* This ensures that the {@code equals} method works properly across |
646 |
> |
* different implementations of the {@code Map.Entry} interface. |
647 |
> |
* |
648 |
> |
* @param o object to be compared for equality with this map entry |
649 |
> |
* @return {@code true} if the specified object is equal to this map |
650 |
> |
* entry |
651 |
> |
* @see #hashCode |
652 |
> |
*/ |
653 |
> |
public boolean equals(Object o) { |
654 |
> |
if (!(o instanceof Map.Entry)) |
655 |
> |
return false; |
656 |
> |
Map.Entry e = (Map.Entry)o; |
657 |
> |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
658 |
> |
} |
659 |
> |
|
660 |
> |
/** |
661 |
> |
* Returns the hash code value for this map entry. The hash code |
662 |
> |
* of a map entry {@code e} is defined to be: <pre> |
663 |
> |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
664 |
> |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
665 |
> |
* This ensures that {@code e1.equals(e2)} implies that |
666 |
> |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
667 |
> |
* {@code e1} and {@code e2}, as required by the general |
668 |
> |
* contract of {@link Object#hashCode}. |
669 |
> |
* |
670 |
> |
* @return the hash code value for this map entry |
671 |
> |
* @see #equals |
672 |
> |
*/ |
673 |
> |
public int hashCode() { |
674 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
675 |
> |
(value == null ? 0 : value.hashCode()); |
676 |
> |
} |
677 |
|
|
678 |
|
/** |
679 |
|
* Returns a String representation of this map entry. This |
683 |
|
* |
684 |
|
* @return a String representation of this map entry |
685 |
|
*/ |
686 |
< |
public String toString() { |
687 |
< |
return key + "=" + value; |
688 |
< |
} |
686 |
> |
public String toString() { |
687 |
> |
return key + "=" + value; |
688 |
> |
} |
689 |
|
|
690 |
|
} |
691 |
|
|
698 |
|
* @since 1.6 |
699 |
|
*/ |
700 |
|
public static class SimpleImmutableEntry<K,V> |
701 |
< |
implements Entry<K,V>, java.io.Serializable |
701 |
> |
implements Entry<K,V>, java.io.Serializable |
702 |
|
{ |
703 |
< |
private static final long serialVersionUID = 7138329143949025153L; |
703 |
> |
private static final long serialVersionUID = 7138329143949025153L; |
704 |
|
|
705 |
< |
private final K key; |
706 |
< |
private final V value; |
705 |
> |
private final K key; |
706 |
> |
private final V value; |
707 |
|
|
708 |
|
/** |
709 |
|
* Creates an entry representing a mapping from the specified |
712 |
|
* @param key the key represented by this entry |
713 |
|
* @param value the value represented by this entry |
714 |
|
*/ |
715 |
< |
public SimpleImmutableEntry(K key, V value) { |
716 |
< |
this.key = key; |
715 |
> |
public SimpleImmutableEntry(K key, V value) { |
716 |
> |
this.key = key; |
717 |
|
this.value = value; |
718 |
< |
} |
718 |
> |
} |
719 |
|
|
720 |
|
/** |
721 |
|
* Creates an entry representing the same mapping as the |
723 |
|
* |
724 |
|
* @param entry the entry to copy |
725 |
|
*/ |
726 |
< |
public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) { |
727 |
< |
this.key = entry.getKey(); |
726 |
> |
public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) { |
727 |
> |
this.key = entry.getKey(); |
728 |
|
this.value = entry.getValue(); |
729 |
< |
} |
729 |
> |
} |
730 |
> |
|
731 |
> |
/** |
732 |
> |
* Returns the key corresponding to this entry. |
733 |
> |
* |
734 |
> |
* @return the key corresponding to this entry |
735 |
> |
*/ |
736 |
> |
public K getKey() { |
737 |
> |
return key; |
738 |
> |
} |
739 |
> |
|
740 |
> |
/** |
741 |
> |
* Returns the value corresponding to this entry. |
742 |
> |
* |
743 |
> |
* @return the value corresponding to this entry |
744 |
> |
*/ |
745 |
> |
public V getValue() { |
746 |
> |
return value; |
747 |
> |
} |
748 |
|
|
749 |
< |
/** |
750 |
< |
* Returns the key corresponding to this entry. |
751 |
< |
* |
734 |
< |
* @return the key corresponding to this entry |
735 |
< |
*/ |
736 |
< |
public K getKey() { |
737 |
< |
return key; |
738 |
< |
} |
739 |
< |
|
740 |
< |
/** |
741 |
< |
* Returns the value corresponding to this entry. |
742 |
< |
* |
743 |
< |
* @return the value corresponding to this entry |
744 |
< |
*/ |
745 |
< |
public V getValue() { |
746 |
< |
return value; |
747 |
< |
} |
748 |
< |
|
749 |
< |
/** |
750 |
< |
* Replaces the value corresponding to this entry with the specified |
751 |
< |
* value (optional operation). This implementation simply throws |
749 |
> |
/** |
750 |
> |
* Replaces the value corresponding to this entry with the specified |
751 |
> |
* value (optional operation). This implementation simply throws |
752 |
|
* <tt>UnsupportedOperationException</tt>, as this class implements |
753 |
|
* an <i>immutable</i> map entry. |
754 |
< |
* |
755 |
< |
* @param value new value to be stored in this entry |
756 |
< |
* @return (Does not return) |
757 |
< |
* @throws UnsupportedOperationException always |
754 |
> |
* |
755 |
> |
* @param value new value to be stored in this entry |
756 |
> |
* @return (Does not return) |
757 |
> |
* @throws UnsupportedOperationException always |
758 |
|
*/ |
759 |
< |
public V setValue(V value) { |
759 |
> |
public V setValue(V value) { |
760 |
|
throw new UnsupportedOperationException(); |
761 |
|
} |
762 |
|
|
763 |
< |
/** |
764 |
< |
* Compares the specified object with this entry for equality. |
765 |
< |
* Returns {@code true} if the given object is also a map entry and |
766 |
< |
* the two entries represent the same mapping. More formally, two |
767 |
< |
* entries {@code e1} and {@code e2} represent the same mapping |
768 |
< |
* if<pre> |
769 |
< |
* (e1.getKey()==null ? |
770 |
< |
* e2.getKey()==null : |
771 |
< |
* e1.getKey().equals(e2.getKey())) |
772 |
< |
* && |
773 |
< |
* (e1.getValue()==null ? |
774 |
< |
* e2.getValue()==null : |
775 |
< |
* e1.getValue().equals(e2.getValue()))</pre> |
776 |
< |
* This ensures that the {@code equals} method works properly across |
777 |
< |
* different implementations of the {@code Map.Entry} interface. |
778 |
< |
* |
779 |
< |
* @param o object to be compared for equality with this map entry |
780 |
< |
* @return {@code true} if the specified object is equal to this map |
781 |
< |
* entry |
782 |
< |
* @see #hashCode |
783 |
< |
*/ |
784 |
< |
public boolean equals(Object o) { |
785 |
< |
if (!(o instanceof Map.Entry)) |
786 |
< |
return false; |
787 |
< |
Map.Entry e = (Map.Entry)o; |
788 |
< |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
789 |
< |
} |
790 |
< |
|
791 |
< |
/** |
792 |
< |
* Returns the hash code value for this map entry. The hash code |
793 |
< |
* of a map entry {@code e} is defined to be: <pre> |
794 |
< |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
795 |
< |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
796 |
< |
* This ensures that {@code e1.equals(e2)} implies that |
797 |
< |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
798 |
< |
* {@code e1} and {@code e2}, as required by the general |
799 |
< |
* contract of {@link Object#hashCode}. |
800 |
< |
* |
801 |
< |
* @return the hash code value for this map entry |
802 |
< |
* @see #equals |
803 |
< |
*/ |
804 |
< |
public int hashCode() { |
805 |
< |
return (key == null ? 0 : key.hashCode()) ^ |
806 |
< |
(value == null ? 0 : value.hashCode()); |
807 |
< |
} |
763 |
> |
/** |
764 |
> |
* Compares the specified object with this entry for equality. |
765 |
> |
* Returns {@code true} if the given object is also a map entry and |
766 |
> |
* the two entries represent the same mapping. More formally, two |
767 |
> |
* entries {@code e1} and {@code e2} represent the same mapping |
768 |
> |
* if<pre> |
769 |
> |
* (e1.getKey()==null ? |
770 |
> |
* e2.getKey()==null : |
771 |
> |
* e1.getKey().equals(e2.getKey())) |
772 |
> |
* && |
773 |
> |
* (e1.getValue()==null ? |
774 |
> |
* e2.getValue()==null : |
775 |
> |
* e1.getValue().equals(e2.getValue()))</pre> |
776 |
> |
* This ensures that the {@code equals} method works properly across |
777 |
> |
* different implementations of the {@code Map.Entry} interface. |
778 |
> |
* |
779 |
> |
* @param o object to be compared for equality with this map entry |
780 |
> |
* @return {@code true} if the specified object is equal to this map |
781 |
> |
* entry |
782 |
> |
* @see #hashCode |
783 |
> |
*/ |
784 |
> |
public boolean equals(Object o) { |
785 |
> |
if (!(o instanceof Map.Entry)) |
786 |
> |
return false; |
787 |
> |
Map.Entry e = (Map.Entry)o; |
788 |
> |
return eq(key, e.getKey()) && eq(value, e.getValue()); |
789 |
> |
} |
790 |
> |
|
791 |
> |
/** |
792 |
> |
* Returns the hash code value for this map entry. The hash code |
793 |
> |
* of a map entry {@code e} is defined to be: <pre> |
794 |
> |
* (e.getKey()==null ? 0 : e.getKey().hashCode()) ^ |
795 |
> |
* (e.getValue()==null ? 0 : e.getValue().hashCode())</pre> |
796 |
> |
* This ensures that {@code e1.equals(e2)} implies that |
797 |
> |
* {@code e1.hashCode()==e2.hashCode()} for any two Entries |
798 |
> |
* {@code e1} and {@code e2}, as required by the general |
799 |
> |
* contract of {@link Object#hashCode}. |
800 |
> |
* |
801 |
> |
* @return the hash code value for this map entry |
802 |
> |
* @see #equals |
803 |
> |
*/ |
804 |
> |
public int hashCode() { |
805 |
> |
return (key == null ? 0 : key.hashCode()) ^ |
806 |
> |
(value == null ? 0 : value.hashCode()); |
807 |
> |
} |
808 |
|
|
809 |
|
/** |
810 |
|
* Returns a String representation of this map entry. This |
814 |
|
* |
815 |
|
* @return a String representation of this map entry |
816 |
|
*/ |
817 |
< |
public String toString() { |
818 |
< |
return key + "=" + value; |
819 |
< |
} |
817 |
> |
public String toString() { |
818 |
> |
return key + "=" + value; |
819 |
> |
} |
820 |
|
|
821 |
|
} |
822 |
|
|