A List is an in-built interface. It is part of the Collection framework. It can be used to store a group of objects. In this article, we will be learning more about Lists and understanding the various operations that can be performed on a List
- What is a List
- Creating a List
- Adding Data to a List
- Deleting Data from a List
- Determining List Size
- Retrieving Element By Position
- List Iteration
- Other List Operations
What is a List
As mentioned earlier, List is an interface in the Collection framework. It can be used to store a group of objects. The values stored in a List (or any Collection for that matter) are also known as elements. List elements begin at position 0, so the first element is at position 0 and so on.
List is a sub-interface of java.util.Collection. There are many classes that implement the List interface like ArrayList, LinkedList, Vector and Stack. One thing special about a List is that it allows storing duplicate values. Also, a List is ordered, that is elements are stored in the same order in which they are inserted. This allows positional insert, access and removal of elements within the List.
Creating a List
In order to create a List, you need to create an object of any of the classes that implement the List interface like ArrayList, LinkedList etc.
Code Sample
List numbers = new ArrayList(); List strings = new LinkedList();
- Line 1 creates a List called numbers. It stores data of type Integer. It uses an ArrayList implementation
- Line 2 creates a List called strings. It stores data of type String. It uses a LinkedList implementation
Adding Data to the List
The List interface has a method called add. This can be used to add data to the List. It accepts as parameter the value to be added to the List
Code Sample
List animals = new ArrayList(); animals.add("rabbit"); animals.add("lion"); animals.add("giraffe"); animals.add("lion"); System.out.println(animals);
- Line 1 creates a List called animals that stores data of type String
- Line 2 invokes the add method. It specifies the String to be added to the List (In this case, the String rabbit)
- Similarly, Lines 3-4 add some other Strings to the List
- Line 5 adds the value lion again to the List. As mentioned earlier, a List allows duplicate values to be stored in the List. So, the value lion gets added two times to the List
Output
[rabbit, lion, giraffe, lion]
As mentioned earlier, a List allows positional insert. So, you can add an element at a particular position in the List. There is an overloaded version of the add method. In addition to the value being added, it also accepts as a parameter an integer corresponding to the position where the element is to be added.
Code Sample
List animals = new ArrayList(); animals.add("rabbit"); animals.add("lion"); animals.add("giraffe"); animals.add(1,"tiger"); System.out.println(animals);
- As before, Line 1 creates a List called animals and Lines 2-4 add some values to the List
- Line 5 invokes the add method again. It specifies that the value tiger should be added at position 1.
Output
[rabbit, tiger, lion, giraffe]
Deleting Data from a List
Just like add, there is a remove method on the List interface. This can be used to remove an element from the List. It accepts as a parameter the value to be removed from the List
Code Sample
List animals = new ArrayList(); animals.add("rabbit"); animals.add("lion"); animals.add("giraffe"); System.out.println("Before removing lion:"+animals); animals.remove("lion"); System.out.println("After removing lion:"+animals);
Line 7 uses the remove method with the value lion. This causes the value lion to be removed from the List
Output
Before removing lion:[rabbit, lion, giraffe]
After removing lion:[rabbit, giraffe]
As mentioned earlier, a List allows positional delete. So, you can also remove an element by specifying its position. There is an overloaded version of the remove method. This accepts as parameter an integer corresponding to the position from which the element is to be removed.
Code Sample
List animals = new ArrayList(); animals.add("rabbit"); animals.add("lion"); animals.add("giraffe"); System.out.println("Before removing lion:"+animals); animals.remove(1); System.out.println("After removing lion:"+animals);
Line 7 uses the remove method with the value 1. This causes the element at position 1( lion) to be removed from the List. This code produces the same output as before.
Determining List Size
The List interface has a method called size. This returns the size of the List or the number of elements in the List
Code Sample
List animals = new ArrayList(); animals.add("rabbit"); animals.add("lion"); animals.add("giraffe"); int animalsSize = animals.size(); System.out.println("Animals has "+animalsSize+" values");
Line 6 invokes the size method to obtain the List size and assigns it to the animalsSize variable
Output
Retrieving Element by Position
As mentioned earlier, a List allows retrieving an element by specifying its position. It has a method called get which accepts as a parameter an integer corresponding to the position from which the element is to be retrieved.
Code Sample
List animals = new ArrayList(); animals.add("rabbit"); animals.add("lion"); animals.add("giraffe"); String animal = animals.get(2); System.out.println("Animal at position 2:"+animal);
Line 6 uses the animals.get with the value 2. This retrieves the element at position 2 (giraffe) and assigns it to the animal variable.
Output
List Iteration
It is possible to iterate over a List. So, each element from the List can be fetched and processed as required.
Code Sample
List animals = new ArrayList(); animals.add("rabbit"); animals.add("lion"); animals.add("giraffe"); for(String animal:animals) System.out.println(animal); }
Line 6 uses a for-each loop to iterate over the List. This loop simply fetches each element from the List and prints it. Similarly, the other Java loops can also be used to iterate over a List.
Output
rabbit
lion
giraffe
Other List Operations
In addition to the operations listed above, there are several methods on the List interface that help to perform various operations. These are as listed below:
- indexOf – Accepts as parameter a value and returns an integer corresponding to the position of the specified element in the List. Returns -1 if the specified element is not present in the List
- isEmpty – Returns a boolean value which indicates whether the List contains any elements or not
- sublist – Accepts as parameter integers corresponding to the starting position and ending position and returns a List with all the elements between these positions
- contains – Accepts as parameter a value and returns true if the specified value is present in the List
- removeAll – Accepts as parameter a Collection and removes all the elements from the List which are present in the input Collection
- addAll – Accepts as parameter a Collection and adds all the elements in the input Collection to the List
- sort – Sorts the List as per the specified Comparator
Conclusion
So, in this article, we learned about Java Lists and some of the operations that can be performed on a List. We saw how to add data to the List, remove data from the List, obtain the size of the List and iterate over a List. Finally, we saw some of the other operations that can be performed on Lists.