Java Collection API - Java Tutorials

Java Collection API

Java Collection API - Java Tutorials

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}

Tutorials

  1. Assertion in java
  2. Anonymous Inner Classes - Anonymous Inner Classes tutorial
  3. Appending Strings - Java Tutorials
  4. Assertion in Java
  5. Autoboxing unboxing in Java - Java Tutorials
  6. Thread Deadlocks - Java Tutorials
  7. BASIC Java - Java Tutorials
  8. Interthread Communication in Java
  9. boolean comparisons - tutorial
  10. Catching Exceptions in GUI Code - Java Tutorials
  11. Exception in Java - Java Tutorials
  12. Causing Deadlocks in Swing Code
  13. Class names don't identify a class - Java Tutorials
  14. Commenting out your code - Java Tutorials
  15. Java Deadlocks - Java Deadlocks Tutorials, Deadlocks in Java
  16. Disassembling Java Classes - Java Tutorials
  17. Double-checked locking,java tutorials,java tutorial
  18. Exceptional Constructors - Java Tutorials
  19. Final Methods - Java Tutorials
  20. garbage collection in java
  21. Java - JDK Tutorials
  22. J2EE Singleton Pattern - Design Pattern Tutorials
  23. Java Comments - Java Tutorials
  24. Java Field Initialisation - Java Tutorials
  25. Java HashSet  - Java Tutorials
  26. Java Multi Dimensions Array - Java Tutorials
  27. java awt package tutorial
  28. Java GC
  29. Java HashMap - Java Tutorials
  30. JDK 1.4 the NullPointerException - Java Tutorials
  31. HashMap and HashCode
  32. LinkedHashMap - Java Tutorials
  33. Which is Faster - LinkedList or ArrayList?
  34. Making Enumerations Iterable - JDK 5 Example
  35. Making Exceptions Unchecked - java tutorial,java tutorials
  36. Creation Time Comparison of Multi Dimensional Array- Java Tutorials
  37. Multicasting in Java - java tutorials,tutorial
  38. Non-virtual Methods in Java - java tutorials
  39. Orientating Components Right to Left,java newsletter,java,tutorial
  40. The link to the outer class,java tutorial,java tutorials