In this article, we will discuss simple example on LinkedList specific methods i.e.; what are the various methods available in LinkedList class and how it can be operated on LinkedList objects
1. LinkedList specific method and its description:
LinkedList methods | Description |
void addFirst(Object obj); | add/inserts the specified element/object at the beginning of the invoking list |
void addLast(Object obj); | add/inserts the specified element/object at the end of the invoking list |
Object getFirst(); | returns first element/object from invoking list
throws NoSuchElementException; if list is empty |
Object getLast(); | returns last element/object from invoking list
throws NoSuchElementException; if list is empty |
Object removeFirst(); | removes & returns first element/object from invoking list
throws NoSuchElementException; if list is empty |
Object removeLast(); | removes & returns last element/object from invoking list
throws NoSuchElementException; if list is empty |
2. Example on LinkedList operations with all its methods :
LinkedListSpecificMethods.java
package in.bench.resources.java.collections;
import java.util.LinkedList;
public class LinkedListSpecificMethods {
public static void main(String[] args) {
// creating LinkedList object of type String
LinkedList<String> ll = new LinkedList<String>();
// adding elements to LinkedList object
ll.add("Sun");
ll.add("Apple");
ll.add("JBoss");
ll.add("Whatsup");
ll.add("Android");
ll.add("BEA Weblogic");
ll.add("Apache");
// Iterating using enhanced for-loop
System.out.println("LinkedList contents "
+ "as per Insertion Order:\n");
for(String str : ll) {
System.out.println(str);
}
// LinkedList specific method examples
// getting 1st and last elements of LinkedList
String strFirst = ll.getFirst();
String strLast = ll.getLast();
System.out.println("\n\nFirst element of LinkedList : "
+ strFirst);
System.out.println("Last element of LinkedList : "
+ strLast);
// adding First and Last elements to LinkedList
ll.addFirst("Instagram");
ll.addLast("Pinterest");
// Iterating using enhanced for-loop
System.out.println("\n\nIterating LinkedList "
+ "after adding First & Last elements:\n");
for(String str : ll) {
System.out.println(str);
}
// removing First and Last elements of LinkedList
String strRemoveFirst = ll.removeFirst();
String strRemoveLast = ll.removeLast();
System.out.println("\n\nFirst element removed is : "
+ strRemoveFirst);
System.out.println("Last element removed is : "
+ strRemoveLast);
// Iterating using enhanced for-loop
System.out.println("\n\nIterating LinkedList "
+ "after removing First & Last elements:\n");
for(String str : ll) {
System.out.println(str);
}
}
}
Output:
LinkedList contents as per Insertion Order:
Sun
Apple
JBoss
Whatsup
Android
BEA Weblogic
Apache
First element of LinkedList : Sun
Last element of LinkedList : Apache
Iterating LinkedList after adding First & Last elements:
Instagram
Sun
Apple
JBoss
Whatsup
Android
BEA Weblogic
Apache
Pinterest
First element removed is : Instagram
Last element removed is : Pinterest
Iterating LinkedList after removing First & Last elements:
Sun
Apple
JBoss
Whatsup
Android
BEA Weblogic
Apache
Related Articles:
- Java – Iterating LinkedList in reverse order
- Java – LinkedList specific method examples
- Java – Push and pop operations with LinkedList
- Java – Collections class a utility class for Collection
- Java – Sorting ArrayList using Comparable and Comparator
- Java – Searching element from ArrayList using Binary Search Algorithm
- Java – How to Reverse order of elements in ArrayList ?
- Java – How to Reverse order of Comparator ?
- Java – How to count duplicate elements of ArrayList ?
- Java – How to swap elements of ArrayList ?
- Java – How to copy elements of one ArrayList to another List ?
- Java – How to shuffle elements of ArrayList ?
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Enumeration.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html
- https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
Happy Coding !!
Happy Learning !!