In this post, we will learn important Java LinkedList methods with examples.
LinkedList Class Methods
The below class diagram shows a list of LinkedList class methods:add() Method
We can add new elements to the end of the LinkedList using add() method:
// Creating LinkedList of Strings
LinkedList<String> list = new LinkedList<String>();
// Using add() method to add elements in the list
list.add("Java");
list.add("Python");
list.add("JavaScript");
We can add an element at the specified position in the LinkedList using add(index, element) method:
// Adding an element at the specific position in the list
list.add(1, "C++");
addFirst() method
We can add an element at the beginning of the LinkedList using the addFirst() method:
// Adding elements at the beginning of the list using addFirst()
list.addFirst("Swift");
addLast() method
We can add an element at the end of the LinkedList using the addLast() method:
// Adding elements at the end of the list using addLast()
list.addLast("Kotlin");
addAll() Method:
This method is used to append all of the elements in the specified collection to the end of a list.
List<String> additionalLangs = Arrays.asList("C", "Golang");
list.addAll(additionalLangs);
System.out.println(list);
get(int index) method
The get(int index) method is used to return the element at the specified position in the list.
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<String>();
// Adding elements to the LinkedList
list.add("Java");
list.add("Python");
list.add("JavaScript");
list.add("C++");
list.add("Kotlin");
// Accessing elements from the LinkedList
// The get() method throws IndexOutOfBoundsException, if the specified index is out of range
System.out.println("Element returned by get(2): " + list.get(2));
getFirst() method
The getFirst() method is used to retrieve the first element from the LinkedList.
// The first() method returns the first element in this list
System.out.println("Element returned by first(): " + list.getFirst());
getLast() method
The getLast() method is used to retrieve the last element from the LinkedList, which is "Kotlin" in our case.
// The last() method returns the last element in this list
System.out.println("Element returned by last(): " + list.getLast());
remove(int index)
This method is used to remove an element from a specific position in a list.
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<String>();
// Adding elements to the LinkedList
list.add("Java");
list.add("Python");
list.add("JavaScript");
list.add("C++");
list.add("Kotlin");
// Displaying the LinkedList
System.out.println("Initial LinkedList: " + list);
// The remove(int index) method removes the element at the specified position in this list
list.remove(2);
System.out.println("LinkedList after removing element at index 2: " + list);
remove(Object o)
This method is used to remove the first occurrence of the specified element from a list.
// The remove(Object) method removes the first occurrence of the specified element from this list, if it is present
list.remove("Python");
System.out.println("LinkedList after removing 'Python': " + list);
removeFirst() Method
The removeFirst() method removes and returns the first element from this list. list.removeFirst();
System.out.println("LinkedList after removing first element: " + list);
removeLast() Method
The removeLast() method removes and returns the last element from this list.list.removeLast();
System.out.println("LinkedList after removing last element: " + list);
clear() Method
This method is used to remove all of the elements from a list.
list.clear();
System.out.println("LinkedList after calling clear(): " + list);
contains(Object o):
This method is used to check if a specific element is contained in a list.
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<String>();
// Adding elements to the LinkedList
list.add("Java");
list.add("Python");
list.add("JavaScript");
list.add("C++");
list.add("Python");
list.add("Kotlin");
// Displaying the LinkedList
System.out.println("LinkedList: " + list);
// The contains(Object) method returns true if this list contains the specified element
boolean found = list.contains("Python");
System.out.println("'Python' is in the list: " + found);
indexOf(Object) method
The indexOf(Object) method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. int firstIndex = list.indexOf("Python");
System.out.println("First occurrence of 'Python' is at index: " + firstIndex);
lastIndexOf(Object)
The lastIndexOf(Object) method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. int lastIndex = list.lastIndexOf("Python");
System.out.println("Last occurrence of 'Python' is at index: " + lastIndex);
size()
This method is used to return the number of elements in a list.
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<String>();
// Adding elements to the LinkedList
list.add("Java");
list.add("Python");
list.add("JavaScript");
list.add("C++");
list.add("Python");
list.add("Kotlin");
int size = list.size();
// Displaying the LinkedList size
System.out.println("LinkedList size: " + size);
iterator()
This method is used to return an iterator over the elements in a list in the proper sequence.
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<String>();
list.add("Java");
list.add("Python");
list.add("JavaScript");
list.add("C++");
// Using Iterator
System.out.println("Iterating using Iterator:");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
Conclusion
In this post, we learned all the important Java LinkedList methods with examples. You can learn everything about Java LinkedList here: Learn Everything About LinkedList
Comments
Post a Comment
Leave Comment