Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

Its good

Posted by Roopesh Soni on Tuesday, 03.13.07 @ 15:38pm | #11576

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.