Home » Java programming language

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.
Submitted by Karan Ghai, on May 15, 2019

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:

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:

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:

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

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.