Core Collection Interfaces


Core Collection Interfaces
The core interfaces that define common functionality and allow collections to be manipulated independent of their implementation.
The 6 core Interfaces used in the Collection framework are:
  • Collection
  • Set
  • List
  • Iterator (Not a part of the Collections Framework)
  • SortedSet
  • Map
  • SortedMap
Note: Collection and Map are the two top-level interfaces.
Collection Interface

Map Interface

Concrete Classes

The concrete classes that are specific implementations of the core interfaces, providing data structures that a java program can use.
Note: Concrete Classes for the Map is shown in the previous section.
Standard utility methods and algorithms
Standard utility methods and algorithms
that can be used to perform various operations on collections, such as sorting, searching or creating customizedcollections.

How are Collections Used

  • The collections stores object references, rather than objects themselves. Hence primitive values cannot bestored in a collection directly. They need to be encapsulated (using wrapper classes) into an Object prior to storing them into a Collection (such as HashSet, HashMap etc).
  • The references are always stored as type Object. Thus, when you retrieve an element from a collection, you get an Object rather then the actual type of the collection stored in the database. Hence we need to downcast it to the Actual Type while retrieving an element from a collection.
  • One of the capabilities of the Collection Framework is to create a new Collection object and populate it with the contents of an existing Collection object of a same or different actual type.
Below is an example program showing the storing and retrieving of a few Collection Types
import java.util.*;

public class CollectionsDemo {

 public static void main(String[] args) {
  List a1 = new ArrayList();
  a1.add("Beginner");
  a1.add("Java");
  a1.add("tutorial");
  System.out.println(" ArrayList Elements");
  System.out.print("\t" + a1);
  List l1 = new LinkedList();
  l1.add("Beginner");
  l1.add("Java");
  l1.add("tutorial");
  System.out.println();
  System.out.println(" LinkedList Elements");
  System.out.print("\t" + l1);
  Set s1 = new HashSet(); // or new TreeSet() will order the elements;
  s1.add("Beginner");
  s1.add("Java");
  s1.add("Java");
  s1.add("tutorial");
  System.out.println();
  System.out.println(" Set Elements");
  System.out.print("\t" + s1);
  Map m1 = new HashMap(); // or new TreeMap() will order based on keys
  m1.put("Windows", "98");
  m1.put("Win", "XP");
  m1.put("Beginner", "Java");
  m1.put("Tutorial", "Site");
  System.out.println();
  System.out.println(" Map Elements");
  System.out.print("\t" + m1);
 }
}
Output
ArrayList Elements
[Beginner, Java, tutorial]
LinkedList Elements
[Beginner, Java, tutorial]
Set Elements
[tutorial, Beginner, Java]
Map Elements
{Tutorial=Site, Windows=98, Win=XP, Beginner=Java}
Download CollectionsDemo.java

Java Collections Source Code Examples

On the following pages in this tutorial I have described how elements can be manipulated by different collectionsnamely;

No comments: