×

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

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Converting Roman to Integer Numerals in java

In this article, you will be acknowledged about the process of conversion of roman numbers to integers in java. The most important approaches are discussed and also the implementation of...

3 minutes read.

Determine the Upper Bound of a Two-Dimensional Array in Java

Multi-Dimensional Array Multi-faceted exhibits in Java are regular and can be named various clusters. Information in a two-layered bunch in Java is put away in 2D even structure. A two-layered collection...

3 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Web Service Response Time Calculation in Java

Administration response time or reaction time is the typical measure of time it takes for a solicitation to be handled by a PC framework, like your organization switch. In the...

4 minutes read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Java Math floorDiv() Method

The floorDiv () method of Math class returns the largest integer value that is less than or equal to the algebraic quotient. It firstly divides the dividend and divisor and...

2 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Java Characters

Normally, when we work with characters, we use primitive data types char. When we have to work with the objects of char, we use Character class. Character class has many important...

2 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Java Variable Declaration

In this article, you will be acknowledged about java variable declaration. You will be able to learn and interpret about declaring a variable in Java. Variable in Java Variables are necessary for...

6 minutes read.

Java ArrayList

Java ArrayList Class A Java ArrayList class is a dynamic array which is used to store the elements. It is a part of collection framework. It implements the List Interface and inherits the...

12 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

2 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.