Java Collections Framework (Lists, Sets, Maps)

The Java Collections Framework (JCF) is a set of classes and interfaces that provide a standardized architecture for representing and manipulating collections of objects in Java. It includes several commonly used data structures such as lists, sets, and maps. Let's explore these three main interfaces in the Java Collections Framework:


1. Lists

Lists represent an ordered collection of elements where each element has a specific position (index) within the list. Some commonly used implementations of the List interface are ArrayList, LinkedList, and Vector.


Example

import java.util.ArrayList;
import java.util.List;

public class ListExample {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        
        // Adding elements to the list
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        
        // Accessing elements by index
        String firstElement = names.get(0);
        System.out.println("First element: " + firstElement);
        
        // Iterating over the list
        for (String name : names) {
            System.out.println(name);
        }
    }
}


2. Sets

Sets represent a collection of unique elements with no duplicates. Implementations of the Set interface include HashSet, TreeSet, and LinkedHashSet.


Example

import java.util.HashSet;
import java.util.Set;

public class SetExample {
    public static void main(String[] args) {
        Set<Integer> numbers = new HashSet<>();
        
     
  // Adding elements to the set
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(1); // Duplicate element
        
        // Iterating over the set
        for (Integer number : numbers) {
            System.out.println(number);
        }
    }
}


3. Maps

Maps represent a collection of key-value pairs, where each key is unique and maps to a specific value. Implementations of the Map interface include HashMap, TreeMap, and LinkedHashMap.


Example

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

public class MapExample {
    public static void main(String[] args) {
        Map<String, Integer> ages = new HashMap<>();
        
     
  // Adding key-value pairs to the map
        ages.put("Alice", 30);
        ages.put("Bob", 25);
        ages.put("Charlie", 35);
        
     
  // Accessing values by key
        int ageOfAlice = ages.get("Alice");
        System.out.println("Age of Alice: " + ageOfAlice);
        
     
  // Iterating over the map
        for (Map.Entry<String, Integer> entry : ages.entrySet()) {
            System.out.println(entry.getKey() + " - " + entry.getValue());
        }
    }
}


Conclusion

The Java Collections Framework provides a rich set of classes and interfaces for representing and manipulating collections of objects in Java. Lists, sets, and maps are three fundamental interfaces in the framework, each serving different purposes and offering various implementations to suit different requirements. Understanding how to use lists, sets, and maps effectively is essential for writing efficient and maintainable Java code.

Nenhum comentário:

Postar um comentário

Internet of Things (IoT) and Embedded Systems

The  Internet of Things (IoT)  and  Embedded Systems  are interconnected technologies that play a pivotal role in modern digital innovation....