@NotThreadSafe public class UnifiedMap<K,V> extends AbstractMutableMap<K,V> implements Externalizable, BatchIterable<V>
The final result is a Map implementation that's leaner than java.util.HashMap and faster than Trove's THashMap. The best of both approaches unified together, and thus the name UnifiedMap.
Constructor and Description |
---|
UnifiedMap() |
UnifiedMap(int initialCapacity) |
UnifiedMap(int initialCapacity,
float loadFactor) |
UnifiedMap(Map<? extends K,? extends V> map) |
UnifiedMap(Pair<K,V>... pairs) |
Modifier and Type | Method and Description |
---|---|
boolean |
allSatisfy(Predicate<? super V> predicate)
Returns true if the predicate evaluates to true for every element of the iterable or if the iterable is empty.
|
<P> boolean |
allSatisfyWith(Predicate2<? super V,? super P> predicate,
P parameter)
Returns true if the predicate evaluates to true for every element of the collection, or returns false.
|
boolean |
anySatisfy(Predicate<? super V> predicate)
Returns true if the predicate evaluates to true for any element of the iterable.
|
<P> boolean |
anySatisfyWith(Predicate2<? super V,? super P> predicate,
P parameter)
Returns true if the predicate evaluates to true for any element of the collection, or return false.
|
void |
batchForEach(Procedure<? super V> procedure,
int sectionIndex,
int sectionCount) |
void |
clear() |
UnifiedMap<K,V> |
clone() |
<E> MutableMap<K,V> |
collectKeysAndValues(Iterable<E> iterable,
Function<? super E,? extends K> keyFunction,
Function<? super E,? extends V> valueFunction)
Adds all the entries derived from
iterable to this . |
<R> MutableMap<K,R> |
collectValues(Function2<? super K,? super V,? extends R> function)
For each key and value of the map the function is evaluated.
|
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
V |
detect(Predicate<? super V> predicate)
Returns the first element of the iterable for which the predicate evaluates to true or null in the case where no
element returns true.
|
Pair<K,V> |
detect(Predicate2<? super K,? super V> predicate)
Return the first key and value of the map for which the predicate evaluates to true when they are given
as arguments.
|
V |
detectIfNone(Predicate<? super V> predicate,
Function0<? extends V> function)
Returns the first element of the iterable for which the predicate evaluates to true.
|
<P> V |
detectWith(Predicate2<? super V,? super P> predicate,
P parameter)
Returns the first element that evaluates to true for the specified predicate2 and parameter, or null if none
evaluate to true.
|
<P> V |
detectWithIfNone(Predicate2<? super V,? super P> predicate,
P parameter,
Function0<? extends V> function)
Returns the first element of the iterable that evaluates to true for the specified predicate2 and parameter, or
returns the value of evaluating the specified function.
|
Set<Map.Entry<K,V>> |
entrySet() |
boolean |
equals(Object object)
Follows the same general contract as
Map.equals(Object) . |
void |
forEachKey(Procedure<? super K> procedure)
Calls the
procedure with each key of the map. |
void |
forEachKeyValue(Procedure2<? super K,? super V> procedure)
Calls the
procedure with each key-value pair of the map. |
void |
forEachValue(Procedure<? super V> procedure)
Calls the procedure with each value of the map.
|
<P> void |
forEachWith(Procedure2<? super V,? super P> procedure,
P parameter)
The procedure2 is evaluated for each element in the iterable with the specified parameter provided
as the second argument.
|
void |
forEachWithIndex(ObjectIntProcedure<? super V> objectIntProcedure)
Iterates over the iterable passing each element and the current relative int index to the specified instance of
ObjectIntProcedure.
|
V |
get(Object key) |
int |
getBatchCount(int batchSize) |
int |
getCollidingBuckets() |
V |
getIfAbsentPut(K key,
Function0<? extends V> function)
Get and return the value in the Map at the specified key.
|
V |
getIfAbsentPut(K key,
V value)
Get and return the value in the Map at the specified key.
|
<P> V |
getIfAbsentPutWith(K key,
Function<? super P,? extends V> function,
P parameter)
Get and return the value in the Map at the specified key.
|
int |
getMapMemoryUsedInWords()
Returns the number of JVM words that is used by this map.
|
int |
hashCode()
Follows the same general contract as
Map.hashCode() . |
boolean |
isEmpty()
Returns true if this iterable has zero items.
|
Set<K> |
keySet() |
MutableMap<K,V> |
newEmpty()
Creates a new instance of the same type, using the default capacity and growth parameters.
|
MutableMap<K,V> |
newEmpty(int capacity)
Creates a new instance of the same type, using the given capacity and the default growth parameters.
|
static <K,V> UnifiedMap<K,V> |
newMap() |
static <K,V> UnifiedMap<K,V> |
newMap(int size) |
static <K,V> UnifiedMap<K,V> |
newMap(int size,
float loadFactor) |
static <K,V> UnifiedMap<K,V> |
newMap(Map<? extends K,? extends V> map) |
static <K,V> UnifiedMap<K,V> |
newMapWith(Iterable<Pair<K,V>> inputIterable) |
static <K,V> UnifiedMap<K,V> |
newMapWith(Pair<K,V>... pairs) |
static <K,V> UnifiedMap<K,V> |
newWithKeysValues(K key,
V value) |
static <K,V> UnifiedMap<K,V> |
newWithKeysValues(K key1,
V value1,
K key2,
V value2) |
static <K,V> UnifiedMap<K,V> |
newWithKeysValues(K key1,
V value1,
K key2,
V value2,
K key3,
V value3) |
static <K,V> UnifiedMap<K,V> |
newWithKeysValues(K key1,
V value1,
K key2,
V value2,
K key3,
V value3,
K key4,
V value4) |
boolean |
noneSatisfy(Predicate<? super V> predicate)
Returns true if the predicate evaluates to false for every element of the iterable or if the iterable is empty.
|
<P> boolean |
noneSatisfyWith(Predicate2<? super V,? super P> predicate,
P parameter)
Returns true if the predicate evaluates to false for every element of the collection, or return false.
|
V |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> map) |
void |
readExternal(ObjectInput in) |
V |
remove(Object key) |
V |
removeKey(K key)
Remove an entry from the map at the specified
key . |
int |
size()
Returns the number of items in this iterable.
|
ImmutableMap<K,V> |
toImmutable()
Returns an immutable copy of this map.
|
String |
toString()
Returns a string representation of this collection.
|
V |
updateValue(K key,
Function0<? extends V> factory,
Function<? super V,? extends V> function)
Looks up the value associated with
key , applies the function to it, and replaces the value. |
<P> V |
updateValueWith(K key,
Function0<? extends V> factory,
Function2<? super V,? super P,? extends V> function,
P parameter)
Same as
MutableMapIterable.updateValue(Object, Function0, Function) with a Function2 and specified parameter which is
passed to the function. |
Collection<V> |
values() |
UnifiedMap<K,V> |
withKeysValues(K key,
V value) |
UnifiedMap<K,V> |
withKeysValues(K key1,
V value1,
K key2,
V value2) |
UnifiedMap<K,V> |
withKeysValues(K key1,
V value1,
K key2,
V value2,
K key3,
V value3) |
UnifiedMap<K,V> |
withKeysValues(K key1,
V value1,
K key2,
V value2,
K key3,
V value3,
K key4,
V value4) |
void |
writeExternal(ObjectOutput out) |
asSynchronized, asUnmodifiable, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, flatCollect, flip, groupBy, groupByEach, partition, partitionWith, reject, reject, rejectWith, select, select, selectInstancesOf, selectWith, tap, withAllKeyValueArguments, withAllKeyValues, withKeyValue, withoutAllKeys, withoutKey, zip, zipWithIndex
add, aggregateBy, aggregateInPlaceBy, collect, flipUniqueValues, getIfAbsentPutWithKey, groupByUniqueKey, iterator, keysView, keyValuesView, valuesView
asLazy, chunk, contains, each, getFirst, getIfAbsent, getIfAbsentValue, getIfAbsentWith, getLast, ifPresentApply, toArray, toArray
appendString, appendString, appendString, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, containsAll, containsAllArguments, containsAllIterable, count, countWith, flatCollect, forEach, groupBy, groupByEach, groupByUniqueKey, injectInto, injectInto, injectInto, injectInto, injectInto, makeString, makeString, makeString, max, max, maxBy, min, min, minBy, notEmpty, reject, rejectWith, select, selectWith, sumByDouble, sumByFloat, sumByInt, sumByLong, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toBag, toList, toMap, toSet, toSortedBag, toSortedBag, toSortedBagBy, toSortedList, toSortedList, toSortedListBy, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toSortedSetBy, zip, zipWithIndex
forEach
aggregateBy, aggregateInPlaceBy, collect, flipUniqueValues, groupByUniqueKey
add, getIfAbsentPutWithKey
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
getIfAbsent, getIfAbsentValue, getIfAbsentWith, ifPresentApply, keysView, keyValuesView, valuesView
appendString, appendString, appendString, asLazy, chunk, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectInt, collectLong, collectShort, collectWith, contains, containsAll, containsAllArguments, containsAllIterable, count, countWith, each, flatCollect, getFirst, getLast, groupBy, groupByEach, groupByUniqueKey, injectInto, injectInto, injectInto, injectInto, injectInto, makeString, makeString, makeString, max, max, maxBy, min, min, minBy, notEmpty, reject, rejectWith, select, selectWith, sumByDouble, sumByFloat, sumByInt, sumByLong, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, toArray, toArray, toBag, toList, toMap, toSet, toSortedBag, toSortedBag, toSortedBagBy, toSortedList, toSortedList, toSortedListBy, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toSortedSetBy, zip, zipWithIndex
forEach
forEach, iterator, spliterator
public UnifiedMap()
public UnifiedMap(int initialCapacity)
public UnifiedMap(int initialCapacity, float loadFactor)
public static <K,V> UnifiedMap<K,V> newMap()
public static <K,V> UnifiedMap<K,V> newMap(int size)
public static <K,V> UnifiedMap<K,V> newMap(int size, float loadFactor)
public static <K,V> UnifiedMap<K,V> newMap(Map<? extends K,? extends V> map)
public static <K,V> UnifiedMap<K,V> newMapWith(Pair<K,V>... pairs)
public static <K,V> UnifiedMap<K,V> newMapWith(Iterable<Pair<K,V>> inputIterable)
public static <K,V> UnifiedMap<K,V> newWithKeysValues(K key, V value)
public static <K,V> UnifiedMap<K,V> newWithKeysValues(K key1, V value1, K key2, V value2)
public static <K,V> UnifiedMap<K,V> newWithKeysValues(K key1, V value1, K key2, V value2, K key3, V value3)
public static <K,V> UnifiedMap<K,V> newWithKeysValues(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
public UnifiedMap<K,V> withKeysValues(K key, V value)
public UnifiedMap<K,V> withKeysValues(K key1, V value1, K key2, V value2, K key3, V value3)
public UnifiedMap<K,V> withKeysValues(K key1, V value1, K key2, V value2, K key3, V value3, K key4, V value4)
public UnifiedMap<K,V> clone()
clone
in interface MutableMap<K,V>
clone
in class AbstractMutableMap<K,V>
public MutableMap<K,V> newEmpty()
MutableMapIterable
newEmpty
in interface MutableMap<K,V>
newEmpty
in interface MutableMapIterable<K,V>
public MutableMap<K,V> newEmpty(int capacity)
AbstractMutableMap
newEmpty
in class AbstractMutableMap<K,V>
public V updateValue(K key, Function0<? extends V> factory, Function<? super V,? extends V> function)
MutableMapIterable
key
, applies the function
to it, and replaces the value. If there
is no value associated with key
, starts it off with a value supplied by factory
.updateValue
in interface MutableMapIterable<K,V>
updateValue
in class AbstractMutableMapIterable<K,V>
public <P> V updateValueWith(K key, Function0<? extends V> factory, Function2<? super V,? super P,? extends V> function, P parameter)
MutableMapIterable
MutableMapIterable.updateValue(Object, Function0, Function)
with a Function2 and specified parameter which is
passed to the function.updateValueWith
in interface MutableMapIterable<K,V>
updateValueWith
in class AbstractMutableMapIterable<K,V>
public V getIfAbsentPut(K key, Function0<? extends V> function)
MutableMapIterable
getIfAbsentPut
in interface MutableMapIterable<K,V>
getIfAbsentPut
in class AbstractMutableMapIterable<K,V>
public V getIfAbsentPut(K key, V value)
MutableMapIterable
getIfAbsentPut
in interface MutableMapIterable<K,V>
getIfAbsentPut
in class AbstractMutableMapIterable<K,V>
public <P> V getIfAbsentPutWith(K key, Function<? super P,? extends V> function, P parameter)
MutableMapIterable
getIfAbsentPutWith
in interface MutableMapIterable<K,V>
getIfAbsentPutWith
in class AbstractMutableMapIterable<K,V>
public int getCollidingBuckets()
public int getMapMemoryUsedInWords()
public V get(Object key)
get
in interface Map<K,V>
get
in interface MapIterable<K,V>
Map.get(Object)
public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in interface MapIterable<K,V>
Map.containsKey(Object)
public boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in interface MapIterable<K,V>
Map.containsValue(Object)
public void forEachKeyValue(Procedure2<? super K,? super V> procedure)
MapIterable
procedure
with each key-value pair of the map.
final Collection<String> collection = new ArrayList<String>(); MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three"); map.forEachKeyValue(new Procedure2<Integer, String>() { public void value(final Integer key, final String value) { collection.add(String.valueOf(key) + value); } }); Verify.assertContainsAll(collection, "1One", "2Two", "3Three");
forEachKeyValue
in interface MapIterable<K,V>
public <E> MutableMap<K,V> collectKeysAndValues(Iterable<E> iterable, Function<? super E,? extends K> keyFunction, Function<? super E,? extends V> valueFunction)
MutableMap
iterable
to this
. The key and value for each entry
is determined by applying the keyFunction
and valueFunction
to each item in
collection
. Any entry in map
that has the same key as an entry in this
will have its value replaced by that in map
.collectKeysAndValues
in interface MutableMap<K,V>
public V removeKey(K key)
MutableMapIterable
key
.removeKey
in interface MutableMapIterable<K,V>
Map.remove(Object)
public int getBatchCount(int batchSize)
getBatchCount
in interface BatchIterable<V>
public void batchForEach(Procedure<? super V> procedure, int sectionIndex, int sectionCount)
batchForEach
in interface BatchIterable<V>
public void forEachKey(Procedure<? super K> procedure)
MapIterable
procedure
with each key of the map.
final Collection<Integer> result = new ArrayList<Integer>(); MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3"); map.forEachKey(new CollectionAddProcedure<Integer>(result)); Verify.assertContainsAll(result, 1, 2, 3);
forEachKey
in interface MapIterable<K,V>
forEachKey
in class AbstractMapIterable<K,V>
public void forEachValue(Procedure<? super V> procedure)
MapIterable
Set<String> result = UnifiedSet.newSet(); MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four"); map.forEachValue(new CollectionAddProcedure<String>(result)); Verify.assertSetsEqual(UnifiedSet.newSetWith("One", "Two", "Three", "Four"), result);
forEachValue
in interface MapIterable<K,V>
forEachValue
in class AbstractMapIterable<K,V>
public boolean isEmpty()
RichIterable
isEmpty
in interface Map<K,V>
isEmpty
in interface RichIterable<V>
isEmpty
in class AbstractRichIterable<V>
public int size()
RichIterable
size
in interface Map<K,V>
size
in interface RichIterable<V>
size
in interface BatchIterable<V>
public boolean equals(Object object)
MapIterable
Map.equals(Object)
.public int hashCode()
MapIterable
Map.hashCode()
.public String toString()
AbstractRichIterable
This implementation creates an empty string buffer, appends a left square bracket, and iterates over the collection appending the string representation of each element in turn. After appending each element except the last, the string ", " is appended. Finally a right bracket is appended. A string is obtained from the string buffer, and returned.
toString
in interface MapIterable<K,V>
toString
in interface RichIterable<V>
toString
in class AbstractRichIterable<V>
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
readExternal
in interface Externalizable
IOException
ClassNotFoundException
public void writeExternal(ObjectOutput out) throws IOException
writeExternal
in interface Externalizable
IOException
public void forEachWithIndex(ObjectIntProcedure<? super V> objectIntProcedure)
InternalIterable
Example using a Java 8 lambda:
people.forEachWithIndex((Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
Example using an anonymous inner class:
people.forEachWithIndex(new ObjectIntProcedure() { public void value(Person person, int index) { LOGGER.info("Index: " + index + " person: " + person.getName()); } });
forEachWithIndex
in interface InternalIterable<V>
forEachWithIndex
in class AbstractMapIterable<K,V>
public <P> void forEachWith(Procedure2<? super V,? super P> procedure, P parameter)
InternalIterable
Example using a Java 8 lambda:
people.forEachWith((Person person, Person other) -> { if (person.isRelatedTo(other)) { LOGGER.info(person.getName()); } }, fred);
Example using an anonymous inner class:
people.forEachWith(new Procedure2() { public void value(Person person, Person other) { if (person.isRelatedTo(other)) { LOGGER.info(person.getName()); } } }, fred);
forEachWith
in interface InternalIterable<V>
forEachWith
in class AbstractMapIterable<K,V>
public <R> MutableMap<K,R> collectValues(Function2<? super K,? super V,? extends R> function)
MapIterable
e.g. peopleByCity.collectValues(new Function2<City, Person, String>() { public String value(City city, Person person) { return person.getFirstName() + " " + person.getLastName(); } });
collectValues
in interface MapIterable<K,V>
collectValues
in interface MutableMap<K,V>
collectValues
in interface MutableMapIterable<K,V>
collectValues
in interface UnsortedMapIterable<K,V>
collectValues
in class AbstractMutableMap<K,V>
public Pair<K,V> detect(Predicate2<? super K,? super V> predicate)
MapIterable
e.g. peopleByCity.detect(new Predicate2<City, Person>() { public boolean accept(City city, Person person) { return city.getName().equals("Anytown") && person.getLastName().equals("Smith"); } });
detect
in interface MapIterable<K,V>
detect
in class AbstractMutableMapIterable<K,V>
public V detect(Predicate<? super V> predicate)
RichIterable
Example using a Java 8 lambda expression:
Person person = people.detect(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
Example using an anonymous inner class:
Person person = people.detect(new Predicate<Person>() { public boolean value(Person person) { return person.getFirstName().equals("John") && person.getLastName().equals("Smith"); } });
detect
in interface RichIterable<V>
detect
in class AbstractMapIterable<K,V>
public <P> V detectWith(Predicate2<? super V,? super P> predicate, P parameter)
RichIterable
Example using a Java 8 lambda expression:
Person person = people.detectWith((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
Example using an anonymous inner class:
Person person = people.detectWith(new Predicate2<Person, String>() { public boolean value(Person person, String fullName) { return person.getFullName().equals(fullName); } }, "John Smith");
detectWith
in interface RichIterable<V>
detectWith
in class AbstractMapIterable<K,V>
public V detectIfNone(Predicate<? super V> predicate, Function0<? extends V> function)
RichIterable
detectIfNone
in interface RichIterable<V>
detectIfNone
in class AbstractMapIterable<K,V>
public <P> V detectWithIfNone(Predicate2<? super V,? super P> predicate, P parameter, Function0<? extends V> function)
RichIterable
detectWithIfNone
in interface RichIterable<V>
detectWithIfNone
in class AbstractMapIterable<K,V>
public boolean anySatisfy(Predicate<? super V> predicate)
RichIterable
anySatisfy
in interface RichIterable<V>
anySatisfy
in class AbstractMapIterable<K,V>
public <P> boolean anySatisfyWith(Predicate2<? super V,? super P> predicate, P parameter)
RichIterable
anySatisfyWith
in interface RichIterable<V>
anySatisfyWith
in class AbstractMapIterable<K,V>
public boolean allSatisfy(Predicate<? super V> predicate)
RichIterable
allSatisfy
in interface RichIterable<V>
allSatisfy
in class AbstractMapIterable<K,V>
public <P> boolean allSatisfyWith(Predicate2<? super V,? super P> predicate, P parameter)
RichIterable
allSatisfyWith
in interface RichIterable<V>
allSatisfyWith
in class AbstractMapIterable<K,V>
public boolean noneSatisfy(Predicate<? super V> predicate)
RichIterable
noneSatisfy
in interface RichIterable<V>
noneSatisfy
in class AbstractMapIterable<K,V>
public <P> boolean noneSatisfyWith(Predicate2<? super V,? super P> predicate, P parameter)
RichIterable
noneSatisfyWith
in interface RichIterable<V>
noneSatisfyWith
in class AbstractMapIterable<K,V>
public ImmutableMap<K,V> toImmutable()
MutableMapIterable
toImmutable
in interface MapIterable<K,V>
toImmutable
in interface MutableMapIterable<K,V>
toImmutable
in interface UnsortedMapIterable<K,V>
toImmutable
in class AbstractMutableMap<K,V>
Copyright © 2004–2016. All rights reserved.