×

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

Java List Implementations

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

Java List Implementations

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

Java List Implementations

Related Topics

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

ArrayList vs Vector in Java

ArrayList Vs. Vector in Java In Java, the two classes ArrayList and Vector both are associated with Java Collections Framework. Both classes implement java.util.List interface. Even so, these classes have noticeable...

4 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Java Try Resource

In Java, a try argument that specifies one or more resources is known as a try-with-resources statement. When your program is finished utilizing an object, it must be closed, which...

4 minutes read.

Java StringWriter Class

The StringWriter class is a character stream in which it is used to store the output consisting of characters into the string buffer. Upon collecting output into a string buffer,...

3 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.