Java Collection List

In Java, List is an ordered collection that allows duplicate elements and provides indexed access. It is a part of the Java Collections Framework and extends the Collection interface.

Types of Sets in Java

Implementation

Internal Structure

Performance

Best Use Case

ArrayList

Dynamic Array

Fast for random accessO(1), Slow for insertions/deletionsO(n)

When searching is frequent

LinkedList

Doubly Linked List

Fast for insertions/deletionsO(1), Slow for random accessO(n)

When insertions/removals are frequent

Vector

Dynamic Array (Thread-safe)

Similar to ArrayList, but synchronized

When thread safety is required

Stack

LIFO (Last-In-First-Out)

Push & pop in O(1)

When stack operations are needed

ArrayList:

import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list); // Output: [Apple,Banana,Cherry]
}
}

Try it yourself

LinkedList:

import java.util.LinkedList;
import java.util.List;

public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list); // Output: [10,20,30]
}
}

Try it yourself

Vector:

vector is child of List Interface so In Vector we can use all List interface fuction with synchronized thread safe(multi thread)

import java.util.Vector;

public class Main {
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("A");
vector.add("B");
vector.add("C");
System.out.println(vector); // Output: [A,B,C]
}
}

Try it yourself

Stack:

Stackis child of Vector Interface and List interface so In Stack we can use all List interface fuction with some extra function like (push,pop,peek)

import java.util.Stack;

public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.add(100);
stack.add(200);
stack.add(300);
System.out.println(stack); // Output: [100,200,300]
}
}

Try it yourself


List Methods:

Commonly Used Methods of List Interface

Method

Description

add(Object o) or addLast(Object o)

Adds an element to the list 

addFirst(Object o)

Adds an element to the list at first index

getFirst() 

Returns the element at the first index

getLast()

Returns the element at the last index 

get(int index)

Returns the element at the specified index

set(int index, Object o)

Replaces the element at the specified index

removeFirst()

Removes the element at the first index

removeLast()

Removes the element at the last index

remove(int index)

Removes the element at the specified index

size()

Returns the number of elements in the list

contains(Object o)

Checks if the list contains the specified element

indexOf(Object o)

Returns the first index of the element

isEmpty()

Checks if the list is empty

subList(int from, int to)

Returns a portion of the list

reversed()

Returns a reverse of the list

copy()

Returns a copied list

sort()

Sort the list

Stack Methods:

All List Interface methods are working in Stack Interface apart from Stack have some extra methods

Method

Description

push(Object o)

Adds an element to the list can use (add(Object o))

pop()

Removes the element at the last index can use (removeLast())

peek()

Returns the element at the last index can use(getLast())

empty()

Checks if the list is empty can use(isEmpty())

search(Object o)

Returns the first index of the element can use(indexOf(Object o))


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.