Create an ArrayList, HashSet, and HashMap, and perform basic operations like adding, removing, and iterating.

Abhishek Kumar - Oct 3 - - Dev Community

Here’s how to create an ArrayList, HashSet, and HashMap in Java and perform basic operations such as adding, removing, and iterating through them.

1. ArrayList Example

import java.util.ArrayList;

public class ArrayListExample {
    public static void main(String[] args) {
        // Create an ArrayList
        ArrayList<String> fruits = new ArrayList<>();

        // Add elements to the ArrayList
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");

        // Iterate over the ArrayList
        System.out.println("ArrayList Elements:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        // Remove an element from the ArrayList
        fruits.remove("Banana");
        System.out.println("After removing 'Banana': " + fruits);

        // Accessing element by index
        System.out.println("Element at index 1: " + fruits.get(1));
    }
}
Enter fullscreen mode Exit fullscreen mode

2. HashSet Example

import java.util.HashSet;

public class HashSetExample {
    public static void main(String[] args) {
        // Create a HashSet
        HashSet<String> fruits = new HashSet<>();

        // Add elements to the HashSet
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Orange");
        fruits.add("Apple"); // Duplicate entry (will be ignored)

        // Iterate over the HashSet
        System.out.println("HashSet Elements:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }

        // Remove an element from the HashSet
        fruits.remove("Banana");
        System.out.println("After removing 'Banana': " + fruits);

        // Check if a value exists
        System.out.println("Contains 'Apple'? " + fruits.contains("Apple"));
    }
}
Enter fullscreen mode Exit fullscreen mode

3. HashMap Example

import java.util.HashMap;
import java.util.Map;

public class HashMapExample {
    public static void main(String[] args) {
        // Create a HashMap
        HashMap<String, Integer> fruitPrices = new HashMap<>();

        // Add key-value pairs to the HashMap
        fruitPrices.put("Apple", 100);
        fruitPrices.put("Banana", 40);
        fruitPrices.put("Orange", 80);

        // Iterate over the HashMap
        System.out.println("HashMap Elements:");
        for (Map.Entry<String, Integer> entry : fruitPrices.entrySet()) {
            System.out.println(entry.getKey() + " costs " + entry.getValue() + " units");
        }

        // Remove an element from the HashMap
        fruitPrices.remove("Banana");
        System.out.println("After removing 'Banana': " + fruitPrices);

        // Check if a key exists
        System.out.println("Contains 'Apple'? " + fruitPrices.containsKey("Apple"));
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • ArrayList:

    • Stores elements in the order they are added.
    • Allows duplicate elements.
    • Provides indexed access to elements.
  • HashSet:

    • Does not allow duplicate elements.
    • Does not guarantee the order of elements.
  • HashMap:

    • Stores key-value pairs.
    • Does not allow duplicate keys, but values can be duplicated.
    • Efficient for searching, adding, and removing elements using keys.

These examples demonstrate basic operations such as adding, removing, checking existence, and iterating through each data structure.

. . . . . . . . . . . . . . . . . . . . .
Terabox Video Player