Collections in Java

 Collections in Java


Collections are data structures that allow you to store and manipulate groups of objects. Java provides a variety of built-in collections, including ArrayList, LinkedList, HashSet, and HashMap.


Example code:


csharp


List<String> names = new ArrayList<>();

names.add("Alice");

names.add("Bob");

names.add("Charlie");


for (String name : names) {

    System.out.println(name);

}


Set<Integer> numbers = new HashSet<>();

numbers.add(1);

numbers.add(2);

numbers.add(3);


for (int number : numbers) {

    System.out.println(number);

}


Map<String, Integer> ages = new HashMap<>();

ages.put("Alice", 25);

ages.put("Bob", 30);

ages.put("Charlie", 35);


for (String name : ages.keySet()) {

    System.out.println(name + " is " + ages.get(name) + " years old.");

}

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...