The Collection and the Collections

Collection is a Collection interface. It provides generic interface methods for performing basic operations on collection objects. The Collection interface has many concrete implementations in the Java class library. Is the parent interface of list, set, etc.

Collections is a wrapper class. It contains a variety of statically polymorphic methods for collection operations. This class cannot be instantiated, just like a utility class that serves Java’s Collection framework.

Set is different from List

List and Set are inherited from the Collection interface. Are used to store a set of elements of the same type.

List features: ordered elements, elements can be repeated.

There is order, that is, the elements put in first come first.

Set features: Elements are unordered and cannot be repeated.

No order, that is, the elements put in first do not necessarily come first. Non-repeatable, that is, only one copy of the same element is kept in the set. So, in some scenarios, set can be used to remove weights. However, it is important to note that set has a method to determine whether the element is duplicated when it is inserted.

ArrayList vs. LinkedList vs. Vector

List mainly includes ArrayList, LinkedList and Vector.

All three of them implement the List interface. The main difference is that they have different efficiency for different operations because of different implementation methods.

An ArrayList is a resizable array. As more elements are added to the ArrayList, their size grows dynamically. The internal elements can be accessed directly through get and set methods, because an ArrayList is essentially an array. The initial capacity of an ArrayList is very small. If you can estimate the amount of data, it is best practice to allocate a large initial value to reduce resizing overhead.

LinkedList is a double-linked list that performs better than ArrayList when adding and removing elements. But weaker in lookups than ArrayList, LinkedList takes up more memory than ArrayList because its node stores data as well as two references, one to the previous element and one to the latter.

Vector is similar to ArrayList, but is a strongly synchronized class. If your program itself is thread-safe (thread-safe, not sharing the same collection/object across multiple threads), then ArrayList is a better choice.

Vector and ArrayList request more space as more elements are added. Vector requests double its size at a time, whereas ArrayList increases its size by 50% at a time.

The LinkedList also implements the Queue interface, which provides more methods than List, including Offer (),peek(),poll(), and so on.