📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
// 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");
// Adding an element at the specific position in the list
list.add(1, "C++");
addFirst() method
// Adding elements at the beginning of the list using addFirst()
list.addFirst("Swift");
addLast() method
// Adding elements at the end of the list using addLast()
list.addLast("Kotlin");
addAll() Method:
List<String> additionalLangs = Arrays.asList("C", "Golang");
list.addAll(additionalLangs);
System.out.println(list);
get(int index) method
// 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 first() method returns the first element in this list
System.out.println("Element returned by first(): " + list.getFirst());
getLast() method
// The last() method returns the last element in this list
System.out.println("Element returned by last(): " + list.getLast());
remove(int index)
// 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)
// 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
list.clear();
System.out.println("LinkedList after calling clear(): " + list);
contains(Object o):
// 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()
// 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()
// 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());
}
Comments
Post a Comment
Leave Comment