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.
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 illustrate the Java.util.LinkedList.set() method:
LinkedList:[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]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Java.util.LinkedList.get(), getFirst(), getLast() in Java
- Java.util.LinkedList.indexOf(), lastIndexof() in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- LinkedList removeFirst() Method in Java
- LinkedList add() Method in Java
- LinkedList getFirst() Method in Java
- LinkedList getLast() Method in Java
- LinkedList clear() Method in Java
- LinkedList addFirst() Method in Java
- LinkedList addLast() Method in Java
- LinkedList addAll() Method in Java
- LinkedList remove() Method in Java
- LinkedList removeLast() Method in Java
- LinkedList get() Method in Java
- LinkedList size() Method in Java
- LinkedList indexOf() method in Java
- LinkedList lastIndexOf() method in Java
- LinkedList contains() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.