linked list in java code example
Example 1: java linked list functions
import java.util.LinkedList;
LinkedList myList = new LinkedList();
myList.add(0);
myList.remove(0);//Remove at index 0
myList.size();
myList.get(0);//Return element at index 0
Example 2: how to create a linked list in java
List list = new LinkedList<>();
Example 3: how to declare a linked list in java
LinkedList list=new LinkedList();
Example 4: LinkedHashSet in java
import java.util.LinkedHashSet;
public class LinkedHashsetExample
{
public static void main(String[] args)
{
LinkedHashSet lhs = new LinkedHashSet();
// add elements to LinkedHashSet
lhs.add("Anteater");
lhs.add("Bear");
lhs.add("Cheetah");
lhs.add("Deer");
// will not add new element as Anteater already exists
lhs.add("Anteater");
lhs.add("Elephant");
System.out.println("LinkedHashSet size: " + lhs.size());
System.out.println("Given LinkedHashSet: " + lhs);
System.out.println("Deer removed from LinkedHashSet: " + lhs.remove("Deer"));
System.out.println("Remove Zebra which is not present: " + lhs.remove("Zebra"));
System.out.println("Check if Anteater is present: " + lhs.contains("Anteater"));
System.out.println("New LinkedHashSet: " + lhs);
}
}
Example 5: linkedlist in java
LinkedList not synchronized, doubly linked
Example 6: linked list in java
// Java program to iterate the elements
// in an LinkedList
import java.util.*;
public class GFG {
public static void main(String args[])
{
LinkedList ll
= new LinkedList<>();
ll.add("Geeks");
ll.add("Geeks");
ll.add(1, "For");
// Using the Get method and the
// for loop
for (int i = 0; i < ll.size(); i++) {
System.out.print(ll.get(i) + " ");
}
System.out.println();
// Using the for each loop
for (String str : ll)
System.out.print(str + " ");
}
}