public class IntIntMap extends Object implements Iterable<IntIntMap.Entry>
This class performs fast contains and remove (typically O(1), worst case O(n) but that is rare in practice). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.
Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.
| Modifier and Type | Class and Description |
|---|---|
static class |
IntIntMap.Entries |
static class |
IntIntMap.Entry |
static class |
IntIntMap.Keys |
static class |
IntIntMap.Values |
| Modifier and Type | Field and Description |
|---|---|
int |
size |
| Constructor and Description |
|---|
IntIntMap()
Creates a new map with an initial capacity of 51 and a load factor of 0.8.
|
IntIntMap(int initialCapacity)
Creates a new map with a load factor of 0.8.
|
IntIntMap(int initialCapacity,
float loadFactor)
Creates a new map with the specified initial capacity and load factor.
|
IntIntMap(IntIntMap map)
Creates a new map identical to the specified map.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear() |
void |
clear(int maximumCapacity)
Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
|
boolean |
containsKey(int key) |
boolean |
containsValue(int value)
Returns true if the specified value is in the map.
|
void |
ensureCapacity(int additionalCapacity)
Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.
|
IntIntMap.Entries |
entries()
Returns an iterator for the entries in the map.
|
boolean |
equals(Object obj) |
int |
findKey(int value,
int notFound)
Returns the key for the specified value, or null if it is not in the map.
|
int |
get(int key,
int defaultValue) |
int |
getAndIncrement(int key,
int defaultValue,
int increment)
Returns the key's current value and increments the stored value.
|
int |
hashCode() |
boolean |
isEmpty()
Returns true if the map is empty.
|
Iterator<IntIntMap.Entry> |
iterator() |
IntIntMap.Keys |
keys()
Returns an iterator for the keys in the map.
|
boolean |
notEmpty()
Returns true if the map has one or more items.
|
void |
put(int key,
int value) |
int |
put(int key,
int value,
int defaultValue)
Returns the old value associated with the specified key, or the specified default value.
|
void |
putAll(IntIntMap map) |
int |
remove(int key,
int defaultValue)
Returns the value for the removed key, or the default value if the key is not in the map.
|
void |
shrink(int maximumCapacity)
Reduces the size of the backing arrays to be the specified capacity / locateKey, or less.
|
String |
toString() |
IntIntMap.Values |
values()
Returns an iterator for the values in the map.
|
forEach, spliteratorpublic IntIntMap()
public IntIntMap(int initialCapacity)
initialCapacity - If not a power of two, it is increased to the next nearest power of two.public IntIntMap(int initialCapacity,
float loadFactor)
initialCapacity - If not a power of two, it is increased to the next nearest power of two.public IntIntMap(IntIntMap map)
public void put(int key,
int value)
public int put(int key,
int value,
int defaultValue)
public void putAll(IntIntMap map)
public int get(int key,
int defaultValue)
public int getAndIncrement(int key,
int defaultValue,
int increment)
public int remove(int key,
int defaultValue)
public boolean notEmpty()
public boolean isEmpty()
public void shrink(int maximumCapacity)
public void clear(int maximumCapacity)
public void clear()
public boolean containsValue(int value)
public boolean containsKey(int key)
public int findKey(int value,
int notFound)
public void ensureCapacity(int additionalCapacity)
public Iterator<IntIntMap.Entry> iterator()
iterator in interface Iterable<IntIntMap.Entry>public IntIntMap.Entries entries()
If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called.
Use the IntIntMap.Entries constructor for nested or multithreaded iteration.
public IntIntMap.Values values()
If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called.
Use the IntIntMap.Entries constructor for nested or multithreaded iteration.
public IntIntMap.Keys keys()
If Collections.allocateIterators is false, the same iterator instance is returned each time this method is called.
Use the IntIntMap.Entries constructor for nested or multithreaded iteration.
Copyright © 2021. All rights reserved.