Types of Lists in Java

Type of lists in java: Here, we are going to learn about the various types of the lists like Array lists, vectors and linked lists with examples. By Karan Ghai Last updated : March 23, 2024

Qualities added to the rundown depend on the file position and it is requested by list position.

Types of Lists are:

  1. Array List
  2. Vector
  3. Linked List

1) Array List

  1. Fast iteration and fast Random Access.
  2. It implements the Random Access Interface.
  3. It is an ordered collection (by index) and not sorted.

Example of Java ArrayList

import java.util.ArrayList;

public class Main {
    public static void Fruits(String[] args) {
        ArrayList < String > names = new ArrayList < String > ();
        names.add("mango");
        names.add("orange");
        names.add("guava");
        names.add("banana");
        names.add("apple");
        System.out.println(names);
    }
}

Output

[mango, orange, guava, banana, apple]

From the output, Array List arranges the insertion order and it takes the same. But not sorted.

2) Vector

It works similar to Array List.

  • Thread safety.
  • It also implements the Random Access.
  • Thread safety usually causes a performance hit.
  • Their methods are synchronized.

Example of Java Vector

import java.util.Vector;

public class Fruit {
    public static void main(String[] args) {
        Vector < String > names = new Vector < String > ();
        names.add("mango");
        names.add("orange");
        names.add("guava");
        names.add("banana");
        names.add("apple");
        System.out.println(names);
    }
}

Output

[mango, orange, guava, banana, apple]

Vector also maintains the insertion way and accepts the same.

3) Linked List

  • Performance is slow than the Array list.
  • Good choice for insertion and deletion.
  • Elements are doubly linked to one another.
  • In Java 5.0 it supports common queue methods peek( ), Pool ( ), Offer ( ) etc.

Example of Java LinkedList

import java.util.LinkedList;

public class Fruit  {
    public static void main(String[] args) {
        LinkedList < String > names = new LinkedList < String > ();
        names.add("mango");
        names.add("orange");
        names.add("guava");
        names.add("banana");
        names.add("apple");
        System.out.println(names);
    }
}

Output

[mango, orange, guava, banana, apple]

It maintains the insertion way and takes the duplicates.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.