Java List Implementations
ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time positional access. When moving numerous pieces at once, it can make use of System.arraycopy rather of allocating a node object for each element in the List. ArrayList can be compared to Vector without the synchronisation costs.
You might think about utilising LinkedList if you routinely add elements to the List's beginning or repeatedly iterate over the List to remove members from its inner. In a LinkedList, these operations take constant time, whereas in an ArrayList, they take linear time. But your performance comes at a high cost.
ArrayList in Java
The java.util package contains the essential collection building block ArrayList. Dynamic arrays are provided in Java. In programmes that demand a lot of array manipulation, while being slower than standard arrays, it could be useful. In the java.util package, this class may be found.
Program for ArrayList implementation in Java
ArrayListImplementation.java
import java.io.*;
import java.util.*;
class ArrayListImplementation{
public static void main(String[] args)
{
Scanner sc1 = new Scanner(System.in);
System.out.println ( " Enter the size of ArrayList " ) ;
int n=sc1.nextInt();
// Declaring the ArrayList
ArrayList <Integer> a1 = new ArrayList <Integer> (n) ;
// using for loop to add elements to the ArrayList
for ( int i = 1 ; i < n ; i++ )
a1 . add ( i ) ;
// printing the ArrayList
System.out.println ( " The ArrayList is " ) ;
System . out . println ( a1 ) ;
// removing the element at index 2
a1 . remove ( 2 ) ;
System.out.println ( " The ArrayList after removing element is " ) ;
System . out . println ( a1 ) ;
// using for loop to add elements to the ArrayList
for ( int i = 0 ; i < a1 . size ( ) ; i++)
System . out . print ( a1 . get ( i ) + " ") ;
}
}
Output

Linked List in Java
The Linked List element of the Collection architecture is part of the java.util package. The LinkedList data structure is a linear data structure in which each element is a unique object with a data portion and an address component that are not maintained in a single location. Pointers and addresses are used to connect the elements. Each element is referred as a node.
LinkedList constructors
An object of the LinkedList class must be created before a LinkedList may be created. The LinkedList class includes a number of constructors that permit the list to be created. The constructors accessible in this class include the following:
1. LinkedList():
An empty linked list is created with this function Object().
Syntax
LinkedList r = new LinkedList();
2. LinkedList(Collection C)
This function Object()is used to build an ordered list of all the items in the collection that has been supplied, as returned by the iterator for that collection.
Syntax
LinkedList l = new LinkedList(C);
Program for Linked List Implementation in Java
LinkedListImplementation.java
// Main class
public class LinkedListImplementation {
// Driver code
public static void main ( String args [ ] )
{
LinkedList < String > l = new LinkedList < String > () ;
l . add ( " R " ) ;
l . add ( " O " ) ;
l . addFirst ( " I " ) ;
l . addLast ( " H " ) ;
l . add ( 2 , " T " ) ;
System . out . println ( l ) ;
l. remove ( " B " ) ;
l . remove ( 3 ) ;
l . removeFirst ( ) ;
l . removeLast ( ) ;
System . out . println ( l ) ;
}
}
Output

Program for operations on Linked list
ListOperations.java
import java.util.*;
public class ListOperations {
public static void mainv( String args [ ] )
{
LinkedList < Integer > l = new LinkedList < > ( ) ;
l . add ( 10 ) ;
l . add ( 20 ) ;
l . add ( 1 , 30 ) ;
System . out . println ( l ) ;
// Updation of elements
System . out . println ( " Changing the value " ) ;
l . set ( 1 , 50 ) ;
// After changing the values of LinkedList
System . out . println ( l ) ;
// removal of elements
ll . remove ( 1 ) ;
// After changing the values of LinkedList
System . out . println ( l ) ;
// Iteration
for ( int i = 0 ; i < l . size ( ) ; i++ ) {
System . out . print (l . get ( i ) + " " ) ;
}
}
}
Output
