Every application larger than the typical Hello world program needs some sort of data structure to hold the application data. Like other languages Java has its own framework to handle exactly this. THE data structure framework in Java is known as the Java Collection Framework. Java Codes for Beginners

Collection Framework

A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:

  • Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
  • Implementations: These are the concrete implementations of the collection interfaces. In essence, 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 algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality. 

Apart from the Java Collections Framework, the best-known examples of collections frameworks are the C++ Standard Template Library (STL) and Smalltalk's collection hierarchy. Historically, collections frameworks have been quite complex, which gave them a reputation for having a steep learning curve.

This information was referenced from Sun Microsystems Java Documenation

Core Collection Interfaces

Java collection framework is made up of core interfaces which allow the user to implement different types of collections based on these interfaces. The core interfaces form a hierarchy which makes it easy to implement the type of collection you need.

The list below describes the core collection interfaces:-

Collection - the root of the collection interfaces. This interface describes a group of objects known as elements. The collection interface is the most general interface and does not have any direct implementations in the java language. If you data structure requires very general data storage this interface will allow you to implement any type of collection of data. The java language uses this interface to provide higher level collections such as Set and List which provide more specific functionality and algorithms.

Set - a set interface is a collection of data that does not allow duplicate entries. A set could be compared to a set of baseball cards. When you have the complete set you have one of each card in that series.

List - a list interface is a collection that allows storage of elements usually in a sequential order. List allow duplicate entries and allow you to retrieve data in the collection by sequence numbers.

Map - a map interface allows storage of elements by mapping a value to a key. Maps do not allow duplicate keys because they use one to one mappings.

SortedSet - a set interface that stores its elements in ascending order. SortedSets are ordered by using works or natual ordering processes.

SortedMap - a map interface that sorts its keys in ascending order. SortedMaps can be used for things like addressbooks or music listings.

Set Example

package wikia;

import java.util.HashSet; import java.util.Iterator; import java.util.Set;

public class SetExample {

   /** Creates a new instance of SetExample */
   public SetExample()
       // Create a new Set object
       Set landRoverSet = new HashSet();
       // Populate the set
       landRoverSet.add("Range Rover Sport");
       landRoverSet.add("Range Rover");
       // Get the number of elements in the set
       System.out.println("The land rover set size is now " + landRoverSet.size());
       // Remove an element from the set
       // Check the size again
       System.out.println("The land rover set size is now " + landRoverSet.size());
       // Attempt to add an element to the set that already exist
       // The addition will have not effect
       System.out.println("The land rover set size is now " + landRoverSet.size());
       // Find out if the element is in the set
           System.out.println("The LR3 is inside the set");
           System.out.println("The LR3 is not inside the set");
       // Iterate over the set and print out all values
       Iterator setIter = landRoverSet.iterator();
           Object setValue =;
           System.out.println("Land Rover: " + setValue);
       // Convert the set into an array
       String[] landRoverArray = (String[])(new String[landRoverSet.size()]);


List Example

package wikia;

import java.util.ArrayList; import java.util.Iterator; import java.util.List;

public class ListExample {

   /** Creates a new instance of ListExample */
   public ListExample()
       // Create a List object
       List landRoverList = new ArrayList();
       // Populate the list
       landRoverList.add("Range Rover Sport");
       // Get the size of the list
       System.out.println("The size of the list is " + landRoverList.size());
       // Remove an element from the list
       // Get the size again
       System.out.println("The size of the list is " + landRoverList.size());
       // Add a duplicate element
       // Check the size to see if it allows duplicates
       System.out.println("The size of the list is " + landRoverList.size());
       // Iterator over the collection/list
       Iterator listIter = landRoverList.iterator();
           Object listValue =;
           System.out.println("The value of the list is: " + listValue);
           // Check to see if the list value is Discovery if it is remove
               System.out.println("Removing " + listValue);




When you need to store a set amount of data and retrieve it in a sequential order (usually).


Use an array


An array is an object in java that stores a preset amount of data in a sequential order. For example lets say we have an application that needs to allow the user to choose between 5 different colors. We know that there will always only be 5 allowed colors and we know exactly what colors they are and what order they should be in. Lets take this knowlege and create a simple application that utilizes an array as a data structure to store the needed color options.


package wikia;

import java.util.Arrays;

public class ColorPickerArrayExample {

   private final String[] colors = 
       { "Black", "Blue", "Red", "Green", "Brown" };
   /** Creates a new instance of ColorPickerArrayExample */
   public ColorPickerArrayExample()
   private void printColorsInOrder()
       // Print out all colors in the order they are stored
       System.out.println("Your color choices are: ");
       for(int i=0; i < colors.length; i++)
           System.out.println("Color: " + colors[i]);
   private void printSelectColors()
       // Print out colors by selectivly picking an index
       System.out.println("Your reverse color choices are: ");
       // Lets get the value of 3
       System.out.println("Color: 3 " + colors[2]);
       // Lets get the value of 5
       System.out.println("Color 5: " + colors[4]);
       // Lets get the value of 1
       System.out.println("Color 1: " + colors[0]);


Community content is available under CC-BY-SA unless otherwise noted.