Tuesday 21 March 2017

Accessing a Collection via an Iterator & Storing User-Defined Classes in Collections - Java Tutorials

Accessing a Collection via an Iterator

Often, you will want to cycle through the elements in a collection. For example, you might want to display each element. By far, the easiest way to do this is to employ an iterator, an object that implements either the Iterator or the ListIterator interface. Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements. 


Using an Iterator

Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time. In general, to use an iterator to cycle through the contents of a collection, follow these steps:
  1. Obtain an iterator to the start of the collection by calling the collection’s iterator( ) method.
  2. Set up a loop that makes a call to hasNext( ). Have the loop iterate as long as hasNext( ) returns true.
  3. Within the loop, obtain each element by calling next( ).


The Methods Declared by Iterator

boolean hasNext( ):  Returns true if there are more elements. Otherwise, returns false.

Object next( ):  Returns the next element. Throws NoSuchElementException if there is not a next element.

void remove( ):  Removes the current element. Throws IllegalStateException if an attempt is made to call remove( ) that is not preceded by a call to next( ).


The Methods Declared by ListIterator

void add(Object obj):  Inserts obj into the list in front of the element that will be returned by the next call to next( ).

boolean hasNext( ):  Returns true if there is a next element. Otherwise, returns false.

boolean hasPrevious( ):  Returns true if there is a previous element. Otherwise, returns false.

Object next( ):  Returns the next element. A NoSuchElementException is thrown if there is not a next element.

int nextIndex( ):  Returns the index of the next element. If there is not a next element, returns the size of the list.

Object previous( ):  Returns the previous element. A NoSuchElementException is thrown if there is not a previous element.

int previousIndex( ):  Returns the index of the previous element. If there is not a previous element, returns −1.

void remove( ):  Removes the current element from the list. An IllegalStateException is thrown if remove( ) is called before next( ) or previous( ) is invoked.

void set(Object obj):  Assigns obj to the current element. This is the element last returned by a call to either next( ) or previous( ).


For collections that implement List, you can also obtain an iterator by calling ListIterator. As explained, a list iterator gives you the ability to access the collection in either the forward or backward direction and lets you modify an element. Otherwise, ListIterator is used just like Iterator.

Here is an example that implements these steps, demonstrating both Iterator and ListIterator. It uses an ArrayList object, but the general principles apply to any type of collection. Of course, ListIterator is available only to those collections that implement the List interface.

  // Demonstrate iterators.
  import java.util.*;

  class IteratorDemo {
    public static void main(String args[]) {
      // create an array list
      ArrayList al = new ArrayList();

      // add elements to the array list
      al.add("C");
      al.add("A");
      al.add("E");
      al.add("B");
      al.add("D");
      al.add("F");

      // use iterator to display contents of al
      System.out.print("Original contents of al: ");
      Iterator itr = al.iterator();
      while(itr.hasNext()) {
        Object element = itr.next();
        System.out.print(element + " ");
      }
      System.out.println();

      // modify objects being iterated
      ListIterator litr = al.listIterator();
      while(litr.hasNext()) {
        Object element = litr.next();
        litr.set(element + "+");
      }
      
      System.out.print("Modified contents of al: ");
      itr = al.iterator();
      while(itr.hasNext()) {
        Object element = itr.next();
        System.out.print(element + " ");
      }
      System.out.println();

      // now, display the list backwards
      System.out.print("Modified list backwards: ");
      while(litr.hasPrevious()) {
        Object element = litr.previous();
        System.out.print(element + " ");
      }
      System.out.println();
    }
  }

The output is shown here:

  Original contents of al: C A E B D F
  Modified contents of al: C+ A+ E+ B+ D+ F+
  Modified list backwards: F+ D+ B+ E+ A+ C+

Pay special attention to how the list is displayed in reverse. After the list is modified, litr points to the end of the list. (Remember, litr.hasNext( ) returns false when the end of the list has been reached.) To traverse the list in reverse, the program continues to use litr, but this time it checks to see whether it has a previous element. As long as it does, that element is obtained and displayed.




Storing User-Defined Classes in Collections

For the sake of simplicity, the foregoing examples have stored built-in objects, such as String or Integer, in a collection. Of course, collections are not limited to the storage of built-in objects. Quite the contrary. The power of collections is that they can store any type of object, including objects of classes that you create. For example, consider the following example that uses a LinkedList to store mailing addresses:

  // A simple mailing list example.
  import java.util.*;

  class Address {
    private String name;
    private String street;
    private String city;
    private String state;
    private String code;

    Address(String n, String s, String c,
            String st, String cd) {
      name = n;
      street = s;
      city = c;
      state = st;
      code = cd;
    }

    public String toString() {
      return name + "\n" + street + "\n" +
             city + " " + state + " " + code;
    }
  }

  class MailList {
    public static void main(String args[]) {
      LinkedList ml = new LinkedList();

      // add elements to the linked list
      ml.add(new Address("J.W. West", "11 Oak Ave",
                         "Urbana", "IL", "61801"));
      ml.add(new Address("Ralph Baker", "1142 Maple Lane",
                         "Mahomet", "IL", "61853"));
      ml.add(new Address("Tom Carlton", "867 Elm St",
                         "Champaign", "IL", "61820"));

      Iterator itr = ml.iterator();
      while(itr.hasNext()) {
        Object element = itr.next();
        System.out.println(element + "\n");
      }
      System.out.println();
    }
  }

The output from the program is shown here:

  J.W. West
  11 Oak Ave
  Urbana IL 61801
  Ralph Baker
  1142 Maple Lane
  Mahomet IL 61853
  Tom Carlton
  867 Elm St
  Champaign IL 61820

Aside from storing a user-defined class in a collection, another important thing to notice about the preceding program is that it is quite short. When you consider that it sets up a linked list that can store, retrieve, and process mailing addresses in about 50 lines of code, the power of the collections framework begins to become apparent. As most readers know, if all of this functionality had to be coded manually, the program would be several times longer. Collections offer off-the-shelf solutions to a wide variety of programming problems. You should use them whenever the situation presents itself.


The RandomAccess Interface

Java 2, version 1.4 adds the RandomAccess interface. This interface contains no members. However, by implementing this interface, a collection signals that it supports efficient random access to its elements. Although a collection might support random access, it might not do so efficiently. By checking for the RandomAccess interface, client code can determine at run time whether a collection is suitable for certain types of random access operations—especially as they apply to large collections. (You can use instanceof to determine if a class implements an interface.) RandomAccess is implemented by ArrayList and by the legacy Vector class.

No comments:

Post a Comment