Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Java Linkedlist Methods in Java

Java Linkedlist Methods in Java

LinkedList Methods in Java

In Java, the LinkedList class is part of the java.util package and implements both the List and Deque interfaces. It provides a variety of methods to manipulate elements in the list. These methods can be categorized based on their functionalities like adding, removing, accessing, and modifying elements.

Here is a comprehensive list of commonly used LinkedList methods:


Methods for Adding Elements

  1. add(E e):

    • Adds the specified element to the end of the list.

    list.add("Apple");
  2. addFirst(E e):

    • Adds the specified element at the beginning of the list.

    list.addFirst("Banana");
  3. addLast(E e):

    • Adds the specified element at the end of the list (same as add).

    list.addLast("Cherry");
  4. offer(E e):

    • Adds the element to the end of the list, and returns true (it is equivalent to add in LinkedList).

    list.offer("Date");
  5. offerFirst(E e):

    • Adds the element to the front of the list.

    list.offerFirst("Elderberry");
  6. offerLast(E e):

    • Adds the element to the end of the list.

    list.offerLast("Fig");

Methods for Removing Elements

  1. remove():

    • Removes and returns the first element of the list. Throws NoSuchElementException if the list is empty.

    String removed = list.remove();
  2. removeFirst():

    • Removes and returns the first element of the list.

    String removedFirst = list.removeFirst();
  3. removeLast():

    • Removes and returns the last element of the list.

    String removedLast = list.removeLast();
  4. remove(int index):

    • Removes the element at the specified position in the list.

    list.remove(2);  // Removes element at index 2
  5. remove(Object o):

    • Removes the first occurrence of the specified element from the list.

    list.remove("Apple");  // Removes the first occurrence of "Apple"
  6. poll():

    • Removes and returns the first element of the list, or returns null if the list is empty.

    String polled = list.poll();
  7. pollFirst():

    • Removes and returns the first element of the list, or returns null if the list is empty.

    String polledFirst = list.pollFirst();
  8. pollLast():

    • Removes and returns the last element of the list, or returns null if the list is empty.

    String polledLast = list.pollLast();
  9. clear():

    • Removes all elements from the list.

    list.clear();

Methods for Accessing Elements

  1. get(int index):

    • Returns the element at the specified position in the list.

    String element = list.get(1);  // Gets the element at index 1
  2. getFirst():

    • Returns the first element of the list.

    String firstElement = list.getFirst();
  3. getLast():

    • Returns the last element of the list.

    String lastElement = list.getLast();
  4. peek():

    • Returns the first element of the list without removing it, or null if the list is empty.

    String peeked = list.peek();
  5. peekFirst():

    • Returns the first element of the list without removing it, or null if the list is empty.

    String peekedFirst = list.peekFirst();
  6. peekLast():

    • Returns the last element of the list without removing it, or null if the list is empty.

    String peekedLast = list.peekLast();

Methods for Modifying Elements

  1. set(int index, E element):

    • Replaces the element at the specified position with the specified element.

    list.set(2, "Grape");  // Sets "Grape" at index 2
  2. addAll(Collection<? extends E> c):

    • Appends all elements from the specified collection to the end of the list.

    list.addAll(anotherList);
  3. addAll(int index, Collection<? extends E> c):

    • Inserts all elements from the specified collection starting at the specified position.

    list.addAll(2, anotherList);
  4. removeAll(Collection<?> c):

    • Removes all elements from the list that are contained in the specified collection.

    list.removeAll(anotherList);
  5. retainAll(Collection<?> c):

    • Retains only the elements in the list that are contained in the specified collection.

    list.retainAll(anotherList);

Methods for List Information

  1. size():

    • Returns the number of elements in the list.

    int size = list.size();
  2. isEmpty():

    • Returns true if the list contains no elements, otherwise returns false.

    boolean empty = list.isEmpty();
  3. contains(Object o):

    • Returns true if the list contains the specified element.

    boolean contains = list.contains("Apple");
  4. indexOf(Object o):

    • Returns the index of the first occurrence of the specified element, or -1 if not found.

    int index = list.indexOf("Apple");
  5. lastIndexOf(Object o):

    • Returns the index of the last occurrence of the specified element, or -1 if not found.

    int lastIndex = list.lastIndexOf("Apple");

Methods for Iteration

  1. iterator():

    • Returns an iterator over the elements in the list.

    Iterator<String> iterator = list.iterator();while (iterator.hasNext()) {    System.out.println(iterator.next());}
  2. listIterator():

    • Returns a list iterator over the elements in the list.

    ListIterator<String> listIterator = list.listIterator();while (listIterator.hasNext()) {    System.out.println(listIterator.next());}
  3. forEach():

    • Performs the given action for each element of the list.

    list.forEach(System.out::println);

Example Usage of LinkedList Methods

import java.util.LinkedList;public class LinkedListMethodsExample {    public static void main(String[] args) {        LinkedList<String> list = new LinkedList<>();        // Adding elements        list.add("Apple");        list.addFirst("Banana");        list.addLast("Cherry");        // Accessing elements        System.out.println("First Element: " + list.getFirst());        System.out.println("Last Element: " + list.getLast());                // Modifying elements        list.set(1, "Blueberry");        // Removing elements        list.removeFirst();        list.removeLast();        // Iterating over the list        System.out.println("LinkedList Elements:");        list.forEach(System.out::println);        // Checking the size        System.out.println("Size of list: " + list.size());    }}

Output:

First Element: BananaLast Element: CherryLinkedList Elements:AppleBlueberrySize of list: 2

Conclusion:

The LinkedList class in Java provides a wide range of methods to manage, modify, and iterate over its elements. It is particularly useful for scenarios where frequent additions and deletions are required, but it is generally less efficient for random access operations. Understanding these methods allows you to effectively work with linked lists in Java.

Let me know if you'd like any further explanations or examples! ?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql