Java Lists: Complete Guide

Our insightful guide focuses on different types of lists available in java. It provides an overview with proper code implementations for the creation of a list. It describes various operations for adding, removing, retrieving, and updating the values of a list. The article also highlights the differences between types of lists. So you know when to use each list type, and how to sort a list in java.

Lists in Java

A list is a collection of ordered items available in java. All the elements or objects inside a list in java have a particular index for searching. A list also provides in-built operations for adding, removing, updating, deleting the values or items inside a list. Lists are a part of java’s collections interface.

Collections is a java framework that provides data structures and architectures ready to implement. These architectures can be used for manipulating or storing class objects. Collections also contain in-built algorithms for searching and sorting various data structures present in them.

The list is a subclass of collections that further contains the data structures for:

  1. ArrayList: An ArrayList is a dynamic data structure just like an array. The significant difference between a regular array and an ArrayList is that an ArrayList is dynamic in nature.
  2. LinkedList: Java has a doubly-linked list of nodes where each node contains some data and two references to locations. One to the previous node and the other to the next node of the linked list.
  3. Vector: A vector is also a dynamic data structure that acts like an ArrayList. However, unlike an ArrayList, a vector is synchronized and only suitable for multi-threading environments.

A thread executes a snippet inside a program to allow multiple jobs to run concurrently and efficiently. A vector allows only one thread to access the code at a time. This ensures that multiple threads working on the same vector list do not overlap and cause data loss or irregular update or delete operations.

Adding and removing values in java lists

All subclasses of the list interface in java have in-built operations for quick addition, removal, and updating data. These are:

  • Add(): The add() operation allows you to add an element. The new value or element is always added to the end of the list. If you want to specify the location to add the element, you can do this by using add(index, value).
  • Remove(): The remove operation on the list removes a value. If you want to remove the value from a specific index, you can do so by using remove(index).

Let us show you how you can use the add() and remove() operations on a list in java.

import java.util.ArrayList;
public class algetset {
    public static void main(String[] args) {
        
        ArrayList<Integer> al = new ArrayList<>();
        
        //Adding new elements
        al.add(30);
        al.add(10);
        al.add(20);
        al.add(40);
        
        System.out.println("ArrayList: "+al);
        
        //Removing element at index 2
        al.remove(2);
        
        //Adding element at 2
        al.add(2,100);
        
        System.out.println("ArrayList after replacing 2: "+al);
        
}
}    

Output:

ArrayList: [30, 10, 20, 40]
ArrayList after replacing 2: [30, 10, 100, 40]

Here, the al.add() is used to add new values with and without providing the index. The al.remove() method is used to remove the value from the list by providing its index.

How to modify get and set values using index in java lists?

Lists in java also have in-built methods for recovering or setting values at a specific list index. These are:

  1. get(): The get() method is used to get the value from a specific index using the syntax, listname.get(indexVAL).
  2. set(): The set() operation is for updating the value of an element. The set() method is like the get method but takes an extra attribute as the value you want to set at that index. The syntax for set() is listname.set(indexVal,NewValue).

Let us take a look at how you can use the get() and set() operations inside a list.

import java.util.ArrayList;
public class algetset {
    public static void main(String[] args) {
        
        ArrayList<Integer> al = new ArrayList<>();
        
        //Addition
        al.add(30);
        al.add(10);
        al.add(20);

        System.out.println("Old List Elements: "+al);
        
        //Getting element and printing
        System.out.println("Retrieval using get: "+al.get(0));
        
        //Setting 5 at index value 0
        al.set(0,5);
        
        System.out.println("New Elements: "+al);
        
}
}    

Output:

Old List Elements: [30, 10, 20]
Retrieval using get: 30
New Elements: [5, 10, 20]

Here, we have created a list with 30,10,20 as its values. Then we use the al.get() method to retrieve the value from the particular index and the al.set() to provide a new value at the particular index.

It would be best to remember you should use an ArrayList if your program requires additions and deletions at the end and random access of data using the get and set methods as it does not use a pointer like the LinkedList and will perform better.

However, a LinkedList will perform better if you need regular additions or data removal at specific indexes.

How to sort a list in java?

The collections interface provides easy and quick sorting of all data structures present in it. You can use Collections.sort(listname) method, which can sort lists of all data types, i.e., Integer, Character, Double, and more.

Let us sort our list from the above program using Collections.sort().

import java.util.*;
public class aldemo {
    public static void main(String[] args) {
        
        ArrayList<Integer> al = new ArrayList<>();
        
        al.add(30);
        al.add(10);
        al.add(20);
        al.add(40);
        al.add(50);
       
        System.out.println("Before Sorting: "+al);
        
        Collections.sort(al);
        
        System.out.println("After Sorting: "+al);
     
    }
}

Output:

Before Sorting: [30, 10, 20, 40, 50]
After Sorting: [10, 20, 30, 40, 50]

Differences between ArrayList, LinkedList, and Vector

Efficiency: The retrieval of a value from a specific index, addition, or deletion of a value at the end inside an ArrayList and a Vector is O(1), but the addition or deletion at a specific index in O(NxIndex) where N is the number of elements present in the ArrayList or Vector. A LinkedList takes O(1) for the addition or deletion of values regardless of their index.

Synchronization: A Vector is synchronized. But ArrayList and LinkedList are not synchronized. This means that Vectors can perform better in concurrent environments of multi-threaded jobs inside a program. ArrayList and LinkedList can also be synchronized but need extra coding and are still less efficient.

Data Storage: Both Vector and ArrayList use an array of objects internally to store values. The size of the array has to be increased to accumulate the addition of a new element or value. In adding a new value to an already full array, the vector doubles its original size, whereas an ArrayList adds an extra block. You can use this to your advantage by using vectors, where you need to store more data as it will help you reduce resource overhead.

Conclusion

You have reached the end of an excellent guide on java’s lists. You have learned the different types of lists in java. Learned various operations for deletion, addition, and updating the elements inside a list. You can also sort a list in java using collections and understand the differences between an ArrayList, LinkedList, and Vector so you can implement them correctly according to your program’s requirements.

Please keep checking in for more informative guides on our website. You can also join our live classes for Python Programming, Data Analytics, and SQL via our skill enhancement program.

Good Luck with your coding!

Leave a Reply