Home Java Jdk6 Collections Framework Enhancements

Related Tutorials


 
 

Share on Google+Share on Google+

Collections Framework Enhancements

Advertisement
In Collection framework, we are able to improve the performance hashing function that is used by java.util.HashMap.

Collections Framework Enhancements

     

In Collection framework, we are able to improve the performance hashing function that is used by java.util.HashMap. It provides some new Collection interfaces also.

Following new Interfaces and Classes are provided in JAVA SE 6 :

  • Deque ? Deque is a interface. It is a short for ?Double Ended Queue?. This interface defines some methods that access the element at both ends. That means by the methods of this interface we can add and remove the elements at both ends.
  • ArrayDeque ? ArrayDeque Class implements a Deque interface. This class have no capacity restriction, it can grow according to usage. If the external Synchronization is not available then it don?t support concurrent access by multiple thread.

Constructors Details :

  • public ArrayDeque()
    Above Constructor is used to make a empty array deque with an default capacity that 16 elements.
  • public ArrayDeque(int numElements)
    Above Construtor is used to make a empty array deque with the initial capacity that is sufficient to hold the specified elements.
  • public ArrayDeque<Etype>()
    Etype is the type of the elements that held in this Collection. Above Constructor is used to make a array deque containing elements of specified type.

Methods Details :

  • void addFirst(Etype e)
    Above method is used to insert the element at the starting point of the array deque
  • void addLast(Etype e)
    Above method is used to insert the element at the end point of the array deque.

  Above two methods throws following Exception:

  1. IllegalStateException ? Due to capacity restriction the element cannot be added.
  2. ClassCastException ? Class of the specified element prevents it from being added to this deque
  3. NullPointerException ? If specified element is null.
  4. IllegalArgumentException ? If element having some property that prevent it from being added to this deque
  • boolean offerFirst(Etype e)
    Above method is also used to insert the specified element at the starting point of the array deque. This method is preferable when we using a capacity restricted deque. When element is added in array deque then its return true else it return false.
  • boolean offerLast(Etype e)
    Above method is also used to insert the specified element at the end point of the array deque. This method is preferable when we using a capacity restricted deque. When element is added in array deque then its return true else it return false.

  Above two methods throws following Exception: 

  1. ClassCastException ? Class of the specified element prevents it from being added to this deque.
  2. NullPointerException ? If specified element is null.
  3. IllegalArgumentException ? If element having some property that prevent it from being added to this deque.
  • Etype removeFirst()
    Above method is used to remove the first element of the array deque. And we can also retrieve this element. But if array deque is empty then it throws a NoSuchElementException.
  • Etype removeLast()
    Above method is used to remove the last element of the array deque. And we can also retrieve this element. But if array deque is empty then it throws a NoSuchElementException.
  • Etype pollFirst()
    Above method is same as removeFirst(). It is also used to retrieve and remove the first element of the deque. But it does not throws any Exception even the deque is empty, its only return null.

  • Etype pollLast()
    Above method is same as removeLast(). It is also used to retrieve and remove the last element of the deque. But it does not throws any Exception even the deque is empty, its only return null.
  • Etype getFirst()
    Above method is used just for retrieving the first element of deque. But if array deque is empty then it throws a NoSuchElementException.
  • Etype getLast()
    Above method is used just for retrieving the last element of deque. But if array deque is empty then it throws a NoSuchElementException.
  • Etype peekFirst()
    Above method is same as getFirst().It is also used to retrieving the first element of the deque. But it does not throws any Exception even the deque is empty, its only return null.
  • Etype peekLast()
    Above method is same as getLast().It is also used to retrieving the last element of the deque. But it does not throws any Exception even the deque is empty, its only return null.
  • boolean removeFirstOccurrence(Object obj)
    Above method is used to remove the first occurrence of the specified element. It return true when the specified element was remove. But if the deque does not contain the specified element it is unchanged.
  • boolean removeLastOccurrence(Object obj)
    Above method is used to remove the last occurrence of the specified element. It return true when the specified element was remove. But if the deque does not contain the specified element it is unchanged.
The following example demonstrates the above methods:
import java.io.*;
import java.util.*;
public class NewDeque
{
	public static void main(String s[])throws IOException
	{
		Console c=System.console();
		if(c==null)		
		{
		System.err.println("Console object is not available");
			System.exit(1);
		}
		ArrayDeque<String> dqname = new ArrayDeque<String>();
			String name = null;
			name = c.readLine("Enter any String: ");
			dqname.add(name);    
			show(dqname);
	
		name=c.readLine("Enter any string to add on starting 
point of deque by addFirst():");
			dqname.addFirst(name);
			show(dqname);
			name=c.readLine("Enter any string to add on ending 
point of deque by addLast():");
			dqname.addLast(name);
			show(dqname);
		name=c.readLine("Enter any string to add on starting
 point of deque by offerfirst() :");
			dqname.offerFirst(name);
			show(dqname);
			name=c.readLine("Enter any string to add on ending 
point of deque by offerlast() :");
			dqname.offerLast(name);
			show(dqname);
			System.out.println("Getting the first element 
by using getFirst()");
			String str1=dqname.getFirst();
			System.out.println("First element is  : "+str1);
		System.out.println("Getting the Last element by using 
getLast()");
			str1=dqname.getLast();
			System.out.println("Last element is  : "+str1);
	
			System.out.println("Getting the first element by 
using peekFirst()");
			str1=dqname.peekFirst();
			System.out.println("First element is  : "+str1);
	
			System.out.println("Getting the Last element by
 using peekLast()");
			str1=dqname.peekLast();
			System.out.println("Last element is  : "+str1);
			System.out.println("Removing the first element
 by using removeFirst()");
			str1=dqname.removeFirst();
			show(dqname);			
			System.out.println("Removing the Last element
 by using removeLast()");
			str1=dqname.removeLast();
			show(dqname);
			System.out.println("Removing the first element
 by using pollFirst()");
			str1=dqname.pollFirst();
			show(dqname);
			System.out.println("Removing the Last element
 by using pollFirst()");
			str1=dqname.pollLast();
			show(dqname);
	}
			static void show(ArrayDeque<String> dqname)
			{
			Iterator<String> nameIter = dqname.iterator();
			 while(nameIter.hasNext())
			System.out.println(nameIter.next()); 
			}
}

Output of the program is:  

C:\j2se6>javac NewDeque.java

C:\j2se6>java NewDeque
Enter any String: Rose
Rose
Enter any string to add on starting point of deque by addFirst():India
India
Rose
Enter any string to add on ending point of deque by addLast():Net
India
Rose
Net
Enter any string to add on starting point of deque by offerfirst() :Com
Com
India
Rose
Net
Enter any string to add on ending point of deque by offerlast() :Chandan
Com
India
Rose
Net
Chandan
Getting the first element by using getFirst()
First element is : Com
Getting the Last element by using getLast()
Last element is : Chandan
Getting the first element by using peekFirst()
First element is : Com
Getting the Last element by using peekLast()
Last element is : Chandan
Removing the first element by using removeFirst()
India
Rose
Net
Chandan
Removing the Last element by using removeLast()
India
Rose
Net
Removing the first element by using pollFirst()
Rose
Net
Removing the Last element by using pollFirst()
Rose

C:\j2se6>

Download this Example

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 22, 2007

Related Tutorials

Discuss: Collections Framework Enhancements   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com