Java LinkedList Class: Features, Constructors, Methods, and Differences with ArrayList, Summaries of Java Programming

An introduction to the Java LinkedList class, its relationship to AbstractSequentialList and List/Deque interfaces, and its key features. The document also covers the constructors of LinkedList, methods for adding and removing elements, and the differences between LinkedList and ArrayList. It includes examples of reversing a LinkedList and a comparison of the two data structures.

Typology: Summaries

2019/2020

Uploaded on 08/17/2020

mohamad-sriheen
mohamad-sriheen 🇯🇴

1 document

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JAVA LINKEDLIST CLASS
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Java LinkedList Class: Features, Constructors, Methods, and Differences with ArrayList and more Summaries Java Programming in PDF only on Docsity!

JAVA LINKEDLIST CLASS

INTRODUCTION

 Java LinkedList class uses a doubly linked list

to store the elements. It provides a linked-list

data structure.

 As shown in the diagram, Java LinkedList class

extends AbstractSequentialList class and

implements List and Deque interfaces.

 The important points about Java LinkedList are:

 Java LinkedList class can contain duplicate

elements.

 In Java LinkedList class, manipulation is fast

because no shifting needs to occur.

 Java LinkedList class can be used as a list, stack

or queue.

CONSTRUCTORS OF JAVA LINKEDLIST

Constructor Description

LinkedList() It is used to construct an empty list.

e.g: LinkedList L=new LinkedList<>();

L.add("1");L.add("2");L.add("3");

System.out.println(L);//[1,2,3]

LinkedList(Collection<? extends E> c) It is used to construct a list containing the elements of the specified collection, in the order, they are returned by the collection's iterator.

e.g: LinkedList L=new LinkedList<>();

L.add("1");L.add("2");L.add("3");

LinkedList L1=new LinkedList<>(L);

System.out.println(L1); //[1,2,3]

METHODS OF JAVA LINKEDLIST

Method Description boolean add(E e) It is used to append the specified element to the end of a list. e.g: LinkedList L=new LinkedList<>(); L.add("1");L.add("2");L.add("3"); System.out.println(L);//[1,2,3] void add(int index, E element) It is used to insert the specified element at the specified position index in a list. e.g: LinkedList L=new LinkedList<>(); L.add(0, "1");L.add(0, "2");L.add(0, "3"); System.out.println(L);//[3, 2, 1] boolean addAll(Collection<? extends E> c) It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator. e.g: LinkedList L=new LinkedList<>(); L.add(0, "1");L.add(0, "2");L.add(0, "3"); LinkedList L1=new LinkedList<>(); L1.add(0, "4");L1.add(0, "5");L1.add(0, "6"); L1.addAll(L); System.out.println(L1);//[6, 5, 4, 3, 2, 1]

METHODS OF JAVA LINKEDLIST

int indexOf(Object o) It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element. int lastIndexOf(Object o) It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element. ListIterator listIterator(int index) It is used to return a list-iterator of the elements in proper sequence, starting at the specified position in the list. boolean offer(E e) It adds the specified element as the last element of a list. boolean offerFirst(E e) It inserts the specified element at the front of a list. boolean offerLast(E e) It inserts the specified element at the end of a list. E peek() It retrieves the first element of a list E peekFirst() It retrieves the first element of a list or returns null if a list is empty. E peekLast() It retrieves the last element of a list or returns null if a list is empty.

METHODS OF JAVA LINKEDLIST

E poll() It retrieves and removes the first element of a list. E pollFirst() It retrieves and removes the first element of a list, or returns null if a list is empty. E pollLast() It retrieves and removes the last element of a list, or returns null if a list is empty. E pop() It pops an element from the stack represented by a list. void push(E e) It pushes an element onto the stack represented by a list. E remove() It is used to retrieve and removes the first element of a list. E remove(int index) It is used to remove the element at the specified position in a list. boolean remove(Object o) It is used to remove the first occurrence of the specified element in a list. E removeFirst() It removes and returns the first element from a list.

JAVA LINKEDLIST EXAMPLE TO REVERSE A LIST OF ELEMENTS package linkedlistmanipulation; import java.util.*; public class LinkedListManipulation { public static void main(String[] args) { LinkedList L=new LinkedList<>(); L.add("1");L.add("2");L.add( "3"); Iterator T=L.descendingIterator(); while(T.hasNext()) System.out.print(T.next()+" "); System.out.println(); T=L.iterator(); while(T.hasNext()) System.out.print(T.next()+" "); System.out.println(); } } Output 3 2 1 1 2 3

DIFFERENCE BETWEEN ARRAYLIST AND LINKEDLIST ArrayList LinkedList ArrayList internally uses a dynamic array to store the elements. LinkedList internally uses a doubly linked list to store the elements. Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the bits are shifted in memory. Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. An ArrayList class can act as a list only because it implements List only. LinkedList class can act as a list and queue both because it implements List and Deque interfaces. ArrayList is better for storing and accessing data. LinkedList is better for manipulating data.