Collection vs Collections in Java
Tags: java, architecture
Published: 2020-01-29 14:42:47


<h2>Collection Framework</h2>

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 <b>java.util.package</b>. 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.
<figure>
<img src="https://static.javatpoint.com/images/java-collection-hierarchy.png">
<figcaption>Image source: <a href="https://www.javatpoint.com/collections-in-java" target= "_blank">javatpoint/collections</a>
</figcaption>
</figure>

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


1. <b>ArrayList</b>: 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. <b>LinkedList</b>: 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. <b>HashSet</b>: 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:

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

  

4.<b>LinkedHashSet</b>: 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 <a href="https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html" target="_blank">Javadoc</a> to see all implementing classes.   
 


<h2>Collections</h2>

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. 

<code>List<Theatre.Seat> myList = new ArrayList<>(theatre.getSeats());</code>

You can rotate the elements in the list:

<code>Collections.rotate(myList, 4);</code> 

Or you can reverse them:

<code>Collections.reverse(myList);</code>

You can see further methods <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html" target="_blank">here</a>.

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