Last updated on August 3rd, 2024
In this topic, we will learn about the difference between ArrayList and LinkedList. The ArrayList and LinkedList are both predefined implementation classes of the List interface. These classes are available in the java.util package.
ArrayList vs LinkedList
ArrayList | LinkedList |
This class inherits the features of a list as it implements the List interface. | This class inherits the features of a list and queue as it implements the List and Dequeue interfaces. |
This class’s underlying data structure is a resizable or growable array. | The underlying data structure is double LinkedList. |
This class has a default capacity is “10”. | This class has no default capacity. |
This class provides a better performance for add and search operations. | This class provides better performance for insertion or deletion in the middle. |
This class implements the RandomAccess interface. | This class is not implemented RandomAccess |
There are a few similarities between ArrayList and LinkedList:
• Both classes are implemented List interface.
• Both classes are insertion order is preserved.
• Both classes are allowed heterogeneous objects.
• Both classes have null insertion is possible.
When to use ArrayList and LinkedList?
• If more searches, fewer add and fewer remove operations then in this case use of ArrayList is the best choice.
• If frequent addition and deletion operations then in this case use of LinkedList is the best choice.
Example of ArrayList in Java
In this example, we are creating an ArrayList object and adding some elements to the created ArrayList.
import java.util.ArrayList;
public class SpringJava{
public static void main(String args[]){
//creating an ArrayList object
ArrayList<String> al=new ArrayList<>();
//adding elements
al.add("Pen");
al.add("Pencil");
al.add("Book");
al.add("Notebook");
//printing an ArrayList
System.out.println("ArrayList elements: "+ al);
}
}
Output
ArrayList elements: [Pen, Pencil, Book, Notebook]
Example of LinkedList in Java
In this example, we are creating a LinkedList object and adding some elements to the created LinkedList adding an element at the first position and removing an element from the last position.
import java.util.LinkedList;
public class SpringJava{
public static void main(String args[]){
//creating a LinkedList object
LinkedList<String> l=new LinkedList<>();
//adding elements
l.add("Pen");
l.add("Pencil");
l.add("Book");
l.add("Notebook");
//printing a LinkedList
System.out.println("LinkedList elements: "+ l);
//add element at the first position
l.addFirst("Bag");
//printing a LinkedList
System.out.println("LinkedList elements: "+ l);
//remove element from the last position
l.removeLast();
//printing a LinkedList
System.out.println("LinkedList elements: "+ l);
}
}
Output
LinkedList elements: [Pen, Pencil, Book, Notebook]
LinkedList elements: [Bag, Pen, Pencil, Book, Notebook]
LinkedList elements: [Bag, Pen, Pencil, Book]
FAQ
Q1: When would you choose to use ArrayList over LinkedList?
Ans. When we use ArrayList in case more search operations than adding and deleting scenarios.
Q2: Why ArrayList is faster than LinkedList?
Ans. In the ArrayList, all the elements are located next to each other in the same memory space.
Q3: Is ArrayList more efficient than LinkedList?
Ans. ArrayList is faster in storing and searching data.
Conclusion
In this topic, we learnt about the differences between ArrayList and LinkedList with examples and when to use ArrayList and LinkedList in Java.
Thank you for every other great post. Where else could anyone get that kind of info in such an ideal manner of writing? I have a presentation subsequent week, and I’m on the look for such information.
Hey there! Someone in my Facebook group shared this site with us so I came to check it out. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers! Fantastic blog and superb style and design.
Hello to all, the contents existing at this web site are in fact awesome for people knowledge, well, keep up the nice work fellows.
I am regular reader, how are you everybody? This paragraph posted at this web site is truly pleasant.
Somebody necessarily lend a hand to make critically articles I might state. That is the very first time I frequented your web page and thus far? I amazed with the research you made to create this particular submit amazing. Wonderful job!
Dear springjava.com admin, You always provide in-depth analysis and understanding.
Quality articles or reviews is the secret to attract the visitors to go to see the web page, that’s what this website is providing.
We are a gaggle of volunteers and starting a new scheme in our community. Your web site offered us with valuable information to work on. You have performed a formidable task and our entire group will probably be thankful to you.
You could certainly see your expertise in the work you write. The world hopes for more passionate writers like you who aren’t afraid to say how they believe. All the time go after your heart.
Hello springjava.com owner, Great post!