Java Collections – Iterator vs ListIterator

  • We can use Iterator to traverse Set and List collections whereas ListIterator can be used with Lists only.
  • Iterator can traverse in forward direction only whereas ListIterator can be used to traverse in both the directions.
  • ListIterator inherits from Iterator interface and comes with extra functionalities like adding an element, replacing an element, getting index position for previous and next elements.

Ugliness of Java Generics – Area based behaviour

Generic to Non Generic Behaviour 

Need to be careful with generics as it has method area based behavior as in the below example – 

Class Test {

public static void main(String[] args){

ArrayList<String> genericAreaList = new ArrayList<String>();

//In the below ArrayList can only accept String values

nonGenericAreaMethod(genericAreaList); // passing the ArrayList to this non-generic Arraylist signature method.

//Here ArrayList can add any value as the below signature is non generics ArrayList
private static void  nonGenericAreaMethod(ArrayList nonGenericAreaList) {




Also remember – ArrayList testList = new ArrayList<String>(); works as non-generics and accept any value String, Integer.

Also see – Generics Basics

Java Generics – Difference between <? extends T> and <? super T>

Both <? extends T> and <? super T> represent bounded wildcards
<? extends T> – will accept only T or its subclasses
<? super T> – will accept T or super class. bounded wildcards gives more flexibility to methods which can operate on collection of T or its sub class. These bound types are used with Collections as shown below.

Example – Collections.unmodifiableSet(Set<? extends T> s) will accept Set of type T or Set of sub class of T.

Also see – Generics Basics