For example, in Jsoup, they defined a class Elements to collect Element(s).
It’s common to define a class A and As which contains a lot of A(s). But why? Why don’t we just use ArrayList or something?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
Elementsclass in JSoup has many specific methods to its function e.g.Which wouldn’t be available on ArrayList. I guess
ArrayListcould have been extended, but the author ofElementshas gone for composition.ElementsHAS-AArrayListinstance which it keeps private.Elementsexposes methods of the List interface, bespoke methods and others.But
Elementsis backed by an ArrayList – this way the author can also decorate the methods of ArrayList he chooses to make public via his own Class. This is an extract from the source code:BTW: You used to see wrappers pre Java 5 to give type safety to Java
Collections, but since Java 5 Generics have stopped a lot of this.