Java Collections: ArrayList, LinkedList, HashSet, HashMap
In Java, the Collections framework provides various implementations for List and Set interfaces, such as ArrayList, LinkedList, HashSet, and HashMap.
ArrayList:
ArrayList is an implementation of the List interface. It stores elements in a dynamically resizable array.
// Example of using ArrayList
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
public static void main(String[] args) {
// Creating an ArrayList
List<String> fruits = new ArrayList<>();
// Adding elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Accessing elements
System.out.println("Fruits: " + fruits);
// Iterating through the list
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
LinkedList:
LinkedList is another implementation of the List interface. It stores elements in a doubly-linked list.
// Example of using LinkedList
import java.util.LinkedList;
import java.util.List;
public class LinkedListExample {
public static void main(String[] args) {
// Creating a LinkedList
List<String> colors = new LinkedList<>();
// Adding elements
colors.add("Red");
colors.add("Green");
colors.add("Blue");
// Accessing elements
System.out.println("Colors: " + colors);
// Iterating through the list
for (String color : colors) {
System.out.println(color);
}
}
}
HashSet:
HashSet is an implementation of the Set interface. It stores unique elements and does not maintain any order.
// Example of using HashSet
import java.util.HashSet;
import java.util.Set;
public class HashSetExample {
public static void main(String[] args) {
// Creating a HashSet
Set<String> countries = new HashSet<>();
// Adding elements
countries.add("India");
countries.add("USA");
countries.add("China");
// Adding a duplicate element (ignored in a Set)
countries.add("India");
// Accessing elements
System.out.println("Countries: " + countries);
// Iterating through the set
for (String country : countries) {
System.out.println(country);
}
}
}
HashMap:
HashMap is an implementation of the Map interface. It stores key-value pairs and allows fast retrieval of values based on keys.
// Example of using HashMap
import java.util.HashMap;
import java.util.Map;
public class HashMapExample {
public static void main(String[] args) {
// Creating a HashMap
Map<String, Integer> population = new HashMap<>();
// Adding key-value pairs
population.put("India", 1380004385);
population.put("China", 1444216107);
population.put("USA", 331002651);
// Accessing values by key
System.out.println("Population of India: " + population.get("India"));
// Iterating through the map
for (Map.Entry<String, Integer> entry : population.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue() + " people");
}
}
}
Note:
The ArrayList, LinkedList, HashSet, and HashMap are commonly used implementations in the Java Collections framework, providing different functionalities and use cases based on the requirements of your application.