mirror of
https://github.com/apache/commons-collections.git
synced 2025-02-17 15:35:00 +00:00
[COLLECTIONS-556] Added method SetUtils.identityHashSet.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1681635 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0e11402b98
commit
1918bb005e
@ -22,6 +22,10 @@
|
||||
<body>
|
||||
|
||||
<release version="4.1" date="TBD" description="">
|
||||
<action issue="COLLECTIONS-556" dev="tn" type="add">
|
||||
Added method "SetUtils#identityHashSet()" which returns a new identity HashSet
|
||||
using reference-equality instead of object-equality.
|
||||
</action>
|
||||
<action issue="COLLECTIONS-562" dev="tn" type="update">
|
||||
Upgraded minimum java requirement to Java 6 (up from Java 5).
|
||||
</action>
|
||||
|
@ -18,6 +18,7 @@ package org.apache.commons.collections4;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
@ -151,6 +152,28 @@ public class SetUtils {
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new hash set that matches elements based on <code>==</code> not
|
||||
* <code>equals()</code>.
|
||||
* <p>
|
||||
* <strong>This set will violate the detail of various Set contracts.</note>
|
||||
* As a general rule, don't compare this set to other sets. In particular, you can't
|
||||
* use decorators like {@link ListOrderedSet} on it, which silently assume that these
|
||||
* contracts are fulfilled.</strong>
|
||||
* <p>
|
||||
* <strong>Note that the returned set is not synchronized and is not thread-safe.</strong>
|
||||
* If you wish to use this set from multiple threads concurrently, you must use
|
||||
* appropriate synchronization. The simplest approach is to wrap this map
|
||||
* using {@link java.util.Collections#synchronizedSet(Set)}. This class may throw
|
||||
* exceptions when accessed by concurrent threads without synchronization.
|
||||
*
|
||||
* @return a new identity hash set
|
||||
* @since 4.1
|
||||
*/
|
||||
public static <E> Set<E> identityHashSet() {
|
||||
return Collections.newSetFromMap(new IdentityHashMap<E, Boolean>());
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Returns a synchronized set backed by the given set.
|
||||
@ -255,7 +278,7 @@ public class SetUtils {
|
||||
* avoid non-deterministic behavior:
|
||||
*
|
||||
* <pre>
|
||||
* Set s = SetUtils.synchronizedSet(mySet);
|
||||
* Set s = SetUtils.synchronizedSortedSet(mySet);
|
||||
* synchronized (s) {
|
||||
* Iterator i = s.iterator();
|
||||
* while (i.hasNext()) {
|
||||
|
@ -102,4 +102,19 @@ public class SetUtilsTest extends BulkTest {
|
||||
assertEquals(0, SetUtils.hashCodeForSet(null));
|
||||
}
|
||||
|
||||
public void testIdentityHashSet() {
|
||||
Set<String> set = SetUtils.identityHashSet();
|
||||
String a = new String("a");
|
||||
set.add(a);
|
||||
set.add(new String("b"));
|
||||
set.add(a);
|
||||
|
||||
assertEquals(2, set.size());
|
||||
|
||||
set.add(new String("a"));
|
||||
assertEquals(3, set.size());
|
||||
|
||||
set.remove(a);
|
||||
assertEquals(2, set.size());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user