Skip to main content
ICT
Lesson AB28 - Sets and Maps
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

A. Sets page 3 of 11

  1. In the Java standard class library, the Set interface defines the operations on an object that represents a set of elements. The Set interface is based on the idea of a mathematical set. A set is a collection that has no duplicate elements.

  2. A Set must not have two elements that are equal as specified by an object’s equals method. For example {7, 11, 13} is a set but {7, 11, 11} is not.

  3. The Set interface allows the following operations:

    • insert an element into the set
    • remove an element from the set
    • test if a given element is in the set
    • iterate over the elements of the set using Iterator

  4. Figure 28.1 below lists the methods of the Set interface that are a part of the AP subset.

    // Adds the specified element to this set if it is
    // not already present and returns true. If this set
    // already contains the specified element, the call
    // leaves this set unchanged and returns false.
    boolean add(Object obj);

    // Returns true if the set contains the specified
    // element, false otherwise.
    boolean contains(Object obj);

    // Removes the specified element from this set if it
    // is present and returns true. Returns false if the
    // element was not in the set.
    boolean remove(Object obj);

    // Returns the number of elements in this set.
    int size();

  5. // Returns an iterator over the elements in the set.
    Iterator iterator()


    Figure 28.1 - Methods of the Set interface included in the AP Subset

  6. Because a set may not have duplicates, the elements of a Set should be immutable (unchangeable) objects. If a Set contained changeable objects, an object could be changed to become equal to another object in the Set.

  7. Because the elements in a set do not have to be in order, the Set interface does not require that the elements in the iterator returned by the iterator method be in any particular order. The order of the values generated by the iterator depends on the class that implements Set.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.