Package com.cedarsoftware.util
Class IdentitySet<T>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<T>
com.cedarsoftware.util.IdentitySet<T>
- Type Parameters:
T- the type of elements maintained by this set
- All Implemented Interfaces:
Iterable<T>,Collection<T>,Set<T>
A lightweight Set implementation that uses object identity (==) instead of equals()
for element comparison. Uses open addressing with linear probing for minimal overhead.
Key features:
- Implements full
Setinterface - Uses object identity (==) not equals() for comparison
- No Entry objects - single Object[] array
- Single identityHashCode call per operation
- Excellent cache locality
This class is a high-performance, drop-in replacement for:
Set<Object> set = Collections.newSetFromMap(new IdentityHashMap<>());
Performance benefits over IdentityHashMap-backed Set:
- No wrapper layer indirection
- No Entry object allocations
- No Boolean.TRUE value storage
- Better CPU cache utilization (contiguous array)
Thread Safety: This class is not thread-safe. If multiple threads access an IdentitySet concurrently, external synchronization is required.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new IdentitySet with default initial capacity (16).IdentitySet(int initialCapacity) Creates a new IdentitySet with the specified initial capacity.IdentitySet(int initialCapacity, float loadFactor) Creates a new IdentitySet with the specified initial capacity and load factor.IdentitySet(Collection<? extends T> c) Creates a new IdentitySet containing the elements of the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds an element to this set using identity comparison.voidclear()Removes all elements from this set.booleanChecks if this set contains the specified element using identity comparison.booleanisEmpty()Returns true if this set contains no elements.iterator()Returns an iterator over the elements in this set.booleanRemoves the specified element from this set using identity comparison.intsize()Returns the number of elements in this set.Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.Set
addAll, containsAll, retainAll, spliterator, toArray, toArray
-
Constructor Details
-
IdentitySet
public IdentitySet()Creates a new IdentitySet with default initial capacity (16). -
IdentitySet
public IdentitySet(int initialCapacity) Creates a new IdentitySet with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity (will be rounded up to power of 2)
-
IdentitySet
public IdentitySet(int initialCapacity, float loadFactor) Creates a new IdentitySet with the specified initial capacity and load factor.- Parameters:
initialCapacity- the initial capacity (will be rounded up to power of 2)loadFactor- the load factor threshold that triggers resize (must be > 0 and < 1)
-
IdentitySet
Creates a new IdentitySet containing the elements of the specified collection.- Parameters:
c- the collection whose elements are to be placed into this set- Throws:
NullPointerException- if the specified collection is null or contains null elements
-
-
Method Details
-
add
Adds an element to this set using identity comparison.- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceSet<T>- Overrides:
addin classAbstractCollection<T>- Parameters:
element- the element to add (must not be null)- Returns:
- true if the element was added (was not already present), false otherwise
- Throws:
NullPointerException- if the element is null
-
contains
Checks if this set contains the specified element using identity comparison.- Specified by:
containsin interfaceCollection<T>- Specified by:
containsin interfaceSet<T>- Overrides:
containsin classAbstractCollection<T>- Parameters:
element- the element to check for- Returns:
- true if the element is present, false otherwise
-
remove
Removes the specified element from this set using identity comparison.- Specified by:
removein interfaceCollection<T>- Specified by:
removein interfaceSet<T>- Overrides:
removein classAbstractCollection<T>- Parameters:
element- the element to remove- Returns:
- true if the element was removed (was present), false otherwise
-
clear
public void clear()Removes all elements from this set.- Specified by:
clearin interfaceCollection<T>- Specified by:
clearin interfaceSet<T>- Overrides:
clearin classAbstractCollection<T>
-
size
public int size()Returns the number of elements in this set.- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein interfaceSet<T>- Specified by:
sizein classAbstractCollection<T>- Returns:
- the number of elements
-
isEmpty
public boolean isEmpty()Returns true if this set contains no elements.- Specified by:
isEmptyin interfaceCollection<T>- Specified by:
isEmptyin interfaceSet<T>- Overrides:
isEmptyin classAbstractCollection<T>- Returns:
- true if empty, false otherwise
-
iterator
Returns an iterator over the elements in this set. The elements are returned in no particular order.
-