JCF: Java Collection Framework Hierarchy

Pasted image 20251014010626

_Bfore starting implementing JCF should you know the Iterable interface

1. Iterable

  • Iterable interface is a builtin interface in java it allow us using “for-each” loop thanks to Iterator method

let’s take an example if you work with arrays in java and suppose you create an new array

int[] arr = new Interger[]{1,2,3}

and you want to make the forEach loop like this for(int n : arr) => this will give you a compiler error

you can create a custom one let’s say MyArray and implement the Iterable interface

class MyArray<T> implements Iterable<T> {
    private T[] data;
    private int size;

    public MyArray(T[] data) {
        this.data = data;
        this.size = data.length;
    }

    @Override
    public Iterator<T> iterator() {
        return new Iterator<T>() {
            int index = 0;
            public boolean hasNext() { return index < size; }
            public T next() { return data[index++]; }
        };
    }
}

now you can use:

MyArray<Integer> arr = new MyArray<>(new Integer[]{1, 2, 3});
for (int n : arr) {
    System.out.println(n);
}

and boom your class works with forEach loop

Table of content of the implementation of each component

[[Collection Interface]]