RKTechGame | Java Common Collection Classes

ArrayList

ArrayList is a resizable array implementation of the List interface. It stores elements in index-based order and allows duplicates. It provides fast random access.

Key features:

Example:

ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list.get(1)); // Output: Banana

LinkedList

LinkedList implements the List and Deque interfaces. It uses a doubly-linked list under the hood. It allows duplicate elements and insertion order is maintained.

Key features:

Example:

LinkedList<String> ll = new LinkedList<>();
ll.add("Dog");
ll.add("Cat");
ll.add("Bird");
System.out.println(ll.remove()); // Removes first element: Dog

HashSet

HashSet implements the Set interface backed by a hash table. It stores unique elements and does not maintain any order.

Key features:

Example:

HashSet<String> hs = new HashSet<>();
hs.add("Java");
hs.add("Python");
hs.add("Java"); // Duplicate ignored
System.out.println(hs); // Output: [Java, Python]

HashMap

HashMap implements the Map interface and stores key-value pairs. Keys are unique but values can be duplicated.

Key features:

Example:

HashMap<Integer, String> map = new HashMap<>();
map.put(1, "Red");
map.put(2, "Green");
map.put(3, "Blue");
System.out.println(map.get(2)); // Output: Green