Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 

JavaTM Programming Language Basics, Part 2
Lesson 5: Collections

[<<BACK] [CONTENTS] [NEXT>>]

A collection is an object that contains other objects and provides methods for working on the objects it contains. A collection can consist of the same types of objects, but can contain objects of different types too.

This lesson adapts the RMIClient2 program from Part 2, Lesson 2: User Interfaces Revisited to use the Collections application programming interface (API) to maintain and print a list of unique customer IDs. The customer IDs are all objects of type String and represent the same type of information, a customer ID. You could, however, have a collection object that contains objects of type String, Integer, and Double if it makes sense in your application.


About Collections

The Collection classes available to use in programs implement Collection interfaces. Interfaces are abstract data types that let collections be manipulated independently of their representation details. There are three primary types of collection interfaces: List, Set, and Map. This lesson focuses on the List and Set collections.

Set implementations do not permit duplicate elements, but List implementations do. Duplicate elements have the same data type and value. For example, two customer IDs of type String containing the value Zelda are duplicate; whereas, an element of type String containing the value 1 and an element of type Integer containing the value 1 are not duplicate.

The API provides two general-purpose Set implementations. HashSet, which stores its elements in a hash table, and TreeSet, which stores its elements in a balanced binary tree called a red-black tree. The example for this lesson uses the HashSet implementation because it currently has the best performance.

This diagram shows the Collection interfaces on the right and the class hierarchy for the java.util.HashSet on the left. You can see that the HashSet class implements the Set interface.

Creating a Set

This example adapts the RMIClient2.java class to collect customer IDs in a Set and print the list of customer IDs whenever the View button is clicked.

The collection object is a Set so if the same customer enters multiple orders, there is only one element for that customer in the list of customer IDs. If the program tries to add an element that is the same as an element already in the set, the second element is simply not added. No error is thrown and there is nothing you have to do in your code.

The RMIClient2.actionPerformed method calls the addCustomer method to add a customer ID to the set when the order processor clicks the View button.

The addCustomer method shown below adds the customer ID to the set and prints a notice that the customer ID has been added.

//Create list of customer IDs
  public void addCustomer(String custID){
    s.add(custID);
    System.out.println("Customer ID added");
  }

Printing

The print method is called from the RMIClient2.actionPerformed method when the order processor clicks the View button. The print method prints the elements currently in the set to the command line.


Note: A HashSet does not guarantee the order of the elements in the set. Elements are printed in the order they occur in the set, but that order is not necessarily the same as the order in which the elements were placed in the set.

To traverse the set, an object of type Iterator is returned from the set. The Iterator object has a hasNext method that lets you test if there is another element in the set, a next that lets you move over the elements in the set, and a remove method that lets you remove an element.

The example print method shows two ways to print the set. The first way uses an iterator and the second way simply calls System.out.println on the set. In the iterator approach, the element returned by the next method is printed to the command line until there are no more elements in the set.

//Print customer IDs
  public void print(){
  //Iterator approach
    if(s.size()!=0){
      Iterator it = s.iterator();
      while(it.hasNext()){
          System.out.println(it.next());
      }
  //Call System.out.println on the set
      System.out.println(s);
    }else{
      System.out.println("No customer IDs available");
    }
  }

More Information

You can find more information on Collections in the Collections trail in The JavaTM Tutorial.

[TOP]



Print Button
[ This page was updated: 31-Mar-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.