LinkedList set() Method in Java Last Updated : 31 Mar, 2023 Comments Improve Suggest changes Like Article Like Report The Java.util.LinkedList.set() method is used to replace any particular element in the linked list created using the LinkedList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: LinkedList.set(int index, Object element)Parameters: This function accepts two parameters as shown in the above syntax and described below. index: This is of integer type and refers to the position of the element that is to be replaced from the linked list.element: It is the new element by which the existing element will be replaced and is of the same object type as the linked list.Return Value: The method returns the previous value from the linked list that is replaced with the new value. Below program illustrates the Java.util.LinkedList.set() method: Example 1: Java // Java code to illustrate set() Method // Importing required libraries import java.io.*; import java.util.LinkedList; // Class public class LinkedListDemo { // Main driver method public static void main(String args[]) { // Creating an empty LinkedList LinkedList<String> list = new LinkedList<String>(); // Use add() method to add elements in the list list.add("Geeks"); list.add("for"); list.add("Geeks"); list.add("10"); list.add("20"); // Displaying the linkedlist System.out.println("LinkedList:" + list); // Using set() method to replace Geeks with GFG System.out.println( "The Object that is replaced is: " + list.set(2, "GFG")); // Using set() method to replace 20 with 50 System.out.println( "The Object that is replaced is: " + list.set(4, "50")); // Displaying the modified linkedlist System.out.println("The new LinkedList is:" + list); } } OutputLinkedList:[Geeks, for, Geeks, 10, 20] The Object that is replaced is: Geeks The Object that is replaced is: 20 The new LinkedList is:[Geeks, for, GFG, 10, 50] The set(int index, E element) method in Java is used to set the element of a LinkedList at the specified index with a new element. This method replaces the existing element at the specified index with the new element. Example 2: Here is an example of how to use the set() method Java import java.util.LinkedList; public class LinkedListExample public static void main(String[] args) { // Create a LinkedList of Strings LinkedList<String> list = new LinkedList<String>(); list.add("apple"); list.add("banana"); list.add("cherry"); list.add("date"); list.add("elderberry"); // Replace an element in the LinkedList list.set(2, "grape"); // Print the updated LinkedList System.out.println("LinkedList after replacement: " + list); } } OutputLinkedList after replacement: [apple, banana, grape, date, elderberry] In this example, we first create a LinkedList of Strings named list with five elements. We then use the set() method to replace the element at index 2 with the String "grape". The index of the elements in a LinkedList starts from 0. Comment More infoAdvertise with us Next Article LinkedList remove() Method in Java chinmoy lenka Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-LinkedList +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads LinkedList in Java Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure, which is a linear data structure where the elements are not stored in contiguous locations, and every element is a separate object with a data part and an 12 min read LinkedList add() Method in Java In Java, the add() method of the LinkedList class is used to add an element to the list. By default, it adds the element to the end of the list, if the index is not specified.Example: Here, we use the add() method to add a single element to the list.Java// Java program to add elements in LinkedList 2 min read LinkedList set() Method in Java The Java.util.LinkedList.set() method is used to replace any particular element in the linked list created using the LinkedList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: Link 3 min read LinkedList remove() Method in Java In Java, the remove() method of the LinkedList class removes an element from the list, either by specifying its index or by providing its value.Example 1: Here, we use the remove() method to remove element from the LinkedList of Strings. By default the remove() will remove the beginning element(head 3 min read LinkedList get() Method in Java In Java, the get() method of LinkedList is used to fetch or retrieve an element at a specific index from a LinkedList.Example 1: Here, we use the get() method to retrieve an element at a specified index.Java// Java Program to illustrate get() method import java.util.LinkedList; public class Geeks { 2 min read LinkedList addAll() Method in Java In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an 3 min read LinkedList addFirst() Method in Java In Java, the addFirst() method of LinkedList, adds elements at the beginning of the list. All the existing elements moved one position to the right and the new element is placed at the beginning.Syntax of LinkedList addFirst() Method public void addFirst( E e)Parameters: E is the data type of elemen 1 min read LinkedList addLast() Method in Java In Java, the addLast() method of the LinkedList class is used to add an element at the end of the list.Syntax of LinkedList addLast() Method void addLast( E e)Parameter: e is the element you want to add at the end of the list.Return type: This method does not return any value.Example: Here, we use t 1 min read LinkedList clear() Method in Java In Java, the clear() is used to remove all the elements from a LinkedList. This method only clears all the element from the list and not deletes the list. After calling this method, the list will be empty.Syntax of LinkedList clear() Methodvoid clear()Parameters: This method does not accept any para 1 min read LinkedList clone() Method in Java In Java, the clone() method of LinkedList, creates a shallow copy which means the structure is duplicated but the objects inside the list are shared between the original and the copied list. So, the cloned list will have the same elements as the original list.Syntax of Java LinkedList clone() Method 1 min read Like