DEV Community

Derya A. Antonelli
Derya A. Antonelli

Posted on

Collection vs Collections in Java

Collection Framework

The Collection is the top-level interface of the Java Collection Framework.
List, Queue and Set are the main sub interfaces of this framework. Collection interface is a member of java.util.package. There are several methods in Collection interface for adding/removing elements, getting the size of an array, iteration, or check if an array is empty.

Image source: javatpoint/collections

I'd like to present an informal overview of some of the classes of Collection.

  1. ArrayList: Uses dynamic array to store elements. Duplicates are allowed in this list. It is also index-based, which is a perfect match for those who want to insert data in order, retrieve based on its index, and access randomly.
    For instance, you can create a program that implements a simple mobile phone, which is able to store, modify, remove and query contact names and hold the contacts in ArrayList. You may, as well, create a playlist and add/remove albums.
    Please bear in mind that removal of an element changes the array order.

  2. LinkedList: Uses doubly linked list to store elements. In basic terms, let's say you are a group of friends who want to form a chain in a LinkedList, so that you might accept new friends and remove existing ones faster(Wow, this is rude). The rule is simple; you will hold the address reference of your left and/or right side friends depending on where you are. Any newcomers are welcome, and existing ones are free to leave whenever they desire. When someone leaves the chain, left and right elements delete his/her address and link to the previous and/or next friends. No one in this chain needs to move on to fill the space left by previous member(s)! That means, the operation will be quicker than the one in an ArrayList, which sounds cool. (This really matters if you are thousands of friends.) You can also ask the iterator if there's someone in your right or left, and move on!

  3. HashSet: Uses hashing to store the elements. Duplicates are not welcome, insertion order is not maintained, null value is allowed. Here immutability is of great importance because if you prefer non-primitive data types in the array, modification of an object/object class may lead to unwanted results. In order to store your objects in HashSet, you need to override equals() and hashCode() methods. For instance, you implement a student class with name and school number class members. In order to insert each student to HashSet, first of all, HashSet needs to know your equality criteria before insertion. In other terms, you define which students are equal and which are not. In this way, HashSet will not add duplicates that are equal to the list. Each student's location is calculated through HashCode() method. Searching for a student on this list is very effective and fast.

An example of how to override HashCode() is:

@Override
public int hashCode() {
return this.name.hashCode() +11;
}

4.LinkedHashSet: In addition to the features inherited from HashSet class, you can keep the insertion order.

The classes are not limited to the above. You can check Javadoc to see all implementing classes.

Collections

Java Collections class, on the other hand, is a support tool which includes a variety of useful methods that work with collections of Java. Do you want to rotate, sort, reverse the elements in your array? You are on the right address.
If your collection elements are List type, there are static methods provided by Collections class for sorting. (For Set type items, you can use TreeSet for this purpose).

Let's assume you have a Theatre class with Seat inner class. You create an ArrayList with Seats.

List myList = new ArrayList<>(theatre.getSeats());

You can rotate the elements in the list:

Collections.rotate(myList, 4);

Or you can reverse them:

Collections.reverse(myList);

You can see further methods here.

Thanks for reading. See you until my next post!
Derya

Top comments (0)