Why do some libraries define their own collections?

For example, in Jsoup, they defined the Elements class to collect Elements.

Common to define the class A and As, which contains a lot of A (s). But why? Why don't we just use an ArrayList or something else?

+5
source share
9 answers

A class Elementsin JSoup has many special methods for its function, for example.

toggleClass(String className) 
html()

ArrayList. , ArrayList , Elements . Elements HAS-A ArrayList, . Elements List, bypoke .

Elements ArrayList - ArrayList, . :

  private List<Element> contents;

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

. pre Java 5 Java Collections, Java 5 Generics .

+5

, API, , , Elements HTML . , . , , Elements Iterable, Collection List.

+5

, . , , , Elements - List, ArrayList. , ArrayList, . , . Elements, , Element, JSoup HTML-, ...

+3

:

  • .
  • .
  • .

, ( ) . ArrayList , , , ++ , ++ .

+2

, - , java 1.4 : Generics, ArrayList<String> , .

+1

, List ( ) . , , .

, , Elements infact List<Element>.

0

, , , .. , . , . Google Guava Apache

0

Composite. , . , , .

0

, :

For some libraries, such as KryoNet, for tight optimization (that is, prohibit null keys and values, block keys and values ​​for a specific type), it is necessary to determine the specific use of the collection class. The best way to do this is through an extension, as this means that the card Class->ClassOptimisation, as an example, is working fine. Otherwise, you will get loading instances of the class that are optimized in your own way that you do not need.

0
source

All Articles