A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit.

The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes.

Advantages of Collection Framework:

Before Collection Framework (or before JDK 1.2) was introduced, the standard methods for grouping Java objects (or collections) were Arrays or Vectors or Hashtables. All of these collections had no common interface.

Accessing elements of these Data Structures was a hassle as each had a different method (and syntax) for accessing its members.

  1. Consistent API : The API has a basic set of interfaces like Collection, Set, List, or Map. All classes (ArrayList, LinkedList, Vector, etc) that implement these interfaces have some common set of methods.
  2. Reduces programming effort: A programmer doesn’t have to worry about the design of the Collection, and he can focus on its best use in his program.
  3. Increases program speed and quality: Increases performance by providing high-performance implementations of useful data structures and algorithms.

Collection Interfaces

1.  Map Interface

The java.util.Map interface represents a mapping between a key and a value. Few characteristics of the Map Interface are:

  1. A Map cannot contain duplicate keys and each key can map to at most one value. Some implementations allow null key and null value like the HashMap and LinkedHashMap, but some do not like the TreeMap.
  2. The order of a map depends on specific implementations, g TreeMap and LinkedHashMap have predictable order, while HashMap does not.
  3. There are two interfaces for implementing Map in java: Map and SortedMap, and three classes: HashMap, TreeMap and LinkedHashMap.

Methods in Map Interface:

  1. put(key, value): This method is used to insert an entry in this map.
  2. putAll(Map map): This method is used to insert the specified map in this map.
  3. remove(key): This method is used to delete an entry for the specified key.
  4. get(key): This method is used to return the value for the specified key.
  5. containsKey(key): This method is used to search the specified key from this map.
  6. clear(): This method clear all the elements.
  7. size(): This method returns the number of components.
Example Program:

Output:

Value at key = 6 : null

Value at key = 456 : Code

There is no key-value pair : false

Remove : Code

Check the value of removed key : null

Size of Map : 1

2.  List Interface

The Java.util.List is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. List Interface is implemented by ArrayList, LinkedList, Vector and Stack classes.

List is an interface, and the instances of List can be created in the following ways:

Methods in List Interface:

  1. add(element): This method is used to add elements in list.
  2. get(index): This method is used to retrieve elements on the basis of index number.
  3. isEmpty(): This method tests if this list has no components.
  4. remove(index): This method removes a single element on the basis of index number.
  5. clear(): This method clear all the elements from list.
  6. size(): This method returns the number of components in this list.
  7. removeAllElements(): This method removes all components from this list and sets its size to zero.
  8. indexOf(element): This method returns first occurrence of given element or -1 if element is not present in list.
  9. lastIndexOf(element): This method returns the last occurrence of given element or - 1 if element is not present in list.

Example Program

             

         

Output:

[10, 20, 30]

Item at index 1 is: 20

After Removing Item [10, 20]

Size of list is: 2

List after removing all items:

3.  Set Interface

  • Set is an interface which extends It is an unordered collection of objects in which duplicate values cannot be stored.
  • Basically, Set is implemented by HashSet, LinkedHashSet or TreeSet (sorted representation).
  • Set has various methods to add, remove clear, size, etc to enhance the usage of this interface

import java.util.*; public class Set_example

{

public static void main(String[] args)

{

// Set deonstration using HashSet

Set<String> hash_Set = new HashSet<String>();

hash_Set.add("Geeks");

hash_Set.add("For");

hash_Set.add("Geeks");

hash_Set.add("Example");

hash_Set.add("Set");

System.out.print("Set output without the duplicates");

System.out.println(hash_Set);

// Set deonstration using TreeSet System.out.print("Sorted Set after passing into TreeSet");

Set<String> tree_Set = new TreeSet<String>(hash_Set);

System.out.println(tree_Set);

}

}

Output

Set output without the duplicates[Geeks, Example, For, Set]

Sorted Set after passing into TreeSet[Example, For, Geeks, Set]

Note: As we can see the duplicate entry “Geeks” is ignored in the final output, Set interface doesn’t allow duplicate entries.

Methods in Set Interface:

  1. add(element): This method is used to add elements in set.
  2. get(index): This method is used to retrieve elements on the basis of index number.
  3. isEmpty(): This method tests if this set has no components.
  4. remove(index): This method removes a single element on the basis of index number.
  5. clear(): This method clear all the elements from set.
  6. size(): This method returns the number of components in this set.
  7. removeAllElements(): This method removes all components from this set and sets its size to zero.
  8. indexOf(element): This method returns first occurrence of given element or -1 if element is not present in set.
  9. lastIndexOf(element): This method returns the last occurrence of given element or - 1 if element is not present in set.