LinkedList in java
LinkedList is a collection of similar data type elements. The size of LinkedList is Dynamic. LinkedList class uses doubly linked list concept.
The ArrayList and LinkedList methods are same. But the main reason for creating LinkedList is , if you add or remove a data between the ArrayList , it's take more time but LinkedList take less time.
Add
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> num = new LinkedList<Integer>(); num.add(1); num.add(2); num.add(3); System.out.println(num); } }
Output
[1, 2, 3]
Remove
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> num = new LinkedList<Integer>(); num.add(1); num.add(2); num.add(3); num.remove(0); System.out.println(num); } }
Output
[2, 3]
Size
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> num = new LinkedList<Integer>(); num.add(1); num.add(2); num.add(3); System.out.println(num.size()); } }
Output
3
Clear
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> num = new LinkedList<Integer>(); num.add(1); num.add(2); num.add(3); num.clear(); System.out.println(num); } }
Output
[]
Get
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> num = new LinkedList<Integer>(); num.add(1); num.add(2); num.add(3); System.out.println(num.get(1)); } }
Output
2
Set
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> num = new LinkedList<Integer>(); num.add(1); num.add(2); num.add(3); num.set(1,99); System.out.println(num); } }
Output
[1, 99, 3]
print each element:
Method-1
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> num = new LinkedList<Integer>(); num.add(1); num.add(2); num.add(3); for (int i=0;i<num.size();i++) { System.out.println(num.get(i)); } } }
Output
1 2 3
Method-2
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<Integer> num = new LinkedList<Integer>(); num.add(1); num.add(2); num.add(3); for (int i:num) { System.out.println(i); } } }
Output
1 2 3
String:
Add
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList<String> name = new LinkedList<String>(); name.add("jack"); name.add("stark"); name.add("steve"); System.out.println(name); } }
Output
[jack, stark, steve]
Extra Method
Method | Explain |
---|---|
addFirst() | This method is used to add the element in the first index of the list. |
addLast() | This method is used to add the element in the last index of the list. |
removeFirst() | This method is used to remove the first index element in the list. |
removeLast() | This method is used to remove the last index element in the list. |
getFirst() | This method is used to get the first index element in the list. |
getLast() | This method is used to get the last index element in the list |