Java Collection Framework
We have tried you to make a walk through the Collection Framework. The Collection Framework provides a well-designed set if interface and classes for sorting and manipulating groups of data as a single unit, a collection.
The Collection Framework provides a standard programming interface to many of the most common abstractions, without burdening the programmer with too many procedures and interfaces.
The Collection Framework is made up of a set of interfaces for working with the groups of objects. The different interfaces describe the different types of groups. For the most part, once you understand the interfaces, you understand the framework. While you always need to create specific, implementations of the interfaces, access to the actual collection should be restricted to the use of the interface methods, thus allowing you to change the underlying data structure, without altering the rest of your code.
|
In the Collections Framework, the interfaces Map and Collection are distinct with no lineage in the hierarchy. The typical application of map is to provide access to values stored by keys.
When designing software with the Collection Framework, it is useful to remember the following hierarchical relationship of the four basic interfaces of the framework.
|
|
|
|
Interface
|
Imlementation
|
Historical
|
|||
Set
|
HashSet
|
TreeSet
|
|||
List
|
|
ArrayList
|
|
LinkedList
|
Vector
Stack |
Map
|
HashMap
|
Treemap
|
|
Hashtable
Properties |
The historical collection classes are called such because they have been around since 1.0 release of the java class libraries. If you are moving from historical collection to the new framework classes, one of the primary differences is that all operations are synchronized with the new classes. While you can add synchronization to the new classes, you cannot remove from the old.
Explore the Interface and Classes of Java Collection Framework
Java Collection Framework - Collection Interface
The Collection interface is used to represent any group of ojects, or elements. Here is a list of the public methods of the Collection Interface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The interface supports basic operations like adding and removing. When you try to remove an element, only a single instance of the element in the collection is removed, if present.boolean
add
(Object o)
boolean
remove(
Object o)
The Collection interface also supports query operations
int size(
)
boolean isEmpty
(
)
boolean contains(
Object o)
Iterator iterator
(
)
Java Collection Framework - Iterator Interface
The iterator() method of the Collection interface returns and Iterator. An Iterator is similar to the Enumeration interface, Iterators differ from enumerations in two ways:
1.Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
2. Method names have been improved.
|
|
|
|
|
|
The remove method is optionally suported by the underlying collection. When called and supported, the element returned by the last next() call is removed.
Java Collection Framework - Set Interface
The set interface extends the Collection interface and, by definition, forbids duplicates within the collection. All the original methods are present and no new method is introduced. The concrete Set implementation classes rely on the equals() method of the object added to check for equality.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Java Collection Framework - List Interface
This is an ordered collection (also known as a sequence). The List interface extends the Collection interface to define an ordered collection, permitting duplicates. The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.
The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. Declarations for other inherited methods are also included here for convenience.
The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.
The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.
The List interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.
The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.
Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the equals and hashCode methods are no longer well defined on a such a list.
Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface.
Java Collection Framework - ListIterator Interface
The ListIterator interface extends the Iterator interface to support bi-directional access as well as adding or removing or changing elements in the underlying collection.
An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next(). In a list of length n, there are n+1 valid index values, from 0 to n, inclusive.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The add(0 operation requires a little bit of explanation, also Adding an element results in the new element being added immediately prior to the implicit cursor. This calling preious after adding an element would return the new element and calling next would have no effect, returning what would hav been the next element prior to the add operation
Java Collection Framework - Map Interface
The Map interface is not an extension of Collection interface. Instead the interface starts of it’s own interface hierarchy, for maintaining key-value associations. The interface describes a mapping from keys to values, without duplicate keys, by defination.
The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The interface methods can be broken down into three sets of operations: altering, querying and providing alternative views
The alteration operation allows you to add and remove key-value pairs from the map. Both the key and value can be null. However you should not add a Map to itself as a key or value.Object put
(
Object key, Object value)
Object remove(
Object key)
void
putAll
(
Map t)
void
clear
()
The query operations allow you to check on the contents of the map
Object get(
Object key)
boolean containsKey
(
Object key)
boolean containsValue
(
Object value)
int size
()
boolean isEmpty
()
The set methods allow you to work with the group ofkeys or values as a collection
Set keySet
()
Collection values
()
Set entrySet
()
Java Collection Framework - SortedSet Interface
The Collection Framework provides a special Set interface for maintaining elements in a sorted order called SortedSet.
|
|
|
|
|
|
|
|
|
|
|
|
The interface provides access methods to the ends of the set as well to subsets of the set. When working with subsets of the list, changes to the subset are reflected in the source set. In addition changes to the source set are reflected in the subset. This works because subsets are identical by elements at the end point, not indices. In addition , if the formElement is part of the source set , it is part of the subset. However, if the toElement is part of the source ser, it is not part of the subset. If you would like a particular to-element to be in the subset, you must find the next element. In the case of a string, the next element is the same strong with a null character appended (string+”\0”).;
The element added to a SortedSet must either implement Comparable or you must provide a Comparator to the constructor to its implementation class: TreeSet.
This example uses the reverse order Comprator available from the Collection calss.
Comparator comparator= Collections.reverseOrder();
Set reverseSer= new TreeSet(comparator);
revereseSet.add("one");
revereseSet.add("two");
revereseSet.add("three");
revereseSet.add("four");
revereseSet.add("one");
System.out.println(reverseSet);
Output of this program
[two, three, one, four]
Java Collection Framework - SortedMap Interface
The Collection Framework provides a special Map interface for maintaining elements in a sorted order called SortedMap.
The interface provides access methods to the ends of the map as well to subsets of the set. Working with a SortedMap is just like a SortedSet, except the sort is done on the map keys. The implementation class provided by the Collection Framework is a TreeMap.
|
|
|
|
|
|
|
|
|
|
|
|
Java Collection Framework- HashSet & TreeSet Classes
The Collection Framework provides two general purpose implementations often Set interface, HashSet and TreeSet. More often than not you will use a HashSet for storing your duplicate-free collection. For efficiency objects added to a HashSet need to implement the hashCode() method in a manner that properly distributes the hash codes. While most system classes override the default hashCode() implementation of the Object, when creating your own class to add to a HashSet remember to override hashCode().
The TreeSet implementations useful when you need to extract elements from a collection in a sorted manner. It is generally faster to add elements to the HasSet then convert the collection to a TreeeSet for sorted traversal.
To optimize HashSet space usage , you can tune initial capacity and load factor. TreeSet has no tuning options, as the tree is always balanced, ensuring log(n0 performance for insertions, deletions and queries.
Example
import java.util.*; public class HashTreeSetEx{ |
||
public static void main (String args[]){ | ||
Set set = new HashSet(){ |
||
} | ||
} |
The program produces the following output. The duplicate entry is olypresent once and the second line outputs sorted
[one, two, three, four]
[four, one, three, two]
Note that these implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:
Set s = Collections.synchronizedSet(new HashSet(...));
SortedSet s = Collections.synchronizedSortedSet(new TreeSet(...));
Java Collection Framework- ArrayList & LinkedList Classes
There are two general-purpose List implementations in the Collection Framework, ArrayList and LinkedList, which of the two List implementations you use depends on your specific needs. If you need to support random access, without inserting or removing elements from any place to other than the end, then ArrayList offers you the optimal collection, the LinkedList class provides uniformly named methods to get, remove and insert an element at the beginning and end of the list.
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.
Note that these implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:
List list = Collections.synchronizedList(new LinkedList(...));
List list = Collections.synchronizedList(new ArrayList(...));
Java Collection Framework- HashMap & TreeMap Classes
The Collection Framework provides two general-purpose Map implementation: HashMap and TreeMap. As with all the concrete implementations, which implement you use depends on your specific needs. For inserting, deleting and locating elements in a Map the HashMap offers best alternatively. If however you need to traverse the keys in a sorted order then TreeMap is better alternative. Depending upon your size of your collection, it may be faster to add elements to a HashMap then convert the Map to a TreeMap for sorted key traversal. Using a HashMap requires that the class of key added have a well-defined hashCode() implementation. With the TreeMap implementation elements added to the Map must be sortable.
To optimize HashMap usage you can tune the initial capacity and load factor. The TreeMap has no tuning options as the tree is always balanced
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method.
As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.
Note that these implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the set. If no such object exists, the set should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the set:
Map m = Collections.synchronizedMap(new HashMap(...));
Map m = Collections.synchronizedMap(new TreeMap(...));
Java Collection Framework- Vector & Stack Classes
A Vector is an historical collection class that acts like a growable array, but can store heterogeneous data elements.
Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.