309 |
|
|
310 |
|
/** |
311 |
|
* Compares the specified object with this set for equality. |
312 |
< |
* Returns <tt>true</tt> if the specified object is the same |
313 |
< |
* object as this object, or if it is also a set, and every |
314 |
< |
* element of the specified set, as revealed by a single traversal |
315 |
< |
* of its <tt>iterator()</tt>, is also contained in this set at |
316 |
< |
* the point of call of this method, and no other elements not |
317 |
< |
* present in the given set are contained in this set. |
312 |
> |
* Returns {@code true} if the specified object is the same object |
313 |
> |
* as this object, or if it is also a {@link Set} and the elements |
314 |
> |
* returned by an {@linkplain List#iterator() iterator} over the |
315 |
> |
* specified set are the same as the elements returned by an |
316 |
> |
* iterator over this set. More formally, the two iterators are |
317 |
> |
* considered to return the same elements if they return the same |
318 |
> |
* number of elements and for every element {@code e1} returned by |
319 |
> |
* the iterator over the specified set, there is an element |
320 |
> |
* {@code e2} returned by the iterator over this set such that |
321 |
> |
* {@code (e1==null ? e2==null : e1.equals(e2))}. |
322 |
|
* |
323 |
|
* @param o object to be compared for equality with this set |
324 |
< |
* @return <tt>true</tt> if the specified object is equal to this set |
324 |
> |
* @return {@code true} if the specified object is equal to this set |
325 |
|
*/ |
326 |
|
public boolean equals(Object o) { |
327 |
|
if (o == this) |
329 |
|
if (!(o instanceof Set)) |
330 |
|
return false; |
331 |
|
Set<?> set = (Set<?>)(o); |
332 |
< |
Iterator<?> setIt = set.iterator(); |
332 |
> |
Iterator<?> it = set.iterator(); |
333 |
|
|
334 |
|
// Uses O(n^2) algorithm that is only appropriate |
335 |
|
// for small sets, which CopyOnWriteArraySets should be. |
340 |
|
// Mark matched elements to avoid re-checking |
341 |
|
boolean[] matched = new boolean[len]; |
342 |
|
int k = 0; |
343 |
< |
while (setIt.hasNext()) { |
343 |
> |
outer: while (it.hasNext()) { |
344 |
|
if (++k > len) |
345 |
|
return false; |
346 |
< |
Object x = setIt.next(); |
343 |
< |
boolean found = false; |
346 |
> |
Object x = it.next(); |
347 |
|
for (int i = 0; i < len; ++i) { |
348 |
|
if (!matched[i] && eq(x, elements[i])) { |
349 |
|
matched[i] = true; |
350 |
< |
found = true; |
348 |
< |
break; |
350 |
> |
continue outer; |
351 |
|
} |
352 |
|
} |
353 |
< |
if (!found) |
352 |
< |
return false; |
353 |
> |
return false; |
354 |
|
} |
355 |
|
return k == len; |
356 |
|
} |
361 |
|
private static boolean eq(Object o1, Object o2) { |
362 |
|
return (o1 == null ? o2 == null : o1.equals(o2)); |
363 |
|
} |
363 |
– |
|
364 |
|
} |