List Data Structure is mostly similar to the arrays data structure but with few tweaks and upgrades. List is an abstract data type where the stored data can be both homogenous and heterogeneous in an ordered sequence. These are sometimes referred to as sequences as well. Unlike arrays, lists do not require any pre-definition of size while declaring or initializing a list. The size of a list grows while we continue to insert data in it.
Operations on a List:
There can four basic operations that are required while working with list:
- Insertion of data in the List
- Deletion of data in the List
- Updation of data in the List
- Find an element in the List.
Before moving on with the usage, let’s understand how to define a List. We will use Java in this example. However, it is important to understand that the syntax would although vary for different languages but the operations and usage requirement remains the same.
Java ArrayList implementation:
For Java, in order to use list, we need to import List and ArrayList interfaces from the java.util package.
List<String> exampleListOfString = new ArrayList<>();
Now that we have understood initializing a list, let’s look at how the operations can be done.
Insertion of Data:
List<String> nameList = new ArrayList<>();
nameList.add("John");
nameList.add("Margaret");
nameList.add("Robert");
for(String names : nameList){
System.out.println(names);
}
The above code will store the entered names in the nameList variable of type List and would finally print the names one by one in the for each loop. The output would be the below:
John
Margaret
Robert
Deletion of Data
To delete an item from the list, we use remove() method and pass either the index or the value of the item to be deleted. Let’s look at it with an example.
List<String> nameList = new ArrayList<>();
nameList.add("John");
nameList.add("Margaret");
nameList.add("Robert");
nameList.add("Smith");
nameList.add("James");
nameList.remove(1);
nameList.remove("Smith");
for(String names : nameList){
System.out.println(names);
}
The above code will remove the item present at index 1 i.e “Margaret”. Again it would delete the item with the value “Smith” and would finally print the names one by one in the for each loop. The output would be the below:
John
Robert
James
Updation of Data
To update an item in the list, it is important to know it’s index in the list. After that, we can use set() method to update the value of the item in that index position. Let’s look at the example below:
List<String> nameList = new ArrayList<>();
nameList.add("John");
nameList.add("Margaret");
nameList.add("Robert");
nameList.set(1,"Martha");
for(String names : nameList){
System.out.println(names);
}
The above code will update the item in index position 1 from “Margaret” to “Martha” and would finally print the names one by one in the for each loop. The output would be the below:
John
Martha
Robert
Finding Element in a List
Finding can element usually include three points. To know, if an element is present in the list, then to know the position of that element (searching for the element) and finally accessing the element. Let’s look at the example below to understand all the above three use cases one by one.
List<String> nameList = new ArrayList<>();
nameList.add("John");
nameList.add("Margaret");
nameList.add("Robert");
nameList.add("Smith");
nameList.add("Robert");
boolean robertPresent = nameList.contains("Robert");
System.out.println(robertPresent);
int indexOfFirstRobert = numbers.indexOf("Robert");
System.out.println(indexOfFirstRobert);
int indexOfLastRobert = numbers.lastIndexOf("Robert");
System.out.println(indexOfLastRobert);
System.out.println(nameList.get(0));
System.out.println(nameList.get(indexOfFirstRobert));
The above code will store the entered names in the nameList variable of type List and would finally print the names one by one in the for each loop. The output would be the below:
true
2
4
John
Robert
Arrays to List and Vice Versa
Now, we know how to work with Lists. While working with building real world applications, lists are preferred over arrays since the operation on them are easier and incur lesser lines of code increasing it’s readability. However, there are always requirements to work with arrays. So, it is in our best interest to know, how to convert arrays to list and vice versa.
String[] nameArray={"John","Margaret","Robert","Smith"};
// Converting arrays to list
List<String> nameList=new ArrayList<String>();
for(String name : nameArray){
nameList.add(name);
}
// Converting the list again back to array
String[] arrayFromList = nameList.toArray(new String[nameList.size()]);
The operations in a List usually has a time complexity of O(n). If you do not know what this means, go through the guide on understanding Big O notation.
We now should have a basic understanding of lists and it’s usage. Let’s look at some other methods present in the list interface.
List Interface Methods
size() | return the size of the list |
clear() | removes all the items in a list |
sort(Comparator comparator) | sorts the items in a list based on the comparator |
addAll(int index, Collection collection) | adds all the items in the collection in the given index of the list. If no index is passed, it gets added in the end. |
containsAll(Collection collection) | Validates the presence of all the items in the collection in the items in the list. |
isEmpty() | checks if a given list is empty. |
equals(Object obj) | checks for the equality of the specified object obj with the items in the list data structure. |
Read more about Java List and the other interfaces of the collections framework: