Why does some libs define their own collections?

Lai Yu-Hsuan :

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?

planetjones :

The Elements class in JSoup has many specific methods to its function e.g.

toggleClass(String className) 
html()

Which wouldn't be available on ArrayList. I guess ArrayList could have been extended, but the author of Elements has gone for composition. Elements HAS-A ArrayList instance which it keeps private. Elements exposes methods of the List interface, bespoke methods and others.

But Elements is 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:

  private List<Element> contents;

    public Elements() {
        contents = new ArrayList<Element>();
    }

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does a dictionary count in some cases faster than collections.Counter?

Why use sql tags in struct in some go libs like gorm?

Why does gcc force PIC for x64 shared libs?

Why does play.libs.Json.toJson return an empty object?

Why does cmake_link_libraries include static libs?

Shared Libs on Linux: why does the dynamic linker run in user context?

Why does DateTime() not define midnight?

Why does "#define A" interfere with "namespace A{}"?

Why do some people define objects as pointers?

Why don't define some undefined behaviours?

Why do c++ libraries often define their own primitive types?

Why do so many libraries define their own fixed width integers?

Why would we still need to define our own function comparator

Why does generics in java collections so strange?

Why does C# not implement GetHashCode for Collections?

Why does Kotlin have Mutable versions of collections?

why my own package does not in the GOROOT

Why does BigQuery has its own storage?

Why does root own the dot dot ".." directory?

why does my class is executing on its own?

In clojure, why doesn't "some" function work consistently on collections?

Why use /apps/libs instead of /libs in Rails?

Why do some packages require their own user to run?

Why do some downloading files not know their own size?

Why does Iterator define the remove() operation?

Why OpenAPI does not define '$ref' as allowed property?

Why does React fail to define 'App' component?

Why does the Linux kernel #define a symbol as itself?

Why does anyone use define(['jquery'...], function($...){...}