Understanding Lists in Java

Caleb Love
3 min readMay 28, 2021

--

Note: I write these articles to learn new topics. If information in these articles are wrong please notify me. Thanks

Java has lots of different types of Classes and interfaces that store elements in a sequential way. On the one hand Java uses the interface “List” and on the other hand classes that implement the “List” interface but add additional value with features. In the following article I will analyse the different types of storing data in succession and explain how to use them most effectively.

Interestingly enough the “List” interface isn’t the root interface to all types of lists but actually it’s another interface called “Collection”. Classes and interfaces that extend the collection interface have the basic characteristics of what one can imagine of a standard list. Functions included in this interface are for example: add(), addAll(), clear() and contains(). One difference between the List interface and the Collection interface is that the List interface implements another important interface: “Iterable”. This allows a List to be used in a for-loop without having to take extra steps(code below) .

List<String> listInterfaceObj = null;
Collection<String> collectionInterfaceObj;

//List loop
for(String sObj : listInterfaceObj){
Log.d(TAG, "listItem: " + listInterfaceObj);

}

Furthermore, the List interface adds further adequate methods that help the coder to dissect a List. One of these important methods is get(). This method enables a fast and efficient way to retrieve elements from a List. To develop Lists that work in different environments lots of classes implement the List interface even further, such as the ArrayList-, the Vector- and LinkedList class. All of these classes are configured to work better in different types of environments. Vectors are superior to ArrayLists when it comes to multithreading and saving memory. Vectors are built in a way that try to store elements in a list most memory-efficient. In ArrayLists, when number sizes differentiate the memory size is doubled while in comparison the Vector memory size always grows by 50 percent. For instance, when there is a number such as 1242345 as an element in the ArrayList, every single element in that list will have the same size of that large number. This isn’t a problem if the ArrayList contains numbers of the same length but if one of the elements in that specific list is small such as 6 then a lot of space is wasted(this doesn’t happen in Vectors). However, Arrays are much faster because they aren’t synchronised and can endure multiple changes from different threads simultaneously. The Vector awaits the action of the first thread and then when that thread is finished it moves on to the next. LinkedList is similar to the other Lists but is faster and uses more memory at the same time. Furthermore it isn’t synchronous and also implements the Queue interface.

All in all, all lists have different use cases that most of the time don’t overlap. In my Android Apps I mostly use ArrayLists due to the fact that they are fast but also don’t use too much memory and can be accessed from different threads at the same time. Thank you very much for reading this article.

--

--

Caleb Love
Caleb Love

Written by Caleb Love

I am Caleb, a young Android Developer living in Berlin. I love to code fun projects and connect with other people.