Java Collection API - Java Tutorials

Java Collection API

Ads

Tutorials   
Java final method Assert in Java Final method in java Autoboxing in Java assert In Java JDBC batch insert using Java bean class Java Singleton Pattern Java Collection API - Java Tutorials Stack Overflow - Java Tutorials End of Year Puzzle The link to the outer class,java tutorial,java tutorials Orientating Components Right to Left,java newsletter,java,tutorial Non-virtual Methods in Java - java tutorials Multicasting in Java - java tutorials,tutorial Creation Time Comparison of Multi Dimensional Array- Java Tutorials Making Exceptions Unchecked - java tutorial,java tutorials Making Enumerations Iterable - JDK 5 Example Which is Faster - LinkedList or ArrayList? LinkedHashMap - Java Tutorials HashMap and HashCode JDK 1.4 the NullPointerException - Java Tutorials Java HashMap - Java Tutorials Java GC java awt package tutorial Java Multi Dimensions Array - Java Tutorials Java HashSet  - Java Tutorials Java Field Initialisation - Java Tutorials Java Comments - Java Tutorials J2EE Singleton Pattern - Design Pattern Tutorials Java - JDK Tutorials garbage collection in java Final Methods - Java Tutorials Exceptional Constructors - Java Tutorials Double-checked locking,java tutorials,java tutorial Disassembling Java Classes - Java Tutorials Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java Commenting out your code - Java Tutorials Class names don't identify a class - Java Tutorials Causing Deadlocks in Swing Code Exception in Java - Java Tutorials Catching Exceptions in GUI Code - Java Tutorials boolean comparisons - tutorial Interthread Communication in Java BASIC Java - Java Tutorials Thread Deadlocks - Java Tutorials Autoboxing unboxing in Java - Java Tutorials Assertion in Java Appending Strings - Java Tutorials Anonymous Inner Classes - Anonymous Inner Classes tutorial Assertion in java
Ads

Java Collection API

Collection was added to Java with J2SE 1.2 release. Collection framework is provided in 'java.util.package'.

All collections framework contains the following three parts :

Interfaces : Interfaces allow collections to be manipulated independently of the details of their representation.

Implementations i.e. Classes: These are the concrete implementations of the collection interfaces. Also, they are reusable data structures.

Algorithms: These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The same method can be used on many different implementations of the appropriate collection interface.

The main benefits of Collection Framework are :

1. The implementations for the fundamental collections (dynamic arrays, linked lists, trees, and hash tables) are highly efficient.

2. It allows different types of collections to work in a similar manner and with a high degree of interoperability.

3. It allows the integration of standard arrays into the Collection Framework.

Collection Interface

The Collection Interface is the base on which the Collection framework is built. The methods declare by this Interface can be used by any collection. The UnsupportedOperationException can be  thrown by many of these methods. These methods are summarized below :

        Methods              Description         
boolean add(Object obj) Adds obj to the invoking collection. Returns true if obj
was added to the collection. Returns false if obj is already
a member of the collection, or if the collection does not
allow duplicates.
boolean addAll(Collection c) Adds all the elements of c to the invoking collection. Returns
true if the operation succeeded (i.e., the elements were
added). Otherwise, returns false.
void clear( ) Removes all elements from the invoking collection.
boolean contains(Object obj)  Returns true if obj is an element of the invoking collection.
 Otherwise, returns false.
boolean containsAll(Collection c)  Returns true if the invoking collection contains all elements
 of c. Otherwise, returns false.
boolean equals(Object obj)  Returns true if the invoking collection and obj are equal.
Otherwise, returns false.
int hashCode( )  Returns the hash code for the invoking collection.
boolean isEmpty( )  Returns true if the invoking collection is empty. Otherwise,
returns false.
Iterator iterator( )  Returns an iterator for the invoking collection.
boolean remove(Object obj)  Removes one instance of obj from the invoking collection.
Returns true if the element was removed. Otherwise, returns
 false.
boolean removeAll(Collection c)  Removes all elements of c from the invoking collection.
Returns true if the collection changed (i.e., elements were
removed). Otherwise, returns false.
boolean retainAll(Collection c) Removes all elements from the invoking collection except
those in c. Returns true if the collection changed
int size( )  Returns the number of elements held in the invoking collection.
Object[ ] toArray( ) Returns an array that contains all the elements stored in the
invoking collection. The array elements are copies of the
 collection elements.
Object[ ] toArray(Object array[ ]) Returns an array containing only those collection elements
whose type matches that of array.

Example :

In the below example, the method "add" of Collection Interface is implemented by various classes :

import java.util.*;

public class CollectionInterfaceDemo {
 
 public static void main(String[] args) {
	List AL = new ArrayList();
	AL.add("ANKIT");
	AL.add("DIVYA");
	AL.add("MANISHA");
	System.out.println("Elements of ArrayList");
	System.out.print("\t" + AL);

	List LL = new LinkedList();
	LL.add("ANKIT");
	LL.add("DIVYA");
	LL.add("MANISHA");
	System.out.println();
	System.out.println("Elements of LinkedList ");
	System.out.print("\t" + LL);

	Set HS = new HashSet();
	HS.add("ANKIT");
	HS.add("DIVYA");
	HS.add("MANISHA");
	System.out.println();
	System.out.println("Elements of set");
	System.out.print("\t" + HS);

	Map HM = new HashMap();
	HM.put("ANKIT", "8");
	HM.put("MAHIMA", "24");
	HM.put("DIVYA", "31");
	HM.put("MANISHA", "12");
	HM.put("VINITA", "14");
	System.out.println();
	System.out.println("Elements of Map");
	System.out.print("\t" + HM);
 }

}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac CollectionInterfaceDemo.java

C:\Program Files\Java\jdk1.6.0_18\bin>java CollectionInterfaceDemo
Elements of ArrayList
[ANKIT, DIVYA, MANISHA]
Elements of LinkedList
[ANKIT, DIVYA, MANISHA]
Elements of set
[MANISHA, DIVYA, ANKIT]
Elements of Map
{MANISHA=12, DIVYA=31, VINITA=14, MAHIMA=24, ANKIT=8}

Advertisements

Share on Google+Share on Google+

Java Collection API - Java Tutorials

Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Visiting Collection's Elements - Java Tutorials  

Post your Comment


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

Ads

 

Ads