PATH |
- Inherits from:
- Object
- Implements:
- Cloneable
- java.io.Serializable
- NSCoding
- Package:
- com.webobjects.foundation
The NSSet and NSMutableSet classes declare the programmatic interface to an object that manages a set of objects. NSSet provides support for the mathematical concept of a set. A set, both in its mathematical sense and in the implementation of NSSet, is an unordered collection of distinct elements. The NSMutableSet class is provided for sets whose contents may be altered.
NSSet declares the programmatic interface for static sets of objects. You establish a static set's entries when it's created, and thereafter the entries can't be modified. NSMutableSet, on the other hand, declares a programmatic interface for dynamic sets of objects. A dynamic-or mutable-set allows the addition and deletion of entries at any time, automatically allocating memory as needed.
Use sets as an alternative to arrays when the order of elements isn't important and performance in testing whether an object is contained in the set is a consideration-while arrays are ordered, testing for membership is slower than with sets. When testing for set membership, the equals method is invoked only once.
Methods that add entries to sets-whether during construction (for all sets) or modification (for mutable sets)-add each member to the set directly. This means that you must ensure that the members do not change. If you expect your members to change for any reason, you should make copies of them and add the copies to the set.
Table 0-13 describes the NSSet methods that provide the basis for all NSSet's other methods; that is, all other methods are implemented in terms of these three. If you create a subclass of NSSet, 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 members in the set. |
member | Returns the object in the set that is equal to the specified object. |
objectsNoCopy | Returns the actual array of objects in the set. |
NSSet provides methods for querying the elements of the set. The allObjects method returns an array containing the objects in a set. The anyObject method returns some object in the set. Additionally, intersectsSet tests for set intersection, isEqualToSet tests for set equality, and isSubsetOfSet tests for one set being a subset of another.The objectEnumerator method provides for traversing elements of the set one by one.
NSSet provides the following constant as a convenience; you can use it when you need an empty set.
Constant | Type | Description |
EmptySet | NSSet | A shared NSSet instance containing no members. |
- Cloneable
- clone
- java.io.Serializable
- NSCoding
- classForCoder
- decodeObject
- encodeWithCoder
- Constructors
- NSSet
- Counting entries
- count
- Accessing the members
- allObjects
- anyObject
- containsObject
- member
- objectEnumerator
- objectsNoCopy
- Comparing sets
- intersectsSet
- isEqualToSet
- isSubsetOfSet
- Joining sets
- setByIntersectingSet
- setBySubtractingSet
- setByUnioningSet
- Methods inherited from Object
- equals
- hashCode
- toString
- Copying sets
- immutableClone
- mutableClone
public NSSet()
public NSSet(NSArray anArray)
Note: NSSet assumes that the member objects are immutable. If your member objects are mutable, you should make copies of them and add the copies to the set. |
public NSSet(NSSet aSet)
public NSSet(Object object)
Note: NSSet assumes that member objects are immutable. If your member objects are mutable, you should make copies of them and add the copies to the set. |
public NSSet(Object[] objects[])
Note: NSSet assumes that member objects are immutable. If your member objects are mutable, you should make copies of them and add the copies to the set. |
public static Object decodeObject(NSCoder coder)
See Also: NSCoding
public NSArray allObjects()
See Also: anyObject, objectEnumerator
public Object anyObject()
See Also: allObjects, objectEnumerator
public Class classForCoder()
public Object clone()
public boolean containsObject(Object anObject)
See Also: member
public int count()
public void encodeWithCoder(NSCoder aNSCoder)
public boolean equals(Object anObject)
true
. If not, it returns false
.
public int hashCode()
public NSSet immutableClone()
public boolean intersectsSet(NSSet otherSet)
false
otherwise. The result of this method corresponds to the mathematical concept of disjoint sets: if the sets are not disjoint, intersectsSet returns true
, otherwise it returns false
.
See Also: isEqualToSet, isSubsetOfSet
public boolean isEqualToSet(NSSet otherSet)
Two sets have equal contents if they each have the same number of members and if each member matches a member in the other set (as determined by equals).
See Also: intersectsSet, isSubsetOfSet
public boolean isSubsetOfSet(NSSet otherSet)
See Also: intersectsSet, isEqualToSet
public Object member(Object anObject)
public NSMutableSet mutableClone()
public java.util.Enumeration objectEnumerator()
java.util.Enumeration enumerator = mySet.objectEnumerator(); while (enumerator.hasMoreElements()) {{ Object anObject = enumerator.nextElement(); /* code to act on each element */ }
When this method is used with mutable subclasses of NSSet, your code shouldn't modify the set during enumeration. If you intend to modify the set, use the allObjects method to create a "snapshot" of the set's members. Enumerate the snapshot, but make your modifications to the original set.
protected Object[] objectsNoCopy()
public NSSet setByIntersectingSet(NSSet otherSet)
See Also: intersectsSet, isSubsetOfSet, isEqualToSet, setBySubtractingSet, setByUnioningSet
public NSSet setBySubtractingSet(NSSet otherSet)
See Also: intersectsSet, isSubsetOfSet, isEqualToSet, setByIntersectingSet, setByUnioningSet
public NSSet setByUnioningSet(NSSet otherSet)
See Also: intersectsSet, isSubsetOfSet, isEqualToSet, setByIntersectingSet, setBySubtractingSet
public String toString()
© 2001 Apple Computer, Inc. (Last Published April 17, 2001)