Collections Java

Java - Collections

Java Collections is a collection of interfaces and classes that help store, order, and process data efficiently. This framework contains several useful classes that in turn contain hundreds of methods, making programming simple and easy. Collections reduce programming time and increase quality and performance.

A collection is an object that collects items in a single list. A collection represents a homogeneous set of data. For example, an email box (mail collection), or a phone book (list of names and their numbers). This site has different tutorials on object collections with examples to help you understand.

The architecture of the Java Collections framework

The Collections framework has a hierarchical architecture to represent all collections that contain or inherit interfaces, implementations, and methods such as object sorting. The interfaces are divided into two groups. The basic interface is java.util.Collection. The other interface is java.util.Map.

java collections

List

The List interface is an ordered collection and it can contain duplicate items. List inherits the methods from the Collection interface, the List interface includes operations such as manipulating items, searching, and browsing with Iterator.

Set

The Set interface eliminates duplicate elements. It is the representation of the mathematical model of sets in java. It inherits the methods from the Collection interface and adds the property of prohibiting duplicate items that must exist only once. It stores items in a hash table which is a great asset for high performance, but it doesn't guarantee the order in which items are inserted when traversing.

SortedSet

The SortedSet interface inherits the methods from the Set interface. It keeps the elements in ascending order or according to the declaration of the compare() method in the Comparator interface passed as arguments when instantiating TreeSet.

Map

The Map interface stores the key peers and its value in a hash table. A Map cannot contain duplicate elements, each key has a single value. Map can be implemented with:
- HashMap: it does not guarantee the order in which elements are inserted during the journey.
- LinkedList: it guarantees the path of the elements based on the order of their insertion.
- TreeMap: it stores the elements sorted according to their values.

SortedMap

Like the SortedSet interface, which inherits its upper Set interface, the SortedMap interface also inherits from Map and implements methods to order elements in ascending and descending order.

Iterator and ListIterator

Both of these interfaces are members of the java framework Collections. Iterator is used to iterate through all collections of objects in the ascending direction, but especially to iterate through classes that inherit from the Map interface and do not allow the for loop. ListIterator allows you to travel in both ascending and descending directions .

Difference Between Collection and Collections

- Collection is the root interface in the java framework Collections hierarchy. This means that each class implements Collection.
- Collections is a utility class that is a member of the java framework Collections and has static methods for manipulating objects that implement the interface Collection. For example, sorting an ArrayList in ascending order:

Collections.sort(arraylist);