See: Description
Package | Description |
---|---|
com.koloboke.collect |
The root package of the collection library.
|
com.koloboke.collect.hash |
Contains basic interfaces and commonly used classes related to containers, based on hash tables.
|
com.koloboke.collect.map |
Contains interfaces of
Map specializations, their factories and cursors. |
com.koloboke.collect.map.hash |
Contains interfaces of
Map specializations, based on hash tables,
their factories and static factory methods. |
com.koloboke.collect.set |
Contains interfaces of
Set specializations and their factories. |
com.koloboke.collect.set.hash |
Contains interfaces of
Set specializations, based on hash tables,
their factories and static factory methods. |
com.koloboke.function |
java.util.function polyfill for all specializations. |
HashSet
class extends
Set
extends Collection
. In this library
HashCharSet
interface extends
CharSet
, which extends Set
and
CharCollection
, which extends
Collection
and Container
.
Also,
the com.koloboke.function
package polyfills java.util.function
functional interface set with the rest specializations (java.util.function
package
defines only some specializations for int
, long
and double
types.)
HashContainerFactory
to configure all factories
which produce hash sets and maps in the application.
Note that all factories in the library are immutable, on changing any configuration a new copy of the factory is returned, with the target configuration changed.
ServiceLoader
. This default factory instance is returned
by getDefaultFactory()
static method in each static factory method holder class.
These classes have a name of container interface plus -s
suffix, for example,
HashIntShortMaps
define static factory methods
which return HashIntShortMap
instances,
delegating to the default
HashIntShortMapFactory
implementation.JDK | The closest equivalent from this library | The recommended equivalent from this library |
---|---|---|
|
|
|
|
|
|
|
|
|
|
|
Collections.unmodifiable*
methods
exist. Second, containers of lesser mutability are implemented in more efficient manner, whenever
possible. So using immutable collections when applicable could improve your application's
performance a bit.
Any operations that change the conceptual container state, e. g. insertions and removals,
as well as that could touch internal representation,
e. g. Container.shrink()
, are disallowed. Other ones
are allowed.
Everything is allowed, except removals of individual elements (entries),
typically names of these operations include "remove" or "retain" verb. Emphasis on "individual"
means that Container.clear()
(i. e. removal all elements or entries
at once) is allowed.
Think about updatable containers as "non-decreasing", which could be "reset"
from time to time by calling clear()
.
In real practice individual removals are rarely needed, so most of the time you should use updatable containers rather than fully mutable ones. On the other hand, prohibition of removals permits faster implementation of hash containers and iterators over many data structures.
All operations are allowed.
In Koloboke Compile, the @Updatable
and
@Mutable
annotations specify that Koloboke
Compile should generate an updatable or a mutable implementation of a container interface,
respectively.
Collection
mutability matrixMethod \ Mutability | Mutable | Updatable | Immutable |
size() |
✓ | ✓ | ✓ |
sizeAsLong() |
✓ | ✓ | ✓ |
isEmpty() |
✓ | ✓ | ✓ |
contains(e) |
✓ | ✓ | ✓ |
containsAll(c)
| ✓ | ✓ | ✓ |
toArray() |
✓ | ✓ | ✓ |
toArray(array) |
✓ | ✓ | ✓ |
iterator() |
✓ | ✓, except Iterator.remove() | |
cursor() |
✓ | ✓, except Cursor.remove() |
|
ensureCapacity(minSize)
| ✓ | ✓ | - |
shrink() |
✓ | ✓ | - |
clear() |
✓ | ✓ | - |
add(e) |
✓ | ✓ | - |
addAll(c) |
✓ | ✓ | - |
remove(e) |
✓ | - | - |
removeAll(c) |
✓ | - | - |
retainAll(c) |
✓ | - | - |
removeIf(filter) | ✓ | - | - |
Map
mutability matrixMethod \ Mutability | Mutable | Updatable | Immutable |
size() |
✓ | ✓ | ✓ |
sizeAsLong() |
✓ | ✓ | ✓ |
isEmpty() |
✓ | ✓ | ✓ |
containsKey(key) |
✓ | ✓ | ✓ |
containsValue(value) |
✓ | ✓ | ✓ |
get(key) |
✓ | ✓ | ✓ |
getOrDefault(key, defaultValue) |
✓ | ✓ | ✓ |
forEach(action) |
✓ | ✓ | ✓ |
cursor() |
✓ | ✓,
except Cursor.remove() | |
keySet() |
✓ | ✓, same mutability
applied to the returned Set | |
values() |
✓ | ✓, same mutability applied to the returned Collection | |
entrySet() |
✓ | ✓, same mutability
applied to the returned Set ,if Immutable - additionally, Map.Entry.setValue(value) not supported | |
ensureCapacity(minSize)
| ✓ | ✓ | - |
shrink() |
✓ | ✓ | - |
clear() |
✓ | ✓ | - |
put(key, value) |
✓ | ✓ | - |
putIfAbsent(key, value) |
✓ | ✓ | - |
computeIfAbsent(key, mappingFunction) |
✓ | ✓ | - |
replace(key, value) |
✓ | ✓ | - |
replace(key, oldValue, newValue) |
✓ | ✓ | - |
putAll(m) |
✓ | ✓ | - |
replaceAll(function) |
✓ | ✓ | - |
compute(key, remappingFunction) |
✓ | ✓, except removing entry on returning null from
the remappingFunction | - |
computeIfPresent(key, remappingFunction) |
✓ | ✓, except removing entry on returning null from
the remappingFunction | - |
merge(key, value, remappingFunction) |
✓ | ✓, except removing entry on returning null from
the remappingFunction | - |
remove(key) |
✓ | - | - |
remove(key, value) |
✓ | - | - |
removeIf(filter) |
✓ | - | - |
See other matrices for information if the concrete method is supported by the given
mutability profile:
Container
.
forEach()
-like methods
which accept closures, the library supplies cursors for every container
.
Iterator |
Cursor |
forEach() |
forEachWhile() |
removeIf() |
|
Available for Collection sub-interfaces in the library |
Yes | ||||
Available for Map sub-interfaces in the library |
Yes | ||||
Coding convenience | High, if elements aren't removed and generic version
of Iterator.next() method is used, Java "for-each" syntax
is applicable. Medium otherwise. |
Medium | High, lambda syntax | ||
Supports early break from the iteration | Yes, by simple break from the loop | Yes, by simple break from the loop | No | Yes, by returning false |
No |
Supports remove of iterated elements (entries) | Yes, by Iterator.remove() |
Yes, by Cursor.remove() |
No | No | Yes, by returning true |
Performance, iteration over Collection |
High, if specialized version of Iterator.next() method
is used. Medium otherwise, because every element is boxed. |
Very high | |||
Performance, iteration over Map |
Medium, Map.Entry objects are allocated |
Very high |
HashCharSet
extends
java.util.Set<Character>
, and made as similar as possible
to java.util.HashSet<Character>
, which extends the same interface. Non-obvious things,
made compatible with JCF in the library:
null
elements, keys and values, despite this is
an antipattern, because most JCF implementations does. Important: hash maps and sets
with Object
keys,
e. g. HashObjDoubleMap
, don't support
null
key by default, you should configure
factory.
withNullKeyAllowed(true)
.
ConcurrentModificationException
on best-effort basis, i. e. they have
fail-fast semantics. See documentation for ConcurrentModificationException
for more information.Float.NaN
!= Float.NaN
(similarly for Double
)
in Java, in this library in containers these values are treated consistently with their boxed
versions (i. e. new Float(Float.NaN)
, all such objects are equal to each other.null
element, key or value. Obviously,
this is by design and can't be fixed.Cloneable
yet. To be fixed, see
the issue.Serializable
yet. To be fixed, see
the issue.byte
, char
or short
keys
can't be complete, i. e. contain all keys of the type, unlike HashSet<Byte>
,
HashSet<Character>
and HashSet<Short>
. There should be 1-2 absent keys.
On attempt of insertion the last keys
HashOverflowException
is thrown.HashOverflowException
is thrown on attempt
of insertion an element or entry beyond the actual limit. java.util.HashMap
and
java.util.HashSet
have higher limit, if there is enough heap space.CharCollection
extends Collection
,
IntFloatMap
extends Map
.
There are also classes with Obj-
prefix, they bring API
additions to collections of objects, if there are no additions for the class or
interface, Obj-
"specializations" are present anyway, for global API symmetry.
IntIntMap map = HashIntIntMaps.newUpdatableMap();
Integer key = 1;
map.put(key, 2); // ambiguous method call
map.put((int) key, 2); // correct
There is one exception from this rule:
ByteCollection.removeByte(byte)
is a specialized version
of Collection.remove(Object)
, but have a different name (the same
for LongCollection
,
FloatCollection
, etc.
for symmetry). This is because remove(int)
in IntCollection
will conflict with
List.remove(int)
method in IntList
(not implemented yet, however).
-As-
infix, is added to the original method name. Examples:
DoubleCollection.toDoubleArray()
, and others
similar, is exceptional from those rules and have special name.Object.equals(Object)
and Object.hashCode()
. Container
factories in the library allow to configure equivalences for elements, keys and values. This allows to implement some functionality very
easy, without defining new subclasses of the existing collections implementations. See
the documentation to ObjCollection.equivalence()
,
ObjObjMap.keyEquivalence()
and
ObjObjMap.valueEquivalence()
methods for more
information.
Collection
interface:ObjCollection.forEachWhile(java.util.function.Predicate)
performs the given action for each element of the collection, while it returns true
.
Cursor
iteration:
ObjCollection.cursor()
. See also
the comparison of iteration ways in the library.Collection
interface
primitive specializations: ByteCollection
,
CharCollection
, etc.
Map
interface:ObjObjMap.cursor()
— cursor
iteration over maps.ObjObjMap.forEachWhile(java.util.function.BiPredicate)
performs the given action for each entry of the map, while it returns true
.ObjObjMap.removeIf(java.util.function.BiPredicate)
removes all of the entries of this map that satisfy the given predicate.ObjIntMap.addValue(Object, int)
and
ObjIntMap.addValue(Object, int, int)
add
the given value to the value associated to the given key. These methods are present
in the map specializations with primitive value.HashMap
, LinkedHashMap
, HashSet
and WeakHashMap
, that allows to control it's memory footprint and performance
characteristics, is loadFactor
constructor argument. IdentityHashMap
don't have even this one. This library allows to tune hash tables very precisely via a bunch
of per-instance methods and factory configurations. See the documentation
to HashContainer
and
HashConfig
classes for more information.