Παράδειγμα γενίκευσης

class LinkedList <E> {
    /** Node's value */
    private E value;
    /** Next node */
    private LinkedList <E> next;

    /** Construct a list with a single element v */
    LinkedList(E v) {
        value = v;
        next = null;
    }

    /** Return a list with element n added to it */
    public LinkedList <E> add(E v) {
        var n = new LinkedList <E>(v);
        n.next = this;
        return n;
    }

    /** Return a string representation of the list */
    public String toString() {
        String me = value.toString();

        /* Recursive implementation */
        if (next == null)
            return me;
        else
            return me + " -> " + next;
    }

    /** Test harness */
    static public void main(String args[]) {
        var ilst = new LinkedList <Integer>(0);

        ilst = ilst.add(1);
        ilst = ilst.add(18);
        ilst = ilst.add(45);

        for (int i = 0; i < 5; i++)
            ilst = ilst.add(i * 10);
        System.out.println(ilst);
    }
}