|
WebObjects 5.4.2 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.webobjects.foundation.NSDictionary
public class NSDictionary
The NSDictionary class declares an API for objects that manage immutable associations of keys and values. This class, or its subclass NSMutableDictionary, is used when a convenient and efficient way is needed to retrieve data associated with an arbitrary key. (For convenience, we use the term dictionary to refer to any instance of one of these classes without specifying its exact class membership.)
A key-value pair within a dictionary is called an entry. Each entry consists of one object that represents the key, and a second object which is that key's value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by
equals
).
An instance of NSDictionary is an immutable dictionary: you establish its entries when it is created, and cannot modify them afterwards. An instance of NSMutableDictionary is a mutable dictionary: you can add or delete entries at any time, and the object automatically allocates memory as needed.
Methods that add entries to dictionaries, whether during construction (for all dictionaries) or modification (for mutable dictionaries), add each value object to the dictionary directly. This means that you must ensure that the keys do not change. If you expect your keys to change for any
reason, you should make copies of the keys and add the copies to the dictionary or otherwise guarantee that the keys will not change with respect to either the equals
or hashCode
methods. java.lang.String
objects are immutable and make good keys.
The NSDictionary methods that provide the basis for all NSDictionary's other the NSDictionary methods that provide the basis for all NSDictionary's other methods; that is, all other methods are implemented in terms of these four. If you create a subclass of NSDictionary, you need only ensure that these base methods work properly. Having done so, you can be sure that all your subclass's inherited methods operate properly.
Method | Description |
count |
Returns the number of entries in the dictionary. |
objectForKey |
Returns the value associated with a given key. |
keysNoCopy |
Returns a natural language array containing the keys in the dictionary. |
objectsNoCopy |
Returns a natural language array containing the objects in the dictionary. |
The other methods declared here operate by invoking one or more of these primitives. The non-primitive methods provide convenient ways of accessing multiple entries at once.
Map
,
HashMap
,
Hashtable
,
NSDictionary._count
,
NSDictionary.objectForKey(java.lang.Object)
,
Serialized FormNested Class Summary |
---|
Nested classes/interfaces inherited from interface com.webobjects.foundation.NSCoding |
---|
NSCoding.Support |
Nested classes/interfaces inherited from interface com.webobjects.foundation.NSKeyValueCodingAdditions |
---|
NSKeyValueCodingAdditions.DefaultImplementation, NSKeyValueCodingAdditions.Utility |
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry |
Field Summary | |
---|---|
static boolean |
CheckForNull
Convenience for the constructor methods |
static NSDictionary |
EmptyDictionary
A NSDictionary instance containing no entries which may be shared. |
static boolean |
IgnoreNull
Convenience for the constructor methods |
Fields inherited from interface com.webobjects.foundation.NSKeyValueCodingAdditions |
---|
KeyPathSeparator |
Constructor Summary | |
---|---|
NSDictionary()
Creates an empty dictionary. |
|
NSDictionary(Dictionary dictionary,
boolean ignoreNull)
Creates a dictionary containing the keys and values found in dictionary . |
|
NSDictionary(Map map)
Creates a dictionary containing the keys and values found in Map . |
|
NSDictionary(Map map,
boolean ignoreNull)
Creates a dictionary containing the keys and values found in Map . |
|
NSDictionary(NSArray objects,
NSArray keys)
Creates an NSDictionary with entries from the contents of the keys and objects NSArrays. |
|
NSDictionary(NSDictionary otherDictionary)
Creates a dictionary containing the keys and values found in otherDictionary . |
|
NSDictionary(Object[] objects,
Object[] keys)
Creates an NSDictionary with entries from the contents of the keys and objects arrays. |
|
NSDictionary(Object object,
Object key)
Creates a dictionary containing a single object object for a single key key . |
Method Summary | |
---|---|
NSArray |
allKeys()
The order of the elements in the returned array is not defined. |
NSArray |
allKeysForObject(Object object)
Finds all occurrences of the value object in the dictionary and returns a new array with the corresponding keys. |
NSArray |
allValues()
The order of the values in the array is not defined. |
Class |
classForCoder()
Conformance to NSCoding. |
void |
clear()
Warning: NSDictionaries are immutable and therefore this method does not adhere to the Java specification. |
Object |
clone()
Returns a copy (an NSDictionary object) of this dictionary. |
boolean |
containsKey(Object key)
NSDictionaries do not allow null keys which differs from the java.util.Map specification. |
boolean |
containsValue(Object value)
NSDictionaries do not allow for null values which differs from the java.util.Map specification. |
int |
count()
This method returns a count of the number of key-value pairs. |
static Object |
decodeObject(NSCoder coder)
Creates an NSDictionary from the data in coder . |
static NSDictionary |
emptyDictionary()
Returns the empty NSDictionary (immutable). |
void |
encodeWithCoder(NSCoder coder)
Encodes the receiver using coder . |
Set |
entrySet()
|
boolean |
equals(Object object)
Compares this dictionary to object . |
Object |
get(Object key)
Conforms to NSDictionary.objectForKey() specification. |
int |
hashCode()
|
HashMap |
hashMap()
|
Hashtable |
hashtable()
|
NSDictionary |
immutableClone()
Since NSDictionaries are immutable, there's no need to make an actual copy. |
boolean |
isEmpty()
|
boolean |
isEqualToDictionary(NSDictionary otherDictionary)
Provided for backward compatibility. |
Enumeration |
keyEnumerator()
This method provides a java.util.Enumeration which can be used to access each key in this dictionary. |
Set |
keySet()
|
protected Object[] |
keysNoCopy()
This method should only be used by subclasses. |
NSMutableDictionary |
mutableClone()
|
Enumeration |
objectEnumerator()
This method provides a java.util.Enumeration which can be used to access each value in this dictionary. |
Object |
objectForKey(Object key)
This method returns the Object associated with key |
NSArray |
objectsForKeys(NSArray keys,
Object notFoundMarker)
This method takes a subset of the dictionary as specified by the array of keys and returns an array of the value objects associated with each of the keys. |
protected Object[] |
objectsNoCopy()
This method should only be used by subclasses. |
Object |
put(Object key,
Object value)
Warning: NSDictionaries are immutable and therefore this method does not adhere to the Java specification. |
void |
putAll(Map t)
Warning: NSDictionaries are immutable and therefore this method does not adhere to the Java specification. |
Object |
remove(Object key)
Warning: NSDictionaries are immutable and therefore this method does not adhere to the Java specification. |
int |
size()
|
void |
takeValueForKey(Object value,
String key)
Conformance to NSKeyValueCoding. |
void |
takeValueForKeyPath(Object value,
String keyPath)
Conformance to NSKeyValueCodingAdditions. |
String |
toString()
|
Object |
valueForKey(String key)
Conformance to NSKeyValueCoding. |
Object |
valueForKeyPath(String keyPath)
Conformance to NSKeyValueCodingAdditions. |
Collection |
values()
Warning: NSDictionaries are immutable and therefore this method does not adhere to the Java specification. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final NSDictionary EmptyDictionary
public static final boolean CheckForNull
public static final boolean IgnoreNull
Constructor Detail |
---|
public NSDictionary()
EmptyDictionary
constant should be used instead.
NSDictionary.EmptyDictionary
public NSDictionary(Object object, Object key)
object
for a single key key
. null
is an illegal value for both object
and key
.
Note: NSDictionary assumes that key objects are immutable. If your key objects are mutable, you should make copies of them and add the copies to the dictionary.
object
- object to be stored in the dictionarykey
- key corresponding to the object to be stored in the dictionarypublic NSDictionary(Object[] objects, Object[] keys)
keys
and objects
arrays. This method steps through objects
and keys
, creating entries in the new dictionary as it goes. Each key object and its corresponding value object
is added directly to the dictionary. None of the objects are permitted to be null
.
Note: NSDictionary assumes that key objects are immutable. If your key objects are mutable, you should make copies of them and add the copies to the dictionary.
objects
- array of objects to be stored in the dictionarykeys
- array of keys corresponding to the object to be stored in the dictionary
java.lang.IllegalArgumentException
- if the object array and the key array do not have the same size, or any element is null
public NSDictionary(NSArray objects, NSArray keys)
keys
and objects
NSArrays. This method steps through objects
and keys
, creating entries in the new dictionary as it goes. Each key object and its corresponding value
object is added directly to the dictionary.
Note: NSDictionary assumes that key objects are immutable. If your key objects are mutable, you should make copies of them and add the copies to the dictionary.
objects
- array of objects to be stored in the dictionarykeys
- array of keys corresponding to the objects to be stored in the dictionary
java.lang.IllegalArgumentException
- if the object array and the key array do not have the same sizepublic NSDictionary(NSDictionary otherDictionary)
otherDictionary
.
otherDictionary
- the input dictionary from which the duplicate dictionary is to be createdpublic NSDictionary(Map map, boolean ignoreNull)
Map
. If ignoreNull
is false
, throws an IllegalArgumentException
if any key or value in dictionary
is null
. Throws a
NullPointerException
if map is null.
map
- the input Map from which the current dictionary is to be createdignoreNull
- true
if null
is to be allowed in the input fields.
java.lang.IllegalArgumentException
- when null
is not allowed and a null
is encounteredMap
public NSDictionary(Map map)
Map
. Throws an IllegalArgumentException
if any key or value in dictionary
is null
. Throws a NullPointerException
if map is null.
map
- the input Map from which the current dictionary is to be created
java.lang.IllegalArgumentException
- when null
is not allowed and a null
is encounteredMap
public NSDictionary(Dictionary dictionary, boolean ignoreNull)
dictionary
. If ignoreNull
is false
, throws an IllegalArgumentException
if any key or value in dictionary
is null
.
dictionary
- the input dictionary from which the current dictionary is to be createdignoreNull
- true
if null
is to be allowed in the input fields.
java.lang.IllegalArgumentException
- when null
is not allowed and a null
is encounteredMethod Detail |
---|
protected Object[] keysNoCopy()
allKeys
except the keys might not be copied.
NSDictionary.objectsNoCopy()
,
NSDictionary.allKeys()
protected Object[] objectsNoCopy()
allValues
except the objects might not be copied.
NSDictionary.keysNoCopy()
,
NSDictionary.allValues()
public int count()
public Object objectForKey(Object key)
key
key
- the key to be searched for
key
, or null
if no value is associated with key
NSDictionary.allKeys()
,
NSDictionary.allValues()
public Hashtable hashtable()
java.util.Hashtable
public HashMap hashMap()
java.util.HashMap
public NSArray allKeysForObject(Object object)
object
in the dictionary and returns a new array with the corresponding keys. Each object in the dictionary is sent an equals
message to determine if it is equalivalent to object
. If no Object matching
object
is found, this method returns an empty array.
object
- the search target in the dictionary
object
NSDictionary.allKeys()
,
NSDictionary.keyEnumerator()
public NSArray objectsForKeys(NSArray keys, Object notFoundMarker)
keys
and returns an array of the value objects associated with each of the keys. The objects in the returned array and the keys
array have a one-for-one correspondence, so that the nth
object in the returned array corresponds to the nth key in keys
. If an object is not found in this dictionary for a corresponding key, the notFoundMarker
Object is placed in that key's position in the resultant array.
keys
- a subset of keysnotFoundMarker
- a marker object indicating that the object corresponding to a key was not found
keys
public boolean isEqualToDictionary(NSDictionary otherDictionary)
equals
instead.
otherDictionary
- the dictionary to compare this one to
true
if both the input dictionary and the this dictionary are equivalent, else false
NSDictionary.equals(java.lang.Object)
public boolean equals(Object object)
object
. If object
is an instanceof NSDictionary
and the contents of object
are equal to these contents, this method returns true
. If not, it returns false
.
Two dictionaries have equal contents if they each hold the same number of entries and, for each key, the corresponding value objects in each dictionary satisfy the equals
test. Note, this method does not adhere to the java.util.Map specification for equals and entrySets cannot
Be compared to each other.
equals
in interface Map
equals
in class Object
object
- the object to compare this dictionary against
true
if this dictionary is equivalent to object
, else false
Object.equals(java.lang.Object)
public NSArray allKeys()
NSDictionary.allValues()
,
NSDictionary.allKeysForObject(java.lang.Object)
,
NSDictionary.keyEnumerator()
public NSArray allValues()
NSDictionary.allKeys()
,
NSDictionary.objectEnumerator()
public Enumeration keyEnumerator()
java.util.Enumeration
which can be used to access each key in this dictionary.
<blockquote> java.util.Enumeration enumerator = myDict.keyEnumerator(); while (enumerator.hasMoreElements()) { Object anObject = enumerator.nextElement(); //code to act on each element } </blockquote>When this method is used with mutable subclasses of NSDictionary, your code shouldn't modify the entries during enumeration. If you intend to modify the entries, use the
allKeys
method to create a "snapshot" of the dictionary's keys. Then use this snapshot to traverse the
entries, modifying them along the way.
Note that the objectEnumerator
method provides a convenient way to access each value in the dictionary.
NSDictionary.allKeys()
,
NSDictionary.allKeysForObject(java.lang.Object)
,
NSDictionary.objectEnumerator()
public Enumeration objectEnumerator()
java.util.Enumeration
which can be used to access each value in this dictionary.
<blockquote> java.util.Enumeration enumerator = myDict.objectEnumerator(); while (enumerator.hasMoreElements()) { Object anObject = enumerator.nextElement(); //code to act on each element } </blockquote>
When this method is used with mutable subclasses of NSDictionary, your code shouldn't modify the entries during enumeration. If you intend to modify the entries, use the allValues
method to create a "snapshot" of the dictionary's values. Work from this snapshot to modify the
values.
NSDictionary.keyEnumerator()
,
NSDictionary.allValues()
public Object valueForKey(String key)
objectForKey
. It provides an entry's value given its key
, or null
if no value is associated with key
.
Additionally, all dictionaries have the keys "allValues", "allKeys", and "count" defined, which provide a result identical to the invocation of the method of that name on this dictionary. If the dictionary contains an actual key which conflicts with these special keys, the real key trumps the
computation. For example, if a dictionary has the key-value pair "count"-"dog" valueForKey
will return "dog" instead of the result of the count
method.
valueForKey
in interface NSKeyValueCoding
key
- the key to be searched for
NSDictionary.objectForKey(java.lang.Object)
,
NSKeyValueCoding.valueForKey(java.lang.String)
,
NSKeyValueCodingAdditions.valueForKeyPath(java.lang.String)
public void takeValueForKey(Object value, String key)
takeValueForKey
in interface NSKeyValueCoding
value
- the input objectkey
- the key corresponding to the object
java.lang.IllegalStateException
NSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String)
,
NSKeyValueCodingAdditions.takeValueForKeyPath(java.lang.Object, java.lang.String)
public Object valueForKeyPath(String keyPath)
keyPath
exists as a distinct key in the dictionary, this method returns the corresponding object in the dictionary by invoking objectForKey
. Otherwise, it recursively invokes valueForKey
for each
segment of the keyPath
on the result of the previous segment.
valueForKeyPath
in interface NSKeyValueCodingAdditions
keyPath
- the key path of the object
keypath
NSDictionary.objectForKey(java.lang.Object)
,
NSKeyValueCodingAdditions.valueForKeyPath(java.lang.String)
,
NSKeyValueCoding.valueForKey(java.lang.String)
public void takeValueForKeyPath(Object value, String keyPath)
takeValueForKeyPath
in interface NSKeyValueCodingAdditions
value
- the input objectkeyPath
- the path of the input object
java.lang.IllegalStateException
NSKeyValueCodingAdditions.takeValueForKeyPath(java.lang.Object, java.lang.String)
,
NSKeyValueCoding.takeValueForKey(java.lang.Object, java.lang.String)
public Class classForCoder()
classForCoder
in interface NSCoding
public static Object decodeObject(NSCoder coder)
coder
.
coder
- the input coder
NSCoding
public void encodeWithCoder(NSCoder coder)
NSCoding
coder
. Object type information along with an object's data is stored.
encodeWithCoder
in interface NSCoding
coder
- an NSCoder object that will be used to encode object of classes that implement this interfaceNSCoder
public int hashCode()
hashCode
in interface Map
hashCode
in class Object
Object.hashCode()
public Object clone()
clone
in class Object
this
NSDictionary.mutableClone()
,
NSDictionary.immutableClone()
,
Object.clone()
public NSDictionary immutableClone()
NSDictionary.mutableClone()
,
NSDictionary.clone()
public NSMutableDictionary mutableClone()
NSDictionary.immutableClone()
,
NSDictionary.clone()
public String toString()
toString
in class Object
public static final NSDictionary emptyDictionary()
This example illustrates the type-safe way to obtain an empty dictionary:
Map<String> s = NSDictionary.emptyArray();Implementation note: Implementations of this method need not create a separate NSDictionary object for each call. Using this method is likely to have comparable cost to using the like-named field. (Unlike this method, the field does not provide type safety.)
NSDictionary.EmptyDictionary
public int size()
size
in interface Map
Map.size()
public boolean isEmpty()
isEmpty
in interface Map
Map.isEmpty()
public boolean containsKey(Object key)
containsKey
in interface Map
Map.containsKey(java.lang.Object)
public boolean containsValue(Object value)
containsValue
in interface Map
Map.containsValue(java.lang.Object)
public Object get(Object key)
get
in interface Map
Map.get(java.lang.Object)
public Object put(Object key, Object value)
put
in interface Map
UnsupportedOperationException
Map.put(K, V)
public Object remove(Object key)
remove
in interface Map
UnsupportedOperationException
Map.remove(java.lang.Object)
public void putAll(Map t)
putAll
in interface Map
UnsupportedOperationException
Map.putAll(java.util.Map)
public void clear()
clear
in interface Map
UnsupportedOperationException
Map.clear()
public Set keySet()
keySet
in interface Map
Map.keySet()
public Collection values()
values
in interface Map
Map.values()
public Set entrySet()
entrySet
in interface Map
Map.keySet()
|
Last updated June 2008 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |