en
Feedback
Tech Jargon - Decoded

Tech Jargon - Decoded

Open in Telegram

Confused by tech terms? Don’t worry, we’ve got you 🀝 We make things simple, one concept at a time. Learn daily Easy & clear Turn Confusion into clarity. #tech #it #softwareengineer #cs #development

Show more
2 018
Subscribers
No data24 hours
-77 days
-4030 days
Posts Archive
πŸ’» Set Iteration Methods
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class SetIterationMethods {

    public static void main(String[] args) {
        Set<String> mySet = new HashSet<>();
        mySet.add("Apple");
        mySet.add("Banana");
        mySet.add("Orange");

        System.out.println("Iterating using enhanced for loop:");
        for (String fruit : mySet) {
            System.out.println(fruit);
        }

        System.out.println("nIterating using iterator:");
        Iterator<String> iterator = mySet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
πŸ“€ Output:
Iterating using enhanced for loop:
Orange
Banana
Apple

Iterating using iterator:
Orange
Banana
Apple

πŸ’» NavigableSet Operations
import java.util.NavigableSet;
import java.util.TreeSet;

public class NavigableSetOperations {

    public static void main(String[] args) {
        NavigableSet<Integer> mySet = new TreeSet<>();

        mySet.add(10);
        mySet.add(20);
        mySet.add(30);
        mySet.add(40);
        mySet.add(50);

        System.out.println("Original Set: " + mySet);

        // lower() method: Returns the greatest element strictly less than the given element.
        Integer lowerValue = mySet.lower(30);
        System.out.println("Lower of 30: " + lowerValue);

        // floor() method: Returns the greatest element less than or equal to the given element.
        Integer floorValue = mySet.floor(30);
        System.out.println("Floor of 30: " + floorValue);

        // ceiling() method: Returns the least element greater than or equal to the given element.
        Integer ceilingValue = mySet.ceiling(30);
        System.out.println("Ceiling of 30: " + ceilingValue);

        // higher() method: Returns the least element strictly greater than the given element.
        Integer higherValue = mySet.higher(30);
        System.out.println("Higher of 30: " + higherValue);

        // first() method: Returns the first (lowest) element currently in this set.
        Integer firstValue = mySet.first();
        System.out.println("First element: " + firstValue);

        // last() method: Returns the last (highest) element currently in this set.
        Integer lastValue = mySet.last();
        System.out.println("Last element: " + lastValue);

        // pollFirst() method: Retrieves and removes the first (lowest) element, or returns null if this set is empty.
        Integer polledFirst = mySet.pollFirst();
        System.out.println("Polled First: " + polledFirst);
        System.out.println("Set after pollFirst(): " + mySet);

        // pollLast() method: Retrieves and removes the last (highest) element, or returns null if this set is empty.
        Integer polledLast = mySet.pollLast();
        System.out.println("Polled Last: " + polledLast);
        System.out.println("Set after pollLast(): " + mySet);
    }
}
πŸ“€ Output:
Original Set: [10, 20, 30, 40, 50]
Lower of 30: 20
Floor of 30: 30
Ceiling of 30: 30
Higher of 30: 40
First element: 10
Last element: 50
Polled First: 10
Set after pollFirst(): [20, 30, 40, 50]
Polled Last: 50
Set after pollLast(): [20, 30, 40]

πŸ’» SortedSet Operations
import java.util.SortedSet;
import java.util.TreeSet;

public class SortedSetOperations {

    public static void main(String[] args) {

        // Creating a SortedSet using TreeSet (implementation of SortedSet)
        SortedSet<Integer> sortedSet = new TreeSet<>();

        // Adding elements to the SortedSet
        sortedSet.add(5);
        sortedSet.add(1);
        sortedSet.add(3);
        sortedSet.add(2);
        sortedSet.add(4);

        System.out.println("SortedSet elements: " + sortedSet); // Elements will be sorted automatically

        // Getting the first element
        int firstElement = sortedSet.first();
        System.out.println("First element: " + firstElement);

        // Getting the last element
        int lastElement = sortedSet.last();
        System.out.println("Last element: " + lastElement);

        // Getting a subset with elements less than 4
        SortedSet<Integer> headSet = sortedSet.headSet(4); // Excludes 4
        System.out.println("HeadSet (elements < 4): " + headSet);

        // Getting a subset with elements greater than or equal to 2
        SortedSet<Integer> tailSet = sortedSet.tailSet(2); // Includes 2
        System.out.println("TailSet (elements >= 2): " + tailSet);

        // Getting a subset with elements between 2 (inclusive) and 4 (exclusive)
        SortedSet<Integer> subSet = sortedSet.subSet(2, 4); // Includes 2, excludes 4
        System.out.println("SubSet (elements >= 2 and < 4): " + subSet);

    }
}
πŸ“€ Output:
SortedSet elements: [1, 2, 3, 4, 5]
First element: 1
Last element: 5
HeadSet (elements < 4): [1, 2, 3]
TailSet (elements >= 2): [2, 3, 4, 5]
SubSet (elements >= 2 and < 4): [2, 3]

πŸ’» Remove Duplicates using Set
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class RemoveDuplicatesUsingSet {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string: ");
        String inputString = scanner.nextLine();

        // Split the string into words
        String[] words = inputString.split(" ");

        // Use a Set to store unique words
        Set<String> uniqueWords = new HashSet<>();

        // Add words to the Set. Duplicates will be automatically ignored.
        for (String word : words) {
            uniqueWords.add(word);
        }

        // Print the unique words
        System.out.println("Unique words:");
        for (String word : uniqueWords) {
            System.out.print(word + " ");
        }
        System.out.println(); // Add newline for better formatting

        scanner.close();
    }
}
πŸ“€ Output:
Input: the quick brown fox jumps over the lazy fox
Output:
Enter a string: Unique words:
quick the lazy brown fox jumps over

πŸ’» Set Operations (Union, Intersection, Difference)
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class SetOperations {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Get input for the first set
        System.out.println("Enter elements for the first set (separated by spaces):");
        String input1 = scanner.nextLine();
        String[] elements1 = input1.split(" ");

        Set<String> set1 = new HashSet<>();
        for (String element : elements1) {
            set1.add(element);
        }

        // Get input for the second set
        System.out.println("Enter elements for the second set (separated by spaces):");
        String input2 = scanner.nextLine();
        String[] elements2 = input2.split(" ");

        Set<String> set2 = new HashSet<>();
        for (String element : elements2) {
            set2.add(element);
        }

        // Perform union
        Set<String> unionSet = new HashSet<>(set1);
        unionSet.addAll(set2);
        System.out.println("Union: " + unionSet);

        // Perform intersection
        Set<String> intersectionSet = new HashSet<>(set1);
        intersectionSet.retainAll(set2);
        System.out.println("Intersection: " + intersectionSet);

        // Perform difference (set1 - set2)
        Set<String> differenceSet = new HashSet<>(set1);
        differenceSet.removeAll(set2);
        System.out.println("Difference (Set1 - Set2): " + differenceSet);

        scanner.close();
    }
}
πŸ“€ Output:
Input: 1 2 3 4 5
Input: 3 5 6 7 8
Output: Enter elements for the first set (separated by spaces):
Output: Enter elements for the second set (separated by spaces):
Output: Union: [1, 2, 3, 4, 5, 6, 7, 8]
Output: Intersection: [3, 5]
Output: Difference (Set1 - Set2): [1, 2, 4]

πŸ’» TreeSet Operations
import java.util.Scanner;
import java.util.TreeSet;

public class TreeSetOperations {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        TreeSet<Integer> myTreeSet = new TreeSet<>();

        System.out.println("Enter the number of elements you want to add to the TreeSet:");
        int numElements = scanner.nextInt();

        System.out.println("Enter the elements:");
        for (int i = 0; i < numElements; i++) {
            int element = scanner.nextInt();
            myTreeSet.add(element);
        }

        System.out.println("TreeSet elements: " + myTreeSet);

        System.out.println("Enter an element to check if it exists in the TreeSet:");
        int searchElement = scanner.nextInt();
        if (myTreeSet.contains(searchElement)) {
            System.out.println(searchElement + " exists in the TreeSet.");
        } else {
            System.out.println(searchElement + " does not exist in the TreeSet.");
        }

        System.out.println("First element in the TreeSet: " + myTreeSet.first());
        System.out.println("Last element in the TreeSet: " + myTreeSet.last());

        System.out.println("Enter an element to find the next greater element:");
        int higherElementInput = scanner.nextInt();
        Integer higherElement = myTreeSet.higher(higherElementInput); // Finds the least element greater than the given element
        if (higherElement != null) {
            System.out.println("Next greater element than " + higherElementInput + " is: " + higherElement);
        } else {
            System.out.println("No greater element found for " + higherElementInput);
        }

        scanner.close();
    }
}
πŸ“€ Output:
Input: 5
Input: 5
Input: 2
Input: 8
Input: 1
Input: 9
Output: Enter the number of elements you want to add to the TreeSet:
Output: Enter the elements:
Output: TreeSet elements: [1, 2, 5, 8, 9]
Input: 5
Output: Enter an element to check if it exists in the TreeSet:
Output: 5 exists in the TreeSet.
Output: First element in the TreeSet: 1
Output: Last element in the TreeSet: 9
Input: 6
Output: Enter an element to find the next greater element:
Output: Next greater element than 6 is: 8

πŸ’» LinkedHashSet Operations
import java.util.LinkedHashSet;
import java.util.Scanner;

public class LinkedHashSetOperations {

    public static void main(String[] args) {
        LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>();
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the number of elements you want to add:");
        int numElements = scanner.nextInt();
        scanner.nextLine();

        System.out.println("Enter the elements to add to the LinkedHashSet:");
        for (int i = 0; i < numElements; i++) {
            String element = scanner.nextLine();
            linkedHashSet.add(element);
        }

        System.out.println("LinkedHashSet elements: " + linkedHashSet);

        System.out.println("Enter an element to check if it exists:");
        String elementToCheck = scanner.nextLine();
        boolean containsElement = linkedHashSet.contains(elementToCheck);
        System.out.println("LinkedHashSet contains " + elementToCheck + ": " + containsElement);

        System.out.println("Enter an element to remove:");
        String elementToRemove = scanner.nextLine();
        boolean removed = linkedHashSet.remove(elementToRemove);
        System.out.println("Element removed: " + removed);

        System.out.println("Updated LinkedHashSet: " + linkedHashSet);

        System.out.println("Size of LinkedHashSet: " + linkedHashSet.size());

        scanner.close();
    }
}
πŸ“€ Output:
Enter the number of elements you want to add:
Input: 3
Enter the elements to add to the LinkedHashSet:
Input: apple
Input: banana
Input: cherry
Output: LinkedHashSet elements: [apple, banana, cherry]
Enter an element to check if it exists:
Input: banana
Output: LinkedHashSet contains banana: true
Enter an element to remove:
Input: apple
Output: Element removed: true
Output: Updated LinkedHashSet: [banana, cherry]
Output: Size of LinkedHashSet: 2

πŸ’» HashSet Operations
import java.util.HashSet;
import java.util.Scanner;

public class HashSetOperations {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        HashSet<String> mySet = new HashSet<>();

        System.out.println("Enter the number of elements to add to the set:");
        int numberOfElements = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        System.out.println("Enter the elements:");
        for (int i = 0; i < numberOfElements; i++) {
            String element = scanner.nextLine();
            mySet.add(element);
        }

        System.out.println("The set is: " + mySet);

        System.out.println("Enter an element to check if it exists in the set:");
        String elementToCheck = scanner.nextLine();
        boolean exists = mySet.contains(elementToCheck);
        System.out.println("Does the set contain " + elementToCheck + "? " + exists);

        System.out.println("Enter an element to remove from the set:");
        String elementToRemove = scanner.nextLine();
        mySet.remove(elementToRemove);
        System.out.println("The set after removal: " + mySet);

        System.out.println("The size of the set is: " + mySet.size());

        mySet.clear();
        System.out.println("The set after clearing: " + mySet);
        System.out.println("Is the set empty? " + mySet.isEmpty());

        scanner.close();
    }
}
πŸ“€ Output:
Input: 3
Input: apple
Input: banana
Input: orange
Input: banana
Input: grape
Input: apple
Output: Enter the number of elements to add to the set:
Output: Enter the elements:
Output: The set is: [orange, banana, apple]
Output: Enter an element to check if it exists in the set:
Output: Does the set contain grape? false
Output: Enter an element to remove from the set:
Output: The set after removal: [orange, apple]
Output: The size of the set is: 2
Output: The set after clearing: []
Output: Is the set empty? true

β˜• Collections Framework - Set

πŸ’» Synchronized List Operations
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SynchronizedListOperations {

    public static void main(String[] args) {
        List<String> myList = new ArrayList<>();

        // Create a synchronized list
        List<String> synchronizedList = Collections.synchronizedList(myList);

        // Add elements to the synchronized list
        synchronized (synchronizedList) {
            synchronizedList.add("Item 1");
            synchronizedList.add("Item 2");
            synchronizedList.add("Item 3");
        }

        // Print the elements of the synchronized list
        System.out.println("Synchronized List elements:");
        synchronized (synchronizedList) {
            for (String item : synchronizedList) {
                System.out.println(item);
            }
        }

        //Remove an element from the synchronized list
        synchronized (synchronizedList) {
            synchronizedList.remove("Item 2");
        }

        // Print the elements of the synchronized list after removal
        System.out.println("nSynchronized List elements after removing Item 2:");
        synchronized (synchronizedList) {
            for (String item : synchronizedList) {
                System.out.println(item);
            }
        }

    }
}
πŸ“€ Output:
Synchronized List elements:
Item 1
Item 2
Item 3

Synchronized List elements after removing Item 2:
Item 1
Item 3

πŸ’» Array to List Conversion
import java.util.Arrays;
import java.util.List;

public class ArrayToListConversion {
    public static void main(String[] args) {
        String[] myArray = {"apple", "banana", "cherry"};

        // Convert array to list using Arrays.asList()
        List<String> myList = Arrays.asList(myArray);

        // Print the list
        System.out.println("List: " + myList);
    }
}
πŸ“€ Output:
List: [apple, banana, cherry]

πŸ’» List to Array Conversion
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

public class ListToArrayConversion {

    public static void main(String[] args) {

        // Creating a List of Strings
        List<String> myList = new ArrayList<>();
        myList.add("Apple");
        myList.add("Banana");
        myList.add("Orange");

        // Converting List to Array
        String[] myArray = myList.toArray(new String[0]);

        // Printing the array elements
        System.out.println("Array elements:");
        for (String element : myArray) {
            System.out.println(element);
        }

        //Alternative method using Arrays.copyOf
        Integer[] intList = {1, 2, 3, 4, 5};
        Integer[] intArray = Arrays.copyOf(intList, intList.length, Integer[].class);

        System.out.println("Integer Array Elements:");
        for(Integer element : intArray){
            System.out.println(element);
        }

    }
}
πŸ“€ Output:
Array elements:
Apple
Banana
Orange
Integer Array Elements:
1
2
3
4
5

πŸ’» Searching in Lists
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ListSearch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        List<String> fruits = new ArrayList<>();
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        fruits.add("Grapes");

        System.out.print("Enter the fruit name to search: ");
        String searchItem = scanner.nextLine();

        boolean found = false;
        for (String fruit : fruits) {
            if (fruit.equalsIgnoreCase(searchItem)) { // Case-insensitive search
                found = true;
                break;
            }
        }

        if (found) {
            System.out.println(searchItem + " is present in the list.");
        } else {
            System.out.println(searchItem + " is not present in the list.");
        }

        scanner.close();
    }
}
πŸ“€ Output:
Input: Banana
Output: Banana is present in the list.

Input: apple
Output: apple is present in the list.

Input: Mango
Output: Mango is not present in the list.

πŸ’» Sorting Lists
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListSorting {

    public static void main(String[] args) {

        // Create a List of Strings
        List<String> names = new ArrayList<>();
        names.add("Zoya");
        names.add("Ankit");
        names.add("Priya");
        names.add("Rohan");

        System.out.println("List before sorting: " + names);

        // Sort the list using Collections.sort()
        Collections.sort(names);

        System.out.println("List after sorting: " + names);

        // Create a List of Integers
        List<Integer> numbers = new ArrayList<>();
        numbers.add(5);
        numbers.add(1);
        numbers.add(9);
        numbers.add(2);

        System.out.println("List before sorting: " + numbers);

        // Sort the list using Collections.sort()
        Collections.sort(numbers);

        System.out.println("List after sorting: " + numbers);
    }
}
πŸ“€ Output:
List before sorting: [Zoya, Ankit, Priya, Rohan]
List after sorting: [Ankit, Priya, Rohan, Zoya]
List before sorting: [5, 1, 9, 2]
List after sorting: [1, 2, 5, 9]

πŸ’» List Iterator Usage
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListIteratorUsage {

    public static void main(String[] args) {

        // Create an ArrayList of Strings
        List<String> myArrayList = new ArrayList<>();
        myArrayList.add("Apple");
        myArrayList.add("Banana");
        myArrayList.add("Orange");

        // Get a ListIterator for the ArrayList
        ListIterator<String> listIterator = myArrayList.listIterator();

        // Iterate forward through the list
        System.out.println("Iterating forward:");
        while (listIterator.hasNext()) {
            String element = listIterator.next();
            System.out.println(element);
        }

        // Iterate backward through the list
        System.out.println("nIterating backward:");
        while (listIterator.hasPrevious()) {
            String element = listIterator.previous();
            System.out.println(element);
        }

        // Example of using set() method to modify an element
        if (listIterator.hasNext()) {
             listIterator.next(); // Move to the next element
             listIterator.set("Mango"); // Replace the element with "Mango"
        }

        System.out.println("nList after modification:");
        for (String fruit : myArrayList) {
            System.out.println(fruit);
        }

    }
}
πŸ“€ Output:
Iterating forward:
Apple
Banana
Orange

Iterating backward:
Orange
Banana
Apple

List after modification:
Apple
Mango
Orange

πŸ’» Stack Operations
import java.util.Stack;
import java.util.Scanner;

public class StackOperations {

    public static void main(String[] args) {

        Stack<Integer> myStack = new Stack<>();
        Scanner scanner = new Scanner(System.in);

        System.out.println("Stack Operations Menu:");
        System.out.println("1. Push (Add) element");
        System.out.println("2. Pop (Remove) element");
        System.out.println("3. Peek (View top element)");
        System.out.println("4. Check if stack is empty");
        System.out.println("5. Exit");

        int choice;
        do {
            System.out.print("Enter your choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    System.out.print("Enter element to push: ");
                    int elementToPush = scanner.nextInt();
                    myStack.push(elementToPush);
                    System.out.println(elementToPush + " pushed to stack.");
                    break;
                case 2:
                    if (!myStack.isEmpty()) {
                        int poppedElement = myStack.pop();
                        System.out.println(poppedElement + " popped from stack.");
                    } else {
                        System.out.println("Stack is empty. Cannot pop.");
                    }
                    break;
                case 3:
                    if (!myStack.isEmpty()) {
                        System.out.println("Top element: " + myStack.peek());
                    } else {
                        System.out.println("Stack is empty. Cannot peek.");
                    }
                    break;
                case 4:
                    System.out.println("Is stack empty? " + myStack.isEmpty());
                    break;
                case 5:
                    System.out.println("Exiting...");
                    break;
                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        } while (choice != 5);

        scanner.close();
    }
}
πŸ“€ Output:
Stack Operations Menu:
1. Push (Add) element
2. Pop (Remove) element
3. Peek (View top element)
4. Check if stack is empty
5. Exit
Enter your choice: Input: 1
Enter element to push: Input: 10
Output: 10 pushed to stack.
Enter your choice: Input: 1
Enter element to push: Input: 20
Output: 20 pushed to stack.
Enter your choice: Input: 3
Output: Top element: 20
Enter your choice: Input: 2
Output: 20 popped from stack.
Enter your choice: Input: 4
Output: Is stack empty? false
Enter your choice: Input: 2
Output: 10 popped from stack.
Enter your choice: Input: 4
Output: Is stack empty? true
Enter your choice: Input: 2
Output: Stack is empty. Cannot pop.
Enter your choice: Input: 5
Output: Exiting...

πŸ’» Vector Operations
import java.util.Vector;
import java.util.Scanner;

public class VectorOperations {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        Vector<Integer> myVector = new Vector<>();

        System.out.print("Enter the number of elements you want to add: ");
        int numElements = scanner.nextInt();

        System.out.println("Enter the elements:");
        for (int i = 0; i < numElements; i++) {
            int element = scanner.nextInt();
            myVector.add(element);
        }

        System.out.println("Vector elements: " + myVector);

        System.out.print("Enter the element you want to find: ");
        int searchElement = scanner.nextInt();
        int index = myVector.indexOf(searchElement);

        if (index != -1) {
            System.out.println("Element " + searchElement + " found at index: " + index);
        } else {
            System.out.println("Element " + searchElement + " not found in the vector.");
        }

        System.out.print("Enter the index of the element you want to remove: ");
        int removeIndex = scanner.nextInt();

        if (removeIndex >= 0 && removeIndex < myVector.size()) {
            myVector.remove(removeIndex);
            System.out.println("Element at index " + removeIndex + " removed.");
            System.out.println("Updated vector: " + myVector);
        } else {
            System.out.println("Invalid index.");
        }

        scanner.close();
    }
}
πŸ“€ Output:
Input: 3
Input: 10
Input: 20
Input: 30
Output: Enter the number of elements you want to add: Enter the elements:
Output: Vector elements: [10, 20, 30]
Input: 20
Output: Enter the element you want to find: Element 20 found at index: 1
Input: 0
Output: Enter the index of the element you want to remove: Element at index 0 removed.
Output: Updated vector: [20, 30]

πŸ’» LinkedList Operations
import java.util.LinkedList;
import java.util.Scanner;

public class LinkedListOperations {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        LinkedList<String> myList = new LinkedList<>();

        System.out.println("Enter elements to add to the linked list (type 'done' to finish):");
        String input;
        while (true) {
            input = scanner.nextLine();
            if (input.equalsIgnoreCase("done")) {
                break;
            }
            myList.add(input);
        }

        System.out.println("Linked List: " + myList);

        System.out.print("Enter element to add at the beginning: ");
        String firstElement = scanner.nextLine();
        myList.addFirst(firstElement);
        System.out.println("Linked List after adding at the beginning: " + myList);

        System.out.print("Enter element to add at the end: ");
        String lastElement = scanner.nextLine();
        myList.addLast(lastElement);
        System.out.println("Linked List after adding at the end: " + myList);

        System.out.print("Enter the index to remove: ");
        int indexToRemove = scanner.nextInt();
        scanner.nextLine(); // Consume newline character
        if (indexToRemove >= 0 && indexToRemove < myList.size()) {
            myList.remove(indexToRemove);
            System.out.println("Linked List after removing element at index " + indexToRemove + ": " + myList);
        } else {
            System.out.println("Invalid index!");
        }

        System.out.print("Enter the element to remove (first occurrence): ");
        String elementToRemove = scanner.nextLine();
        myList.remove(elementToRemove);
        System.out.println("Linked List after removing element " + elementToRemove + ": " + myList);

        System.out.println("First element in the list: " + myList.getFirst());
        System.out.println("Last element in the list: " + myList.getLast());

        scanner.close();
    }
}
πŸ“€ Output:
Enter elements to add to the linked list (type 'done' to finish):
Input: apple
Input: banana
Input: cherry
Input: done
Output: Linked List: [apple, banana, cherry]
Input: Enter element to add at the beginning: apricot
Output: Linked List after adding at the beginning: [apricot, apple, banana, cherry]
Input: Enter element to add at the end: date
Output: Linked List after adding at the end: [apricot, apple, banana, cherry, date]
Input: Enter the index to remove: 2
Output: Linked List after removing element at index 2: [apricot, apple, cherry, date]
Input: Enter the element to remove (first occurrence): date
Output: Linked List after removing element date: [apricot, apple, cherry]
Output: First element in the list: apricot
Output: Last element in the list: cherry

πŸ’» ArrayList Operations
import java.util.ArrayList;
import java.util.Scanner;

public class ArrayListOperations {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Creating an ArrayList of Strings
        ArrayList<String> names = new ArrayList<>();

        // Adding elements to the ArrayList
        System.out.print("How many names do you want to add? ");
        int numberOfNames = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        for (int i = 0; i < numberOfNames; i++) {
            System.out.print("Enter name " + (i + 1) + ": ");
            String name = scanner.nextLine();
            names.add(name);
        }

        // Printing the ArrayList
        System.out.println("ArrayList elements: " + names);

        // Accessing an element by index
        System.out.print("Enter the index of the name you want to see (starting from 0): ");
        int index = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        if (index >= 0 && index < names.size()) {
            System.out.println("Name at index " + index + ": " + names.get(index));
        } else {
            System.out.println("Invalid index.");
        }

        // Removing an element by index
        System.out.print("Enter the index of the name you want to remove (starting from 0): ");
        int removeIndex = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        if (removeIndex >= 0 && removeIndex < names.size()) {
            names.remove(removeIndex);
            System.out.println("Name removed successfully.");
            System.out.println("Updated ArrayList: " + names);
        } else {
            System.out.println("Invalid index.");
        }

        // Checking if an element exists
        System.out.print("Enter a name to check if it exists in the list: ");
        String searchName = scanner.nextLine();

        if (names.contains(searchName)) {
            System.out.println(searchName + " exists in the ArrayList.");
        } else {
            System.out.println(searchName + " does not exist in the ArrayList.");
        }

        // Getting the size of the ArrayList
        System.out.println("Size of the ArrayList: " + names.size());

        scanner.close();
    }
}
πŸ“€ Output:
How many names do you want to add? Input: 3
Enter name 1: Input: Alice
Enter name 2: Input: Bob
Enter name 3: Input: Charlie
Output: ArrayList elements: [Alice, Bob, Charlie]
Enter the index of the name you want to see (starting from 0): Input: 1
Output: Name at index 1: Bob
Enter the index of the name you want to remove (starting from 0): Input: 0
Output: Name removed successfully.
Output: Updated ArrayList: [Bob, Charlie]
Enter a name to check if it exists in the list: Input: Bob
Output: Bob exists in the ArrayList.
Output: Size of the ArrayList: 2

β˜• Collections Framework - List

Tech Jargon - Decoded - Statistics & analytics of Telegram channel @tech_jargon_decoded