A LinkedList in Java is another type of data structure that, like an ArrayList, allows you to store and manage a collection of elements. However, unlike an ArrayList, a LinkedList is implemented as a doubly linked list, where each element (or node) in the list contains a reference to the next and previous elements. This structure provides flexibility in inserting and deleting elements but may be less efficient in random access compared to an ArrayList.
import java.util.LinkedList;
// Syntax for creating a LinkedList
LinkedList<Type> listName = new LinkedList<>();
Code language: JavaScript (javascript)
Adding Elements
- add(element): Appends an element to the end of the list.
- addFirst(element): Inserts an element at the beginning of the list.
- addLast(element): Appends an element to the end of the list.
Accessing Elements
- get(index): Retrieves the element at the specified index.
- getFirst(): Retrieves the first element of the list.
- getLast(): Retrieves the last element of the list.
Removing Elements
- remove(): Removes and returns the first element of the list.
- remove(index): Removes the element at the specified index.
- removeFirst(): Removes and returns the first element of the list.
- removeLast(): Removes and returns the last element of the list.
Size and Capacity
- size(): Returns the number of elements in the list.
- isEmpty(): Checks if the list is empty.
Other Operations
- contains(element): Checks if the list contains a specific element.
- clear(): Removes all elements from the list.
- toArray(): Converts the list to an array.
import java.util.LinkedList;
public class StudentLinkedListExample {
public static void main(String[] args) {
// Creating a LinkedList to store student names
LinkedList<String> studentsList = new LinkedList<>();
// Adding students to the LinkedList
studentsList.add("Paani");
studentsList.add("Mahesh");
studentsList.add("Datta");
studentsList.add("Ganesh");
studentsList.add("Harsha");
// Displaying the names of students
System.out.println("List of students:");
for (String student : studentsList) {
System.out.println(student);
}
// Adding a new student at the beginning
studentsList.addFirst("NewStudent");
// Displaying the updated list
System.out.println("\nUpdated list of students:");
for (String student : studentsList) {
System.out.println(student);
}
// Removing the first student
String removedStudent = studentsList.removeFirst();
System.out.println("\nRemoved student: " + removedStudent);
// Displaying the final list
System.out.println("\nFinal list of students:");
for (String student : studentsList) {
System.out.println(student);
}
}
}
D:\>javac HashtableExample.java
D:\>java HashtableExample
Hashtable of students:
{Ganesh=104, Paani=101, Mahesh=102, Harsha=105, Datta=103}
Removed student 'Datta' with roll number: 103
Updated Hashtable of students:
{Ganesh=104, Paani=101, Mahesh=102, Harsha=105}
Does the Hashtable contain student 'Harsha'? true
Iterating over Hashtable entries using enumeration:
Student: Ganesh, Roll Number: 104
Student: Paani, Roll Number: 101
Student: Mahesh, Roll Number: 102
Student: Harsha, Roll Number: 105
Size of the Hashtable: 4
Is the Hashtable empty now? trueCode language: JavaScript (javascript)