0% found this document useful (0 votes)
21 views

Abstract Data Types: Collection Map

This document discusses abstract data types (ADTs) in Java. It describes several common ADTs including maps, collections, sets, lists, and how they are implemented using interfaces like Map, Collection, Set, List. It provides examples of implementations like HashMap, ArrayList, LinkedList and explains how iterators allow uniform iteration over different collection types.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Abstract Data Types: Collection Map

This document discusses abstract data types (ADTs) in Java. It describes several common ADTs including maps, collections, sets, lists, and how they are implemented using interfaces like Map, Collection, Set, List. It provides examples of implementations like HashMap, ArrayList, LinkedList and explains how iterators allow uniform iteration over different collection types.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Abstract Data Types

An abstract data type (ADT) is a specification of the set of operations that can be performed on the data
stored by the ADT. An ADT is “abstract” in the sense that it is independent of a concrete implementation.
In Java, an abstract data type is represented by an interface, which shows the methods and their
arguments. Clients of an interface (this means other Java classes that use the interface) can be written and
compiled using the interface only—it is not necessary for them to know about the implementation classes.
Because the implementation is hidden, this means that the ADT can be implemented in various ways. As
long as it adheres to the interface, user programs can remain the same. For example, the List ADT can be
represented using the array-based implementation ArrayList or the linked-list implementation LinkedList.
The separation of interfaces and implementations protects Java programs from design decisions that may
change in the future. It makes it easier to maintain programs, and to reuse code (because the same code can
now be used for different implementations). We will see many examples of this in the future.
Java provides interfaces and implementations for many abstract data types. They are in the package
java.util, and are known as the Collections Framework. Fig. 1 shows the main Collection interfaces in
java.util.

Collection Map

Set List Queue SortedMap

SortedSet Deque

Figure 1: Java collection interfaces

Map
A map (also called a dictionary) is an object that maps keys to values. A map supports two main operations:
• insert a (key, value) pair,
• given a key, find the value
A map cannot contain duplicate keys.

public interface Map<Key, Value> {


boolean isEmpty();
boolean containsKey(Key key);
Value get(Key key);
Value put(Key key, Value value);
Value remove(Key key);
}
There is also a SortedMap interface, which can be used for elements that can be compared:
public interface SortedMap<Key, Value> extends Map<Key, Value> {

Key firstKey();
Key lastKey();
SortedMap<Key, Value> subMap(Key fromKey, Key toKey);
}

1
The Java class TreeMap implements SortedMap using a balanced binary tree, and operations typically
take O(log n) time. The class HashMap implements Map using a hash table, and operations take constant
time.
HashMap requires that the key objects implement the hashCode method correctly: if two key objects are
equal, then they must have identical hashCode. This is true for all standard types, but needs to be done by
the programmer for new types!

Collection
A collection represents a group of objects, known as its elements.
public interface Collection<AnyType> {
void clear();
boolean isEmpty();
int size();
boolean add(AnyType obj);
boolean contains(Object obj);
boolean remove(Object obj);
Iterator<AnyType> iterator();
}
Java does not provide any direct implementations of the Collection interface.

Set
A set is a collection that contains no duplicate elements. As implied by its name, this interface models the
mathematical set abstraction. A SortedSet is a set where some order of its elements exists.

public interface Set<T> extends Collection<T> {


// no new methods
}
public interface SortedSet<T> extends Set<T> {
T first();
T last();
SortedSet<T> subSet(T fromElem, T toElem);
}
Implementations of Set and SortedSet are similar to implementations of Map and SortedMap — in fact,
a Set can simply be implemented using a Map. java.util contains the TreeSet class implementing the
SortedSet interface using a balanced binary tree, and the HashSet class implenting the Set interface using
a hash table. As for HashMap, objects stored in a HashSet must correctly implement hashCode!

List
The name “list” is a bit confusing, because in computer science “list” is very often used to mean “linked-list”.
In Java, however, a List is an abstract data type with the meaning of a sequence. A List stores a sequence
of elements, and the user of the List has precise control over where in the sequence each element is inserted.
A List (sequence) can be implemented using a linked-list, but other implementations are also possible (for
instance using an array).
List elements can be accessed by their integer index. Unlike sets, lists typically allow duplicate elements.
public interface List<T> extends Collection<T> {
T get(int idx);
T set(int idx, T obj);

2
int indexOf(Object obj);
int lastIndexOf(Object obj);
T remove(int idx);
void add(int idx, T obj);
List<T> subList(int fromIdx, int toIdx);
}
The two main implementations of List in the Java collections framework are ArrayList (which imple-
ments a sequence using a dynamic array) and LinkedList (which uses a linked list, of course). ArrayList
provides fast access by index, and allows for fast appending. It is slow for inserting and deleting elements in
the middle. LinkedList is fast for adding and removing elements, but slow for accessing elements by index.

Container iterations
One common operation that we often want to perform on a collection is iteration, namely to look at all the
elements of the collection.
Iterators encapsulate the concept of iteration. This allows us to write the same iteration code for any
kind of collection, and to freely change the collection that provides the elements to be considered.
The elements of an array can be listed like this:
for (int i = 0; i < v.length; ++i)
System.out.println(v[i]);
On linked lists, it is not a good idea to access elements by their indices, because it takes O(n) time to access
an element by its index. By providing an iterator object, a collection can provide an efficient way of listing
the elements without giving access to implementation details.
Java iterators work like this:
for (it = v.iterator(); it.hasNext(); )
System.out.println(it.next());
Alternatively, we can write the following short Java code. The Java compiler converts it into the code above.
for (ElementType e : v)
System.out.println(e);
To access the contents of a Map<Key, Value> map, obtain an iterator for its set of keys using

Iterator<Key> it = map.keySet().iterator()

Adding iterators to your own containers


Every Java collection provides an iterator object that implements the Iterator interface. We can create
our own iterators for our own collection classes by implementing the Iterator interface as well.
To use the elegant loop syntax for our own collections, our collection has to implement the Iterable
interface (so that the Java compiler knows that our objects provide iterators).

You might also like