Java List
What is Java List?
- In Java, a List is an interface that represents an ordered collection of elements that allows duplicates. It is a part of the Java Collection Framework and is implemented by various classes such as ArrayList, LinkedList, Vector, etc.
- The List interface provides various methods to add, remove, retrieve, and manipulate elements in the collection. Some of the commonly used methods of the List interface include add(), remove(), get(), set(), size(), and indexOf().
- To understand the Java List, Read the Complete Article.
Classes that Implement List:
In Java, the List interface is implemented by several classes, each with its own characteristics and advantages.
- ArrayList: It is an implementation of List that uses an array to store the elements. It is a dynamic array, which means that it can grow or shrink dynamically based on the number of elements.
- LinkedList: It is an implementation of List that uses a doubly-linked list to store the elements. It provides fast insertion and deletion at the beginning or end of the list but slower random access.
- Vector: It is similar to ArrayList but is synchronized, which means that it is thread-safe. It is a legacy class that was introduced in JDK 1.0 and is not recommended for new development.
Stack: It is a subclass of Vector and provides a stack data structure, where the elements are added and removed in a Last-In-First-Out (LIFO) manner.
These are some of the commonly used classes that implement the List interface in Java.
Let’s look at the Java getOrDefault() Method to perform certain operations.
Example 1:Java Program to Implementing the ArrayList Class
import java.util.ArrayList; import java.util.List; public class Main { public static void main (String[]args) { // Create an instance of ArrayList List < String > myList = new ArrayList < String > (); // Add elements to the ArrayList myList.add ("RealMadrid"); myList.add ("Mancity"); myList.add ("Man utd"); // Print the ArrayList System.out.println ("My List: " + myList); // Modify an element in the ArrayList myList.set (1, "Barca"); // Remove an element from the ArrayList myList.remove ("Man utd"); // Print the ArrayList again System.out.println ("My List after modification: " + myList); // Retrieve an element from the ArrayList String firstElement = myList.get (0); System.out.println ("First element of the List: " + firstElement); // Iterate over the elements of the ArrayList for (String element:myList) { System.out.println (element); } // Get the size of the ArrayList int size = myList.size (); System.out.println ("Size of the List: " + size); } }
Output
My List: [RealMadrid, Mancity, Man utd] My List after modification: [RealMadrid, Barca] First element of the List: RealMadrid RealMadrid Barca Size of the List: 2
Example 2 : Java Program to Implementing the LinkedList Class
import java.util.LinkedList; import java.util.List; public class Main { public static void main (String[]args) { // Create an instance of LinkedList List < String > myList = new LinkedList < String > (); // Add elements to the LinkedList myList.add ("Samsung"); myList.add ("Nokia"); myList.add ("MI"); // Print the LinkedList System.out.println ("My List: " + myList); // Modify an element in the LinkedList myList.set (1, "Motorola"); // Remove an element from the LinkedList myList.remove ("MI"); // Print the LinkedList again System.out.println ("My List after modification: " + myList); // Retrieve an element from the LinkedList String firstElement = myList.get (0); System.out.println ("First element of the List: " + firstElement); // Iterate over the elements of the LinkedList for (String element:myList) { System.out.println (element); } // Get the size of the LinkedList int size = myList.size (); System.out.println ("Size of the List: " + size); } }
Output
My List: [Samsung, Nokia, MI] My List after modification: [Samsung, Motorola] First element of the List: Samsung Samsung Motorola Size of the List: 2
In Java, List and Set are interfaces that define collections of objects. However, there are some key differences between them:
Order: A List maintains the order of elements in which they were inserted, whereas a Set does not guarantee any specific order of its elements.
Duplicates: A List can contain duplicate elements, whereas a Set cannot contain duplicate elements.
Retrieval: A List allows elements to be retrieved by their index position, whereas a Set does not provide a way to retrieve elements by their index position.
Performance: Depending on the implementation, some operations such as searching or removing an element can be faster in a Set compared to a List, because a Set uses a hash-based algorithm for storing elements and checking for duplicates.
Iterator: Iterating over the elements of a List is generally slower compared to a Set because it needs to maintain the order of the elements, whereas a Set can iterate over its elements more efficiently.
In general, you would use a List when you need to maintain the order of elements and allow duplicates, and you would use a Set when you need to ensure that there are no duplicates in the collection and order is not important. The choice between List and Set depends on the specific requirements of your application.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment