In Java programming, a List is an essential data structure that allows developers to store and manipulate collections of elements. The List interface, part of the Java Collections Framework, offers a wide range of functionalities for managing ordered collections.
In this blog post, we will explore the basics of Lists in Java and demonstrate how to use them effectively with code examples.
Creating a list
To create a List in Java, you need to import the java.util.List package and choose an implementation class such as ArrayList, LinkedList, or Vector. Here's an example of creating an ArrayList:
import java.util.List;
import java.util.ArrayList;
List<String> fruits = new ArrayList<>();
Adding and accessing elements
Once the List is created, you can add elements to it using the add
method and access them by index using the get
method. For instance:
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println(fruits.get(0)); // Output: Apple
Removing and iterating over elements
The objects in the ArrayList can be deleted using two functions both named βremoveβ.
remove (object)
β This function deletes the given object in the ArrayList. If the object appears multiple times, then the first occurrence of the object is deleted.remove (int index)
β This function deletes the data value stored at a given index. After doing so, all the elements that lie on the right of the deleted element are shifted to the left. This overhead makes the overall processβ time complexity large to O(N) where N is the size of the list.
Here's an example:
fruits.remove("Banana");
fruits.remove(1)
System.out.println(fruits);
// Output: [Apple, Orange]
Editing a list
To modify an element inside a list, you can use the appropriate method giving by the List class:
fruits.set(2, "Pamplemousse");
Additional list operations
Other common operations on Lists include checking if a List contains a specific element, getting the size of the List, sorting the List, converting a List to an Array, and clearing all elements in a List. Here are examples of these operations:
//Checking a value in a List
if(list.contains("Orange")) {
System.out.println("List contains Orange");
}
// Getting the list size
System.out.println("Size of the list: " + list.size());
// Sorting a List
Collections.sort(list);
// Converting a List to an array
String[] array = list.toArray(new String[list.size()]);
// Removing all elements in a List
list.clear();
I hope you enjoyed reading this, and I'm curious to hear if this tutorial helped you. Please let me know your thoughts below in the comments. Don't forget to subscribe to my newsletter to avoid missing my upcoming blog posts.
You can also find me here LinkedIn β’ Twitter β’ GitHub or Medium
Conclusion
Lists play a crucial role in Java programming for storing and managing collections of elements. By leveraging the functionalities provided by the List interface and its implementations, developers can efficiently work with ordered data structures in their applications.
This blog post has covered the basics of Lists in Java and demonstrated how to perform common operations with code examples.
Whether you are a beginner or an experienced Java programmer, understanding Lists is essential for developing robust and efficient software solutions.