3-14 The Java Collections Framework

We’ve dedicated a lot of time learning how to build abstract data types from scratch based on static arrays and dynamic linked lists.  Designing effective data structures is a lot of work, but it’s also very satisfying to produce efficient and elegant solutions.  Because certain ADTs are so commonly used in computer science, most modern languages support them as part of their core API and there is usually no need to reinvent the wheel.

As mentioned in 3-4 when we talked about the ArrayList, the Java API includes two hierarchies of interfaces and classes called the Java Collections Framework (JCF).  All of the JCF interfaces and classes are located in the java.util package.  Below is an illustration of some of the most commonly used interfaces and classes in the framework:

In UML, <<interface>> is used to indicate an interface class.  Set and List extend the Collection interface:

  • Collection — Defines basic methods that all collections must support.
  • Set — Defines a collection that does not allow duplicate elements.
  • List — Defines a collection that stores elements in a sequence.  It supports index access and allows duplicate elements.
  • Map — Similar to a collection, but a map stores key-value pairs (not just values) where each value has a unique key to identify it.  A Map is different enough from a Collection that it does not extend the Collection

The HashSet, ArrayList, LinkedList, and HashMap classes implement the interfaces above.  In UML, a class that implements an interface is indicated with a dotted arrow:

  • HashSet — Stores only unique elements (no duplicate values allowed).
  • ArrayList — Array-based, so it’s more efficient than a LinkedList for accessing elements randomly (i.e., using index), but less efficient when inserting or deleting elements in the middle of the list (see 3-4).
  • LinkedList – Dynamic, like ours, but with many additional methods including those to support stack and queue operations.  Less efficient than an ArrayList for accessing elements randomly (i.e., using index), but more efficient if inserting or deleting elements in the middle of the list.
  • HashMap – Stores key-value pairs (no duplicate keys allowed).

You might ask: If the Java API already provides these common ADTs why did we spend all this time learning how to build them from scratch?  Good question!  To become an exceptional developer you need deep understanding of the issues involved in designing efficient data structures, and you cannot learn this by simply using existing classes.  Your knowledge of how data structures work from the inside out will give you greater understanding and appreciation for the JCF classes.  Moreover, what if the Java API does not include a data structure that meets your needs?  Again, a deep understanding of how to create your own data structures is a very valuable skill.

You already have experience with the ArrayList class, and the other JCF classes are just as straightforward, so I am not going to provide extensive examples here.  The best way to learn about them is by working with them.

Congratulations!

If you’ve been following along and working through all of the exercises you should have a solid foundation in Java and common algorithms and data structures at this point – more than required for Java AP.  This lesson ends our coverage of the core features of Java.  In the next set of lessons we’ll look at how to build Java applications with graphical user interfaces (GUIs).  Stay tuned!

You Try!

  1. In 3-2 Q2 we wrote a program that only accepted 10 unique integers.  Write a similar program but this time use a HashSet.  Once your code has accepted 10 integers, simply display them.  Hint: Try using an enhanced for loop to display your HashSet contents.
  2. Modify your DynamicQueue to use the JCF LinkedList instead of the one we designed and implemented. Test it with your DynamicQueueTest class.
  3. You’ve been given this Python code to maintain a dictionary of Geek Terms:

    Rewrite the code in Java using a HashMap.