Two popular lists in Java are:
1. ArrayList:-Implemented with the concept of dynamic array.
ArrayList<Type> arrL = new ArrayList<Type>(); Here Type is the data type of elements in ArrayList to be created
2. LinkedList:-Implemented with the concept of doubly linked list.
LinkedList<Type> linkL = new LinkedList<Type>(); Here Type is the data type of elements in LinkedList to be created
- Insertions are easy and fast in LinkedList as compared to ArrayList because there is no
risk of resizing array and copying content to new array if array gets full which makes
adding into ArrayList of O(n) in worst case, while adding is O(1) operation in LinkedList
in Java. ArrayList also needs to be update its index if you insert something anywhere except
at the end of array.
- Removal also better in LinkedList than ArrayList due to same reasons as insertion.
- LinkedList has more memory overhead than ArrayList because in ArrayList each index only
holds actual object (data) but in case of LinkedList each node holds both data and address
of next and previous node.
- Both LinkedList and ArrayList require O(n) time to find if an element is present or not. However we can do Binary Search on ArrayList if it is sorted and therefore can search in O(Log n) time.
ArrayList object output :[0. Practice.GeeksforGeeks.org, 2. Code.GeeksforGeeks.org] Found LinkedList object output :[0. Practice.GeeksforGeeks.org, 2. Code.GeeksforGeeks.org] Found
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Program to convert ArrayList to LinkedList in Java
- ArrayList and LinkedList remove() methods in Java with Examples
- ArrayList of ArrayList in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.get(), getFirst(), getLast() in Java
- Java.util.LinkedList.indexOf(), lastIndexof() in Java
- LinkedList in Java
- LinkedList add() Method in Java
- LinkedList set() Method in Java
- LinkedList get() Method in Java
- LinkedList contains() Method in Java
- Reverse a LinkedList in Java
- LinkedList pop() Method in Java