Skip to the content.

Collections Framework

-

What we’ll cover

What is a collection?

The Collection Interface

The Iterator Interface

The Iterable Interface

-

Iterator and Collection interface

-

What is a Collection?

-

Collections and Maps

List Set Map
ArrayList HashSet TreeMap
LinkedList TreeSet HashMap
ArrayDeque PriorityQueue WeakHashMap
  EnumSet IdentityHashMap
  LinkedHashSet LinkedHashMap
     

-

Collection Interface

public interface Collection<E> extends Iterable<E> {
    boolean add(E element);
    boolean addAll(Collection<? extends E> collection);
    void clear();
    boolean contains(Object object);
    boolean containsAll(Collection<?> collection);
    boolean isEmpty();
    Iterator<E> iterator();
    boolean remove(Object object);
    boolean removeAll(Collection<?> collection);
    boolean retainAll(Collection<?> collection);
    int size();
    Object[] toArray();
    <T> T[] toArray(T[] array);
}

-

Collection Interface

boolean add(E element)

public void demo() {
	Collection<String> set = new HashSet<>();
	String valueToBeAdded = "Hedjet";
	set.add(valueToBeAdded);
	System.out.println(set.add(valueToBeAdded)); // prints false
}

-

Collection Interface

boolean addAll(Collection)

public void demo() {
  Collection<String> set = new HashSet<>();
  String[] valuesToBeAdded = {"Rick", "Beth", "Summer", "Morty", "Jerry"};
  Collection<String> valuesAsList = Arrays.asList(valueToBeAdded);
  System.out.println(set.addAll(list)); // prints true
}

-

Collection Interface

boolean remove(Object)

public void demo() {
  // prints false
  System.out.println(new ArrayList().remove(new Object()));
}

-

Collection Interface

boolean removeAll(Collection)

public void demo() {
  String[] elementsAsArray = {"The", "Quick", "Brown"};
  Collection<String> originalCollection = new ArrayList<>();
  Collection<String> elementsAsList = Arrays.asList(elementsAsArray);

  // prints false
  System.out.println(originalCollection.removeAll(elementsAsList));
}

-

Collection Interface

boolean retainAll(Collection)

public void demo() {
  String[] originalArray = {"The", "Quick", "Brown"};
  String[] elementsToBeRetained = {"The", "Quick"};
  List<String> originalList = new ArrayList<>(Arrays.asList(originalArray));
  List<String> retentionList = Arrays.asList(elementsToBeRetained);

  // prints true
  System.out.println(originalList.retainAll(retentionList)));
}

-

Collection Interface

boolean isEmpty()

public void demo() {
  String[] elementsAsArray = {"The", "Quick", "Brown"};
  Collection<String> elementsAsList = Arrays.asList(arrayOfStrings);
  System.out.println(elementsAsList.isEmpty()); // prints false
}

-

Collection Interface

int size()

public void demo() {
  String[] elementsAsArray = {"The", "Quick", "Brown"};
  Collection<String> elementsAsList = Arrays.asList(arrayOfStrings);
  System.out.println(elementsAsList.size()); // prints 3
}

-

Collection Interface

void clear()

public void demo() {
  String[] elementsAsArray = {"The", "Quick", "Brown"};
  List<String> elementsAsList = new ArrayList<>(Arrays.asList(arrayOfStrings));
  elementsAsList.clear();
  System.out.println(elementsAsList.isEmpty()); // prints true
}

-

Collection Interface

Object[] toArray()

public void demo() {
  String[] elementsToAdd = {"The", "Quick", "Brown"};
  List<String> elementList = new ArrayList<>(Arrays.asList(elementsToAdd));
  Object[] listAsObjectArray = elementList.toArray();
}

-

Collection Interface

E[] toArray(E[])

public void demo() {
  String[] elementsToAdd = {"The", "Quick", "Brown"};
  List<String> elementList = new ArrayList<>(Arrays.asList(elementsToAdd));

  int newArrayLength = elementList.size();
  String[] arrayToBePopulated = new String[newArrayLength];
  String[] listAsStringArray = elementList.toArray(arrayToBePopulated);
}

-

Iterator interface

public interface Iterator<E> {
  E next();
  boolean hasNext();
  void remove();
  default void forEachRemaining(Consumer<? super E> action);
}

-

Collection Interface
Iterator<E> iterator()

-

Iterable Interface

-

Iterable Interface

public interface Iterable<T> {
  Iterator<T> iterator();

    default void forEach(Consumer<? super T> action){
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }
}

-

Iterator Interface

public static void printIterable(Iterable<Object> iterable) {
    Iterator iterator = iterable.iterator();
    while(iterator.hasNext()) {
        System.out.println("Current Element = " + iterator.next());
    }
}

-

Iterator Interface

public static void printIterable(Iterable<Object> iterable) {
  Iterator iterator = iterable.iterator();
  iterator.forEachRemaining((element) -> System.out.println(element));
}

-

Iterator Interface
next()

-

Iterator Interface
remove()

public void deleteFirstElement(Iterator<String> iterator) {
	iterator.next(); // skip first element
	iterator.remove(); // remove first element
}

-

AbstractCollection Class

-

Sample AbstractCollection implementation

public class MyCollection<E> extends AbstractCollection<E>  {
    private final Iterable<E> iterable;
    public MyCollection(Iterable<E> iterable) {
    	this.iterable = iterable;
    }

    @Override
    public Iterator<E> iterator() {  return iterable.iterator(); }

    @Override
    public int size() {
        List<E> list = new ArrayList<>();
        iterator().forEachRemaining(list::add);
/* `::` method reference operator, calling the method by referring to it
with the help of its class directly - we'll explain this further */
        return list.size();
    }
}

-