View RSS Feed

My Java Tips

Introduction To Collections In Java

Rate this Entry
by , 11-08-2011 at 06:56 PM (682 Views)
Framework is a set of classes that acts as a base for building some advanced functionality. It contains super classes having useful functionality and mechanisms which can either be used directly or can be extended in form of subclasses so as to extend the functionality without having to reinvent the basic mechanisms. For example, AWT is a framework for user interfaces.



The Java collections library is a framework for collection classes. It includes a number of interfaces and abstract classes for implementations of these classes and interfaces; It prescribes certain mechanisms, such as the iteration, hashing policy etc. Using collection doesn't demand a sound knowledge of the framework but only a basic idea.
Basically two fundamental interfaces are there in collections.

Collection.
Map.

Elements are inserted into collection with method “ add ”


public boolean add(Object element)

In map contains key/value pairs, and you use the “ put ” method to insert them.


public Object put(Object key, Object value)

To read elements from a collection, iterator can be used .
But from a map use the “ get” method which takes key as an argument and returns the value pair from the corresponding collection.


Object get(Object key)

A List is an ordered collection means elements are inserted in an order. It is important that an object can be placed into its position in two ways.

By an integer index.
By a list iterator.

The List interface defines methods for random access:


void add(int index, Object element)//Adds element at indexObject

get(int index) //to get the object at specified index

void remove(int index) // remove an element from specified index

Hence the List interface provides these random access methods but they might not be efficient for a particular implementation. So to make it possible to avoid the costly random access operations, JDK 1.4 introduced a tagging interface, RandomAccess. With no methods, but you can use it to test whether a particular collection supports efficient random access:

Java Code:
	if (c instanceof RandomAccess){	// use random access methods}

	else	

	{

	// use sequential access algorithm

	}
It is important ArrayList and Vector classes implement the RandomAccess interface so random access methods should be used while using these classes.
From a theoretical point of view, it would be much better to have a separate Array interface that extends the List interface and declares the random access methods in it so that it can be avoided in List interface. But if there were a separate Array interface, then algorithms that require random access would use reference of Array interface and you could not apply them to collections with slow random access.
However designers of the collections framework chose not to define a separate interface, so as to keep the number of interfaces in the library small. Also this design avoids paternalistic attitude toward programmers. You are free to pass a linked list to algorithms that use random access but you just need to be aware of the performance costs.

The ListIterator interface has a method for adding an element before the iterator position:


void add(E element) // use the “ next” and “ remove ” methods of the Iterator interface.

Here is an example
Java Code:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author Rohit
 *
 */
public class SimpleListTest {

   private List arrayList ;

	public SimpleListTest() {
		arrayList = new ArrayList();
	}
	public List getList(){
		return arrayList;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SimpleListTest listTest= new SimpleListTest();
		List testList =listTest.getList();
		testList.add("CompChamps");
		testList.add("Software");

		Iterator listIterator= testList.iterator();
		while(listIterator.hasNext()){
			System.out.println((String)listIterator.next());
		}

	}

}
At the other end “ Set ” interface, Which is identical to the Collection interface stores elements in unordered way. The behavior of the methods is tightly defined. The “ add ” method of a Set rejects duplicates.
The “ equals ” method of a set should be defined so that two sets are identical if they have the same elements, but not necessarily in the same order. The hashCode method should be defined such that two sets with the same elements have the same hash code.
Here the question comes that “ Why a separate interface is there if the method signatures are same ?” . Conceptually, All sets are collections. So making a Set interface enables programmers to write methods that accept only sets.

Submit "Introduction To Collections In Java" to Facebook Submit "Introduction To Collections In Java" to Digg Submit "Introduction To Collections In Java" to del.icio.us Submit "Introduction To Collections In Java" to StumbleUpon Submit "Introduction To Collections In Java" to Google

Tags: None Add / Edit Tags
Categories
Collections Framework , Java SE

Comments