LinkedList Class In Java: A LinkedList has a group of elements in the form of nodes. Each node has three fields where the data field contains the data, and the link fields reference the previous and next nodes.
The Java LinkedList class implements the list interface.
LinkedList is very convenient to store the data. Inserting the elements into the LinkedList and removing them from the LinkedList is done quickly; that’s why it is preferred over the arrays.
LinkedList also has some disadvantages, like the nodes can not be directly like in an array; we are accessing an element by using its index. Here, we need to start from the head and follow through the link to reach a node we can access.
To store the elements in a linked list, we use a double-linked list, which provides a linear data structure and is also used to inherit an abstract class and implement list and deque interfaces.
Important Methods Of LinkedList
- It can contain duplicate elements.
- The insertion is preserved.
- Java LinkedList class is non-synchronized.
- Null Insertion is possible.
- Duplicate and Heterogeneous elements are allowed.
- LinkedList implements the Serializable and Cloneable interfaces but not the random access interface.
- LinkedList is the best choice if your frequent operation is insertion and deletion in the middle, and if your frequent operation is retrieval, then don’t use LinkedList; that time, you can use ArrayList.
- The underlying data structure for LinkedList is a double-linked list.
Constructors for Java LinkedList
The LinkedList class also has various methods and constructors like other Java collections. The LinkedList class has two constructors that are:
- LinkedList(): Used to create an empty linked list.
- LinkedList(Collection C): Used to create an ordered list that contains all the elements of a specified collection, as returned by the collection’s iterator.
LinkedList Class Methods
How do you verify Element Presence LinkedList Java with an example?
package com.java.Softwaretestingblog; import java.util.LinkedList; import java.util.List; public class VerifyLinkedListElement { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in LinkedList<String> arrl = new LinkedList<String>(); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); List<String> list = new LinkedList<String>(); list.add("Second"); list.add("Random"); System.out.println("Does LinkedList contains all list elements?: "+arrl.containsAll(list)); list.add("one"); System.out.println("Does LinkedList contains all list elements?: "+arrl.containsAll(list)); } }
Output:
Does LinkedList contains all list elements?: true Does LinkedList contains all list elements?: false
How To Get All Values LinkedList HasNext() In Java With Example?
package com.java.Softwaretestingblog; import java.util.LinkedList; public class LastElementLinkedList { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in LinkedList<String> arrl = new LinkedList<String>(); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println("Last Element: "+arrl.getLast()); System.out.println("Last Element: "+arrl.peekLast()); } }
Output:
Last Element: Random Last Element: Random
Write a program to get the first element of LinkedList Java with an example.
package com.java.Softwaretestingblog; import java.util.LinkedList; public class GetFirstElementLinkedListJava { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in LinkedList<String> arrl = new LinkedList<String>(); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println("First Element: "+arrl.element()); System.out.println("First Element: "+arrl.getFirst()); System.out.println("First Element: "+arrl.peek()); System.out.println("First Element: "+arrl.peekFirst()); } }
Output:
First Element: First First Element: First First Element: First First Element: First
Write a Program to Clear LinkedList Values In Java With Example?
package com.java.Softwaretestingblog; import java.util.LinkedList; public class ClearMyLinkedList { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in LinkedList<String> arrl = new LinkedList<String>(); //adding elements to the end arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println("Actual LinkedList:"+arrl); arrl.clear(); System.out.println("After clear LinkedList:"+arrl); } }
Output:
Actual LinkedList:[First, Second, Third, Random] After clear LinkedList:[]
How do you reverse LinkedList value using collections in Java with examples?
package com.java.Softwaretestingblog; import java.util.Collections; import java.util.LinkedList; public class Reverse_Value_LinkedList { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in LinkedList<String> list = new LinkedList<String>(); list.add("Java"); list.add("Cric"); list.add("Play"); list.add("Watch"); list.add("Glass"); Collections.reverse(list); System.out.println("Results after reverse operation:"); for(String str: list) { System.out.println(str); } } }
Output:
Results before reverse operation: Java Cric Play Watch Glass Results after reverse operation: Glass Watch Play Cric Java
Please write comments if you find anything incorrect or if you want to share more information about the above-mentioned topic.
Different Ways to Remove Elements LinkedList In Java With Example?
package com.softwaretestingo.java.basics; import java.util.LinkedList; public class RemoveFromLinkedList { public static void main(String[] args) { LinkedList<String> arrl = new LinkedList<String>(); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); arrl.add("four"); arrl.add("five"); arrl.add("six"); arrl.add("seven"); arrl.add("eight"); arrl.add("nine"); System.out.println(arrl); System.out.println("Remov() method:"+arrl.remove()); System.out.println("After remove() method call:"); System.out.println(arrl); System.out.println("remove(index) method:"+arrl.remove(2)); System.out.println("After remove(index) method call:"); System.out.println(arrl); System.out.println("Remov(object) method:"+arrl.remove("six")); System.out.println("After remove(object) method call:"); System.out.println(arrl); System.out.println("removeFirst() method:"+arrl.removeFirst()); System.out.println("After removeFirst() method call:"); System.out.println(arrl); System.out.println("removeFirstOccurrence() method:" +arrl.removeFirstOccurrence("eight")); System.out.println("After removeFirstOccurrence() method call:"); System.out.println(arrl); System.out.println("removeLast() method:"+arrl.removeLast()); System.out.println("After removeLast() method call:"); System.out.println(arrl); System.out.println("removeLastOccurrence() method:"+ arrl.removeLastOccurrence("five")); System.out.println("After removeLastOccurrence() method call:"); System.out.println(arrl); } }
Output:
[First, Second, Third, Random, four, five, six, seven, eight, nine] Remov() method:First After remove() method call: [Second, Third, Random, four, five, six, seven, eight, nine] remove(index) method:Random After remove(index) method call: [Second, Third, four, five, six, seven, eight, nine] Remov(object) method:true After remove(object) method call: [Second, Third, four, five, seven, eight, nine] removeFirst() method:Second After removeFirst() method call: [Third, four, five, seven, eight, nine] removeFirstOccurrence() method:true After removeFirstOccurrence() method call: [Third, four, five, seven, nine] removeLast() method:nine After removeLast() method call: [Third, four, five, seven] removeLastOccurrence() method:true After removeLastOccurrence() method call: [Third, four, seven]
Please comment if you find anything incorrect or want to share more information about the topic discussed above.
How do you add values to the last LinkedList in Java with an example?
package com.java.Softwaretestingblog; import java.util.LinkedList; public class Add_Values_Last_LinkedList { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in LinkedList<String> arrl = new LinkedList<String>(); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.addLast("I am last"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.offerLast("I am last - 1"); System.out.println(arrl); System.out.println("Adding element at last position..."); arrl.offer("I am last - 2"); System.out.println(arrl); } }
Output:
[First, Second, Third, Random] Adding element at last position... [First, Second, Third, Random, I am last] Adding element at last position... [First, Second, Third, Random, I am last, I am last - 1] Adding element at last position... [First, Second, Third, Random, I am last, I am last - 1, I am last - 2]
Please comment if you find anything incorrect or want to share more information about the topic discussed above.
Different Basic Methods LinkedList In Java With Example?
package com.softwaretestingo.java.basics; import java.util.LinkedList; public class LinkedListOperations { public static void main(String[] args) { LinkedList<String> ll = new LinkedList<String>(); ll.add("Orange"); ll.add("Apple"); ll.add("Grape"); ll.add("Banana"); System.out.println(ll); System.out.println("Size of the linked list: "+ll.size()); System.out.println("Is LinkedList empty? "+ll.isEmpty()); System.out.println("Does LinkedList contains 'Grape'? "+ll.contains("Grape")); } }
Output:
[Orange, Apple, Grape, Banana] Size of the linked list: 4 Is LinkedList empty? false Does LinkedList contains 'Grape'? true
Please write comments if you find anything incorrect or if you want to share more information about the above-mentioned topic.
First LinkedList Value Using Java: Write a Program to Add Values First LinkedList In Java?
package com.java.Softwaretestingblog; import java.util.LinkedList; public class Add_Values_First_LinkedList { public static void main(String[] args) { // TODO Auto-generated method stub LinkedList<String> arrl = new LinkedList<String>(); arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println(arrl); System.out.println("Adding element at first position..."); arrl.addFirst("I am first"); System.out.println(arrl); System.out.println("Adding element at first position..."); arrl.offerFirst("I am first - 2"); System.out.println(arrl); } }
Output:
[First, Second, Third, Random] Adding element at first position... [I am first, First, Second, Third, Random] Adding element at first position... [I am first - 2, I am first, First, Second, Third, Random]
Please write comments if you find anything incorrect or if you want to share more information about the above-mentioned topic.
Another LinkedList Java Program: How do you copy values from one list to another LinkedList Java with an example?
package com.java.Softwaretestingblog; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Copyvalues2LinkedlistinJava { public static void main(String[] args) { // TODO Auto-generated method stub //www.softwaretestingblog.in LinkedList<String> arrl = new LinkedList<String>(); //adding elements to the end arrl.add("First"); arrl.add("Second"); arrl.add("Third"); arrl.add("Random"); System.out.println("Actual LinkedList:"+arrl); List<String> list = new ArrayList<String>(); list.add("one"); list.add("two"); arrl.addAll(list); System.out.println("After Copy: "+arrl); } }
Output:
Actual LinkedList:[First, Second, Third, Random] After Copy: [First, Second, Third, Random, one, two]
Please write comments if you find anything incorrect or if you want to share more information about the above-mentioned topic.