Java LinkedList class has a lot of methods to perform different types of operation. In this post, we will discuss all the Java linked list methods with an example. Let’s see the Java linked list methods.
1. How to add elements to a LinkedList?
- add(E e): This method adds a node in LinkedList. It appends the given element e at the end of the LinkedList. You can read it with an example.
- add(int index, E e): This method adds a node in LinkedList at a specific position. It inserts the element e, at a particular index. You can read it with an example.
- addAll(Collection c): The addAll(Collection c) method adds all the elements in LinkedList. It appends the specified collection to the end of this LinkedList. You can read it with an example.
- addAll(int index, Collection c): This method adds all the elements of the specified collection at a specific index. You can read it with an example.
- addFirst(E e): This method adds the given element e at the beginning of LinkedList. You can read it with an example.
- addLast(E e): This method is used to add the given element at the end of the LinkedList. You can read it with an example.
- offer(E e): The offer() method also used to add the specified element at the end of the LinkedList. You can read it with an example.
- offerFirst(E e): This method is used to add the given element at the beginning of LinkedList. Its return type is boolean. You can read it with an example.
- offerLast(E e): This method is used to add the given element at the end of LinkedList. Its return type is boolean. You can read it with an example.
- push(): This method is used to add the given element at the beginning of the LinkedList. Its return type is void. You can read it with an example.
2. How to access elements from a LinkedList?
- remove(): This method is used removes and returns the first element from LinkedList. Its return type is E. You can read it with an example
- remove(Object o): This method is used to remove the first occurrence of the specified element from LinkedList. You can read it with an example
- remove(int index): This method is used to remove the element at the specified position from LinkedList. You can read it with an example
- removeFirst(): This method is used to remove and return the first element from LinkedList. You can read it with an example
- removeLast(): This method is used removes and returns the last element from LinkedList. You can read it with an example
- removeFirstOccurrence(): This method is used removes the first occurrence of the specified element from LinkedList. You can read it with an example
- removeLastOccurrence(): This method is used removes the last occurrence of the specified element from LinkedList. You can read it with an example
- poll(): This method is used to removes and return the first element from LinkedList. You can read it with an example
- pollFirst(): This method is also used to removes and return the first element from LinkedList. Its return type is E. You can read it with an example
- pollLast(): This method is used to removes and return the last element from LinkedList. Its return type is E. You can read it with an example
- pop(): This method is also used to removes and return the first element from LinkedList. Its return type is E. You can read it with an example
- removeAll(Collection c): The removeAll(Collection c) method is used to remove all elements of the given collection from LinkedList. It removes all elements from LinkedList whether it is duplicated in LinkedList or not. You can read it with an example
- removeIf(Predicate filter) method: The removeIf(Predicate filter) method is also used to remove the element from the LinkedList. It takes an argument of Predicate type. You can read it with an example
3. How to get element in ArrayList java?
- get(int index): This method is used to get the element at a specified index in LinkedList. You can read it with an example
- getFirst(): This method is used to get the first element from LinkedList. You can read it with an example
- getLast(): This method is used to get the last element from LinkedList. You can read it with an example
- peek(): This method is used to get to the first element from LinkedList. Its return type is E. You can read it with an example
- peekFirst(): This method is also used to get the first element from LinkedList. You can read it with an example
- peekLast(): This method is used to get to the last element from LinkedList. You can read it with an example
- element(): This method is used to get the first element from LinkedList. But doesn’t remove the element from list. Its return type is E. You can read it with an example
4. Searching in linked list
- index(object o): This method is used to get the index of the specified element. It returns the index value of the first occurrence of the element if LinkedList. You can read it with an example
- lastIndexOf(Object o): This method is used to get the last index of the specified element. It returns the index value of the last occurrence of the element of LinkedList. You can read it with an example.
- contains(Object o): It is used to check whether the given element present in LinkedList or not. Its return type is boolean. You can read it with an example
5. How to perform linked list sorting
- sort(Comparator<? super E> c): This method is used to sort the elements of the LinkedList based on a specified comparator. You can read it with an example
6. How to convert linked list to array in java
- Object[] toArray(): This method is used to return an array containing all of the elements of the LinkedList in the correct order. You can read it with an example
- T [] toArray(T [] O): This method is used to return an array containing all of the elements in the LinkedList in the correct order same as the previous method. You can read it with an example
7. Traverse a linked list java
- iterator(): It is used to return an iterator over the elements in this LinkedList. You can read it with an example
- Spliterator: It is used to create a late-binding and fail-fast Spliterator over the elements in the LinkedList. You can read it with an example
- listIterator(): It is used to return a list iterator over the elements in LinkedList. You can read it with an example
- listIterator(int index): It is used to return a list iterator over the elements in LinkedList starting at the specified position in the LinkedList. You can read it with an example
These all are the Java linked list methods. You can click on link and read each method with example.