PATH  Documentation > Mac OS X > Foundation Reference: Java



Table of Contents

NSSet


Inherits from:
NSObject
Package:
com.apple.yellow.foundation

Class at a Glance


An NSSet object stores an immutable set of objects.

Principal Attributes


Creation



NSSet Creates a set.

Commonly Used Methods



allObjects Returns an array containing the set's member objects.
count Returns the number of objects in the set.
containsObject Indicates whether a given object is present in the set.

Primitive Methods



count
member
objectEnumerator




Class Description


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.

Objects added to a set are not copied; rather, an object is added directly to a set.

NSSet provides methods for querying the elements of the set. allObjects returns an array containing the objects in a set. anyObject returns some object in the set. count returns the number of objects currently in the set. member returns the object in the set that is equal to a specified object. 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.




Method Types


Constructors
NSSet
Counting entries
count
Accessing the members
allObjects
anyObject
containsObject
member
objectEnumerator
Comparing sets
isSubsetOfSet
intersectsSet
isEqualToSet
Joining sets
setByIntersectingSet
setBySubtractingSet
setByUnioningSet


Constructors



NSSet

public NSSet()

Returns an empty set.

public NSSet(Object anObject)

Returns a set containing a single member, anObject.

public NSSet(Object[] objects)

Returns a set containing those objects in objects.

public NSSet(NSSet aSet)

Returns a set containing those objects contained within the set aSet.




Instance Methods



allObjects

public NSArray allObjects()

Returns an array containing the receiver's members, or an empty array if the receiver has no members. The order of the objects in the array isn't defined.

See Also: anyObject, objectEnumerator



anyObject

public Object anyObject()

Returns one of the objects in the set (essentially chosen at random), or null if the set contains no objects.

See Also: allObjects, objectEnumerator



containsObject

public boolean containsObject(Object anObject)

Returns true if anObject is present in the set, false otherwise.

See Also: member



count

public int count()

Returns the number of members in the set.



intersectsSet

public boolean intersectsSet(NSSet otherSet)

Returns true if at least one object in the receiver is also present in otherSet, false otherwise.

See Also: isEqualToSet, isSubsetOfSet



isEqualToSet

public boolean isEqualToSet(NSSet otherSet)

Compares the receiving set to otherSet. If the contents of otherSet are equal to the contents of the receiver, this method returns true. If not, it returns false.

Two sets have equal contents if they each have the same number of members and if each member of one set is present in the other.

See Also: intersectsSet, equals (NSObject), isSubsetOfSet



isSubsetOfSet

public boolean isSubsetOfSet(NSSet otherSet)

Returns true if every object in the receiver is also present in otherSet, false otherwise.

See Also: intersectsSet, isEqualToSet



member

public Object member(Object anObject)

If anObject is present in the set (as determined by equals), the object in the set is returned. Otherwise, member returns null.



objectEnumerator

public java.util.Enumeration objectEnumerator()

Returns an enumerator object that lets you access each object in the set.

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.

See Also: nextElement (NSEnumerator)



setByIntersectingSet

public NSSet setByIntersectingSet(NSSet otherSet)

Returns a set with all objects that are in both the receiver and otherSet.

See Also: intersectsSet, isSubsetOfSet, isEqualToSet, setBySubtractingSet, setByUnioningSet



setBySubtractingSet

public NSSet setBySubtractingSet(NSSet otherSet)

Returns a set with all objects that are in the receiver but not in otherSet.

See Also: intersectsSet, isSubsetOfSet, isEqualToSet, setByIntersectingSet, setByUnioningSet



setByUnioningSet

public NSSet setByUnioningSet(NSSet otherSet)

Returns a set with all objects that are in either the receiver or otherSet or both. If an object is in both, the set contains only one copy.

See Also: intersectsSet, isSubsetOfSet, isEqualToSet, setByIntersectingSet, setBySubtractingSet




Table of Contents