🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
LinkedList in Java is a doubly-linked list implementation of the List and Deque interfaces. It allows for efficient insertion, removal, and access operations, making it an excellent choice for applications requiring dynamic manipulation of elements. This blog post will provide an example of CRUD (Create, Read, Update, Delete) operations using a LinkedList in Java, showcasing its versatility and efficiency in handling a collection of objects.
2. Program Steps
1. Create a new LinkedList.
2. Add elements to the list (Create).
3. Read elements from the list.
4. Update an element in the list.
5. Remove an element from the list (Delete).
3. Code Program
import java.util.LinkedList;
public class LinkedListCRUDExample {
public static void main(String[] args) {
// Step 1: Creating a new LinkedList
LinkedList<String> fruits = new LinkedList<>();
// Step 2: Adding elements to the LinkedList (Create)
fruits.add("Apple");
fruits.add("Banana");
fruits.addFirst("Strawberry"); // Adds to the beginning
fruits.addLast("Mango"); // Adds to the end
System.out.println("After adding elements: " + fruits);
// Step 3: Reading elements from the LinkedList
String firstElement = fruits.getFirst();
String lastElement = fruits.getLast();
System.out.println("First element: " + firstElement);
System.out.println("Last element: " + lastElement);
// Step 4: Updating an element in the LinkedList
fruits.set(1, "Blueberry"); // Replaces the element at index 1
System.out.println("After updating element: " + fruits);
// Step 5: Removing an element from the LinkedList (Delete)
fruits.remove("Strawberry"); // Removes the first occurrence of "Strawberry"
fruits.removeFirst(); // Removes the first element
fruits.removeLast(); // Removes the last element
System.out.println("After removing elements: " + fruits);
}
}
Output:
After adding elements: [Strawberry, Apple, Banana, Mango] First element: Strawberry Last element: Mango After updating element: [Strawberry, Blueberry, Banana, Mango] After removing elements: [Blueberry]
Explanation:
1. The program starts by creating a new LinkedList named fruits that stores strings.
2. Adding elements: Elements are added using add(), addFirst(), and addLast() methods. These operations demonstrate the list's dynamic nature, allowing insertion at both ends and the middle.
3. Reading elements: The getFirst() and getLast() methods retrieve the first and last elements, respectively, showcasing the ease of accessing elements in a LinkedList.
4. Updating elements: The set() method updates the value of an element at a specified index, illustrating the list's ability to modify existing elements.
5. Removing elements: Elements are removed using remove(), removeFirst(), and removeLast() methods. This highlights the LinkedList's flexibility in removing elements from any position.
6. The output reflects the list's state after each operation, demonstrating how LinkedList supports efficient CRUD operations, making it suitable for scenarios requiring frequent insertions and deletions.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment