T
- The type of objects to store in the mappublic interface PersistentMap<T> extends Map<String,T>
Map
that is persistent, meaning
that its contents will continue to be available from one program run to the
next. Basically, it is a container where you can store information
semi-permanently, so that your program(s) can access it again later.
A Map
is an object that maps keys to values. In a
PersistentMap
, the keys are Strings. You can think of the keys
in a persistent map as being string identifiers that you can use to identify
objects that you save now, and then recall again in later programs (or
separate executions of the same program). A map cannot have any duplicate
keys, and each unique key maps to at most one value.
Other than the fact that a persistent map keeps its values from one execution
of a program to the next, you use it just like any other Map
. The
most basic operations are illustrated below.
PersistentMap<UserProfile> map = ...; // Use put(key, value) to save a value under a given key map.put("stedwar2", someObject); // Use get(key) to look up the object saved for that key UserProfile profile = map.get("stedwar2");
Note that if you have saved an object under a given key, and later modify
that object in some way, you need to call put()
again in
order for those new changes to be recorded in the map.
Modifier and Type | Method and Description |
---|---|
void |
clear()
Remove all of the key/value associations from this map.
|
boolean |
containsKey(Object key)
Returns true if this map contains a mapping for the specified key.
|
boolean |
containsValue(Object value)
Returns true if this map maps one or more keys to the specified value.
|
Set<Map.Entry<String,T>> |
entrySet()
Returns a set of all entries (that is, key/value pairs) stored in this
map.
|
T |
get(Object key)
Returns the value to which the specified key is mapped, or null if this
map contains no mapping for the key.
|
boolean |
isEmpty()
Returns
true if this map contains no key-value mappings. |
Set<String> |
keySet()
Returns the set of all keys currently in use in this map--that is, all
keys that are mapped to values.
|
T |
put(String key,
T value)
Stores a given object in the map by associating it with the given key.
|
void |
putAll(Map<? extends String,? extends T> externalMap)
Copies all of the mappings from the specified map into this map.
|
T |
remove(Object key)
Remove an object from this map by specifying its key.
|
int |
size()
Returns the number of key-value mappings in this map.
|
Collection<T> |
values()
Returns a collection representing all of the values stored in this map.
|
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
void clear()
boolean containsKey(Object key)
containsKey
in interface Map<String,T>
key
- The key whose presence in this map is to be tested, which must
be a non-null, non-empty string.boolean containsValue(Object value)
containsValue
in interface Map<String,T>
value
- The value whose presence in the map is being tested.Set<Map.Entry<String,T>> entrySet()
Entry
object represents a key together with its
value, and provides getKey()
and getValue()
methods to access them.T get(Object key)
T
, then null will be returned. Use
containsKey
to determine if a mapping
exists.get
in interface Map<String,T>
key
- The key whose associated value is to be returned. This key
must be a non-null, non-empty string.boolean isEmpty()
true
if this map contains no key-value mappings.Set<String> keySet()
put()
or remove()
on the map while looping over
the set of keys).T put(String key, T value)
put
in interface Map<String,T>
key
- The key with which the specified value is to be associated,
which must be a non-null, non-empty string.value
- The object to be associated with the key in the map.void putAll(Map<? extends String,? extends T> externalMap)
put(k, v)
on this map once for each mapping
from key k
to value v
in the other map. The
behavior of this operation is undefined if the other map is modified
while the operation is in progress.T remove(Object key)
int size()