Site icon Full-Stack

Java Collections Framework: List, Set, and Map Explained

The robust Java Collections Framework (JCF) API offers a number of data structures for effective data processing, manipulation, and storage. List, Set, and Map are the three JCF interfaces that are most frequently used. Writing scalable and effective Java programs requires an understanding of these basic collection types.

In this blog, we’ll explore:


1. Lists in Java

In Java, an ordered collection that permits duplicate elements is called a list. Lists give elements indexed access and preserve the insertion order.

Typical List Implementations:

Example: Using ArrayList

import java.util.*;

public class ListExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");
        list.add("Banana"); // Duplicates are allowed

        System.out.println("List: " + list);
    }
}

Output:

List: [Apple, Banana, Cherry, Banana]

When to Use a List?


2. Sets in Java

In Java, a set is a collection that, with the exception of some implementations, does not permit duplicate elements and does not ensure order.

Typical Set Implementations:

Example: Using HashSet

import java.util.*;

public class SetExample {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        set.add("Apple");
        set.add("Banana");
        set.add("Cherry");
        set.add("Banana"); // Duplicate, will not be added

        System.out.println("Set: " + set);
    }
}

Output (Order may vary):

Set: [Apple, Banana, Cherry]

When to Use a Set?


3. Maps in Java

In Java, a collection that holds key-value pairs is called a map. Values can be replicated, but each key needs to be distinct.

Typical Map Implementations:

Example: Using HashMap

import java.util.*;

public class MapExample {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "Apple");
        map.put(2, "Banana");
        map.put(3, "Cherry");
        map.put(2, "Mango"); // Overwrites previous value

        System.out.println("Map: " + map);
    }
}

Output (Order may vary):

Map: {1=Apple, 2=Mango, 3=Cherry}

When to Use a Map?


Comparison Table: List vs Set vs Map

FeatureListSetMap
Allows DuplicatesYesNoKeys: No, Values: Yes
Maintains OrderYes (Insertion Order)No (Except LinkedHashSet)No (Except LinkedHashMap)
Access Time ComplexityO(1) (ArrayList), O(N) (LinkedList)O(1) (HashSet), O(log N) (TreeSet)O(1) (HashMap), O(log N) (TreeMap)
Key-Value PairsNoNoYes

Conclusion

In Java development, choosing the appropriate data structure requires an understanding of the distinctions between List, Set, and Map. Each has specific applications:

You can create Java apps that are more scalable and efficient by becoming proficient with these collections.

It might be helpful:

Is SAP ABAP a High Paying Job?

Your Definitive Guide to Becoming a SAP ABAP Developer

Is Java or Python Better for Full-Stack Development?

Exit mobile version