collection interface

The Java Collection Interface and Examples

The Collection interface of Java’s Collections Framework is the root of the collection hierarchy and it is the least common denominator that all collections implement i.e., every collection object is a type of Collection interface.

It is used to pass around the collection of objects around and manipulate them when maximum generality is desired. Apply Collection interface as a type.

JDK (Java Development Kit) does not provide any direct implementations of this interface but it provides implementations of more specific sub interfaces, such as Set and List etc.,

Collection Interface Java SE 8

public interface Collection<E> extends Iterable<E> {

// Basic operations
int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element); //optional operation
boolean remove(Object element); //optional operation
boolean equals(Object element);
int hashcode();
Iterator<E> iterator();

// Bulk operations
boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); //optional operation
boolean removeAll(Collection<?> c); //optional operation
boolean retainAll(Collection<?> c); //optional operation
void clear(); //optional operation

// Array operations
Object[] toArray();
<T> T[] toArray(T[] a);

//Other operations since 1.8
default Stream<E> stream()
default Stream<E> parallelStream(); 
default boolean removeIf(Predicate<? super E> filter); 
default Spliterator<E> spliterator();

}

Example: Using “Collection” as a Type

Below is the way to create a ArrayList collection object instance and assign it to “Collection” type.

Collection c = new ArrayList();

Using methods of CollectionInterface

boolean b1 = c.isEmpty();
boolean b2 = c.add(new Integer(10));

Note: In the above code sneppet, polymorphic behavior is expected. For example, the add() implementation of ArrayList class will be invoked. And depending on the implementation duplication will be allowed or not allowed. Say for example add() method of Set interface follows “no duplicate” rule.

If you are using JDK 7 or later and let’s say you have a Collection<String> c which is list, then you can use diamond operator like below

List<String> list = new ArrayList<>(c)

As listed above Collection interface contains methods to support basic operations, bulk operations and additional methods to support array operations. Since from JDK 8 Collection interface also exposes methods Stream<E> stream() and Stream<E> parallelStream(), for obtaining sequential or parallel streams from the underlying collection.

Traversing through Collections

There are three ways to traverse Java Collections.

  1. Aggregate Operations
  2. for-each construct
  3. Iterators.

Example : CollectionInterface and Methods

package com.sneppets.corejava.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author sneppets.com
 */
public class JavaCollectionExample {
	
	public static void main(String[] args)
	{
		//ArrayList
		Collection<String> al = new ArrayList<String>();
		al.add("Ram");
		al.add("Raj");
		al.add("Ram");
		System.out.println("ArrayList Elements");
		System.out.print(al);
		
		//LinkedList
		Collection<String> ll = new LinkedList<String>();
		ll.add("Ram");
		ll.add("Raj");
		ll.add("Ram");
		System.out.println();
		System.out.println("LinkedList Elements");
		System.out.print(ll);
		
		//HashSet
		Collection<String> hs = new HashSet<String>();
		hs.add("Ram");
		hs.add("Raj");
		hs.add("Ram");
		System.out.println();
		System.out.println("HashSet Elements");
		System.out.print(hs);
		
		//HashMap
		Map<String, String> hm = new HashMap<String, String>();
		hm.put("Ram", "1");
		hm.put("Raj", "2");
		hm.put("Ram", "3");
		System.out.println();
		System.out.println("HashMap Elements");
		System.out.print(hm);
	}

}

Output:

ArrayList Elements
[Ram, Raj, Ram]
LinkedList Elements
[Ram, Raj, Ram]
HashSet Elements
[Raj, Ram]
HashMap Elements
{Raj=2, Ram=3}

Tips: Key points on Collection and Map Interfaces

  • By convention each of the collection implementation classes provide a constructor to create a collection based on the elements in the Collection object passed as an argument.
  • In the same way, Map implementations provide a constructor that accepts a Map argument.
  • This allows the implementation of a collection (Collection or Map) to be changed.
  • But Collections and Maps are not interchangeable.

Related Posts

Reference

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments