×

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

What is anagram in Java?

In this section will explain what an anagram is in Java and demonstrate how to determine whether or not a text is an anagram. In Java interviews, the anagram Java...

4 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

How to generate random numbers in Java

Random numbers, also known as fake numbers, are actually a part of a very large sequence, so they are called random numbers. In a defined set of numbers, every number...

6 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.

How to compare two dates in different format in Java?

We need to compare two dates frequently when coding. Real-world examples include sorting a list of persons by age or keeping track of students' attendance. To compare two dates, we...

7 minutes read.

Knapsack problem in Java

We have a collection of items in the knapsack problem. Every object has a weight and a value. These things should go in a knapsack. But there is a weight...

3 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 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.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

Diffie Hellman Algorithm in Java

In this section, you will be acknowledged about Diffie Hellman algorithm clearly step wise along with an example and also an example program. Diffie Hellman Algorithm One of the most significant algorithms...

3 minutes read.