0

I know it is possible to override the .add behaviour of ArrayList, but how can it be? Where is it defined in the Java docs? What does it mean to have curly braces {} after the generic definition ?

List<Element> SortedListOfElements = new ArrayList<Element>() {
    public boolean add(Element mt) {
        int index = Collections.binarySearch(this, mt);
        if (index < 0) {
            index = ~index;
        }
        super.add(index, mt);
        return true;
    }
};
6
  • Does this answer your question? Problem overriding ArrayList add method Commented Feb 20, 2022 at 10:47
  • docs.oracle.com/javase/tutorial/java/javaOO/… Commented Feb 20, 2022 at 10:48
  • 1
    "I know it is possible to change the .add behaviour of ArrayList" => Well, without any bytecode manipulation this is not possible! Commented Feb 20, 2022 at 10:49
  • 1
    Your code segment does not show a changing of the add method, it shows the overriding of it in a subclass. This is one of the core concepts of OOP. Commented Feb 20, 2022 at 10:57
  • Is the Segment/Element mismatch a typo? Commented Feb 20, 2022 at 11:26

1 Answer 1

1

Where is it defined in the Java docs? What does it mean to have curly braces {} after the generic definition?

This is called anonymous class, you may need to check this tutorial page, such classes do not have specific names in the code, they are used to instantiate interfaces/abstract classes, or modify some known methods as in your case with List::add. The curly braces after the constructor are meant to define a class with some overridden/implemented methods.

Declaration of anonymous classes is described in JLS 15.9.5.


Your solution seems to be almost fine with a few comments:

  • if Collections::binarySearch is applied to instances of Segment class, this class has to implement Comparable<Segment> interface
  • method List::addAll(Collection<? extends T> collection) should be overridden too, because default implementation just appends the elements of collection.
  • similarly, a constructor accepting a collection, may need to be overridden.

Thus it would be worth to provide a generic implementation of a "sorted" list instead of the anonymous class:

public class SortedArrayList<T extends Comparable<T>> extends ArrayList<T> {

    public SortedArrayList() {}

    public SortedArrayList(Collection<? extends T> collection) {
        super(collection);
        this.sort(Comparator.naturalOrder());
    }

    @Override
    public boolean add(T item) {
        int index = Collections.binarySearch(this, item);
        if (index < 0) {
            index = ~index;
        }
        super.add(index, item);
        return true;
    }

    @Override
    public boolean addAll(Collection<? extends T> collection) {
        for (T item : collection) {
            this.add(item);
        }
        return true;
    }
}

Usage:

List<Element> sortedList = new SortedArrayList<>();
Sign up to request clarification or add additional context in comments.

3 Comments

My question is where in the javadocs it says I can put a custom function in a curly braces after the new ArrayList<Element>()? What part of the Java syntax is that?
This is called anonymous class, you may need to check this tutorial page, such classes do not have specific names in the code, they are used to instantiate interfaces/abstract classes, or modify some known methods as in your case with List::add. Declaration of anonymous classes is described in JLS 15.9.5
Thanks. Better to use @Override before the function, this what made me confuse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.