March 3, 2021
Hot Topics:

Data Structures in Java: Part 1, Getting Started

  • By Richard G. Baldwin
  • Send Email »
  • More Articles »


In this lesson, I have provided a brief introduction to the use of the Java Collections Framework.  The framework is designed to encourage you to reuse rather than to reinvent collections and maps (I will have more to say about maps in a subsequent lesson).

A collection represents a group of objects, known as its elements.

While some collections allow duplicate elements, others do not. Some collections are ordered and others are not ordered.

The Collections Framework is defined by a set of interfaces and associated contracts.  The framework provides concrete implementations of the interfaces (classes) for the most common data structures.  In addition, the framework also provides several abstract implementations, which are designed to make it easier for you to create new and different concrete implementations.

The TreeSet class is a concrete implementation of the SortedSet interface.  The SortedSet interface extends Set, which extends Collection.  Thus, a TreeSet object is a SortedSet.  Also it is a Set, and it is a Collection.

The TreeSet class guarantees that the sorted set will be in ascending element order, and provides guaranteed log(n) time cost for the basic operations (add, remove and contains).

TreeSet objects can be treated as the generic type Collection.  Methods declared in the Collection interface can be invoked on a TreeSet object without regard for the actual class from which the object was instantiated. (This is polymorphic behavior.)

When such methods are invoked, the author of the program can have confidence that the behavior of the method will be appropriate for an object of the class from which the object was instantiated.  In my opinion, this is the true essence of object-oriented behavior.

What's Next?

This is the first lesson in a miniseries on the Collection Framework.  Subsequent lessons will teach you how to use the framework for creating and using various types of collections and maps.

Once you learn how to use the framework, it is unlikely that you will need to reinvent classical data structures, search algorithms, or sorting algorithms, because those capabilities are neatly packaged within the framework.

Complete Program Listing

A complete listing of the program is provided in Listing 6 below.
import java.util.TreeSet;
import java.util.Collection;
import java.util.Iterator;

public class AP400{
  public static void main(
                        String args[]){
    new Worker().doIt();
  }//end main()
}//end class AP400

class Worker{
  public void doIt(){
    Collection ref = new TreeSet();
    Iterator iter = ref.iterator();
    }//end while loop
  }//end doIt()
}// end class Worker

class Populator{
  public static void fillIt(
                       Collection ref){
    ref.add(new Integer(4));
    ref.add(new Integer(4));
    ref.add(new Integer(3));
    ref.add(new Integer(2));
    ref.add(new Integer(1));
  }//end fillIt()
}//end class populator

Listing 6

Copyright 2000, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.


Page 2 of 2

This article was originally published on May 10, 2001

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date