Posts Tagged: "arraylist"

Java – HashMap (Collections Framework)

HashMap: A map-based collection that stores key-value pairs. Provides constant-time complexity for basic operations.Example: import java.util.HashMap;HashMap map = new HashMap();map.put(1, "John");map.put(2, "Alice");map.put(3, "Bob");System.out.println(map);System.out.println("Key 2: " + map.get(2));map.remove(1);System.out.println(map);for (int key : map.keySet()) { System.out.println(key + " -> " + map.get(key));}

Java – Arrays

Arrays: A collection of elements of the same type, stored in contiguous memory locations. Used to store fixed-size sequential elements.Example: int[] arr = {1, 2, 3, 4, 5};for (int i : arr) { System.out.println(i);}System.out.println("Length: " + arr.length); Multi-Dimensional Arrays: int[][] matrix = {{1, 2}, {3, 4}, {5, 6}};for (int[] row : matrix) { for (int […]