You are on page 1of 3

Collection

While working with Collection frame work we need to remember the following four points. 1. The collection interface is a group of objects that allows the duplicate. 2. Set extends collection but forbids the duplicate. 3. List also extends the collection and allows the duplicate but introduces positional indexing. 4. Map extends neither Collection nor Set. Interface s Set List Map Implementations HashSet ArrayList HashMap TreeSet LinkedList TreeMap Historical

Vector Stack HashTable Properties

Collections Framework Enhancements in 1.5 Three new language features significantly enhance the collections framework:

Generics - adds compile-time type safety to the collections framework and eliminates the need to cast when reading elements from collections. Enhanced for loop - eliminates the need for explicit iterators when iterating over collections. Autoboxing/unboxing - automatically converts primitives (such as int) to wrapper classes (such as Integer) when inserting them into collections, and lconverts wrapper class instances to primitives when reading from collections.

HashMap
map.put("Netscape", "Mountain View, CA"); map.put("O'Reilly", "Sebastopol, CA"); map.put("Sun", "Mountain View, CA"); // List the entries using entrySet() Set entries = map.entrySet(); Iterator it = entries.iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); System.out.println(entry.getKey() + "-->" + entry.getValue())

What is synchronize in this context? What is fail-safe property?


1)Synchronized means only one thread can modify a hash table at one point of time.Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released. 2)Fail-safe is relevant from the context of iterators.If an iterator has been created on a collection object and some other thread tries to modify the collection object "structurally",a concurrent modification exception will be thrown.It is possible for other threads though to invoke "set" method since it doesnt modify the collection "structurally".However, if prior to calling "set", the collection has been modified structurally, "IllegalArgumentException" will be thrown.

HashTable and HashMap


Both provide key-value access to data. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2. The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default. Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know. And, a third difference is that HashMap permits null values in it, while Hashtable doesn't.

Vector and ArrayList


There are four factors to consider:

API Synchronization Data growth Usage patterns

Let's explore each in turn.

API
In The Java Programming Language (Addison-Wesley, June 2000) Ken Arnold, James Gosling, and David Holmes describe the Vector as an analog to the ArrayList. So, from an API perspective, the two classes are very similar. However, there are still some major differences between the two classes.

Synchronization
Vectors are synchronized. Any method that touches the Vector's contents is thread safe. ArrayList, on the other hand, is unsynchronized, making them, therefore, not thread safe.

With

that difference in mind, using synchronization will incur a performance hit. So if you don't need a thread-safe collection, use the ArrayList. Why pay the price of synchronization unnecessarily?

Data growth
Internally, both the ArrayList and Vector hold onto their contents using an Array. You need to keep this fact in mind while using either in your programs. When you insert an element into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent. Depending on how you use these classes, you could end up taking a large performance hit while adding new elements. It's always best to set the object's initial capacity to the largest capacity that your program will need. By carefully setting the capacity, you can avoid paying the penalty needed to resize the internal array later. If you don't know how much data you'll have, but you do know the rate at which it grows, Vector does possess a slight advantage since you can set the increment value.

Usage patterns
Both the ArrayList and Vector are good for retrieving elements from a specific position in the container or for adding and removing elements from the end of the container. All of these operations can be performed in constant time -- O(1). However, adding and removing elements from any other position proves more expensive -- linear to be exact: O(n-i), where n is the number of elements and i is the index of the element added or removed. These operations are more expensive because you have to shift all elements at index i and higher over by one element. So what does this all mean? It means that if you want to index elements or add and remove elements at the end of the array, use either a Vector or an ArrayList. If you want to do anything else to the contents, go find yourself another container class. For example, the LinkedList can add or remove an element at any position in constant time -- O(1). However, indexing an element is a bit slower -- O(i) where i is the index of the element. Traversing an ArrayList is also easier since you can simply use an index instead of having to create an iterator. The LinkedList also creates an internal object for each element inserted. So you have to be aware of the extra garbage being created.

You might also like