📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ 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.
Comments
Post a Comment
Leave Comment