Java Collections Abstract Data Types

J.E.D.I.

3.3.8 Java Collections

Youve now been introduced to the foundations of abstract data types. In particular, youve learned about the basics of linked lists, stacks and queues. A good news is these abstract data types have already been implemented and included in Java. The Stack and LinkedList classes are available for use without a full understanding of these concepts. However, as computer scientists, it is important that we understand the abstract data types. Thus, a detailed explanation was still given in the preceding section. With the release of J2SE5.0, the Queue interface was also made available. For the details on these classes and interface, please refer to the Java API documentation. Java has provided us with other Collection classes and interfaces, which are all found in the java.util package. Examples of Collection classes include LinkedList, ArrayList, HashSet and TreeSet. These classes are actually implementations of different collection interfaces. At the root of the collection interface hierarchy is the Collection interface. A collection is just a group of objects that are known as its elements. Collections may allow duplicates and requires no specific ordering. The SDK does not provide any built-in implementations of this interface but direct subinterfaces, the Set interface and the List interface, are available. Now, what is the difference between these two interfaces. A set is an unordered collection that contains no duplicates. Meanwhile, a list is an ordered collection of elements where duplicates are permitted. HashSet, LinkedHashSet and TreeSet are some known implementing classes of the interface Set. ArrayList, LinkedList and Vector are some of the classes implementing the interface List. root interface Collection interface Set interface List implementing classes implementing classes HashSet LinkedHashSet TreeSet ArrayList LinkedList Vector Table 5: Java collections The following is a list of some of the Collection methods provided in the Collections API of Java 2 Platform SE v1.4.1. In Java 2 Platform SE v.1.5.0, these methods were modified to accomodate generic types. Since generic types have not been discussed yet, it is advisable to consider these methods first. It is advised that you refer to newer Collection methods once you understand the generic types, which is discussed in a latter chapter. Collection Methods public boolean addObject o Inserts the Object o to this collection. Returns true if o was successfully added to the collection. public void clear Removes all elements of this collection. public boolean removeObject o Removes a single instance of the Object o from this collection, if it is present. Returns true if o was found and removed from the collection. public boolean containsObject o Returns true if this collection contains the Object o. public boolean isEmpty Returns true if this collection does not contain any object or element. Introduction to Programming II Page 53 J.E.D.I. public int size Returns the number of elements in this collection. public Iterator iterator Returns an iterator that allows us to go through the contents of this collection. public boolean equalsObject o Returns true if the Object o is equal to this collection. public int hashCode Returns the hash code value i.e., the ID for this collection. Same objects or collections have the same hash code value or ID. Table 6: Methods of class Collection Please refer to the API documentation for a complete list of the methods found in the Collection, List and Set interface. We’ll now look at some collection classes. Please refer to the API for the list of methods included in these classes. In a preceding section, you’ve seen how to implement a linked list on your own. The Java SDK also has provided us with a built-in implementation of the linked list. The LinkedList class contains methods that allows linked lists to be used as stacks, queues or some other ADT. The following code shows how to use the LinkedList class. import java.util.; class LinkedListDemo { public static void mainString args[] { LinkedList list = new LinkedList; list.addnew Integer1; list.addnew Integer2; list.addnew Integer3; list.addnew Integer1; System.out.printlnlist + , size = + list.size; list.addFirstnew Integer0; list.addLastnew Integer4; System.out.printlnlist; System.out.printlnlist.getFirst + , + list.getLast; System.out.printlnlist.get2 + , + list.get3; list.removeFirst; list.removeLast; System.out.printlnlist; list.removenew Integer1; System.out.printlnlist; list.remove3; System.out.printlnlist; list.set2, one; System.out.printlnlist; } } The ArrayList is a resizable version an ordinary array. It implements the List interface. Analyze the following code. import java.util.; class ArrayListDemo { public static void mainString args[] { ArrayList al = new ArrayList2; Introduction to Programming II Page 54 J.E.D.I. System.out.printlnal + , size = + al.size; al.addR; al.addU; al.addO; System.out.printlnal + , size = + al.size; al.removeU; System.out.printlnal + , size = + al.size; ListIterator li = al.listIterator; while li.hasNext System.out.printlnli.next; Object a[] = al.toArray; for int i=0; ia.length; i++ System.out.printlna[i]; } } The HashSet is an implementation of the Set interface that uses a hash table. The use of a hash table allows easier and faster look up of elements. The table uses a formula to determine where an object is stored. Trace this program that uses the HashSet class. import java.util.; class HashSetDemo { public static void mainString args[] { HashSet hs = new HashSet5, 0.5f; System.out.printlnhs.addone; System.out.printlnhs.addtwo; System.out.printlnhs.addone; System.out.printlnhs.addthree; System.out.printlnhs.addfour; System.out.printlnhs.addfive; System.out.printlnhs; } } The TreeSet is an implementation of the Set interface that uses a tree. This class ensures that the sorted set will be arranged in ascending order. Consider how the TreeSet class was used in the following source code. import java.util.; class TreeSetDemo { public static void mainString args[] { TreeSet ts = new TreeSet; ts.addone; ts.addtwo; ts.addthree; ts.addfour; System.out.printlnts; } } Introduction to Programming II Page 55 J.E.D.I. Figure 3.8: TreeSet Example Introduction to Programming II Page 56 J.E.D.I. 4 Tour of the java.lang Package

4.1 Objectives