Array, List, Set, and Map
Here’s a concise overview of key Java collections: Array, List, Set, and Map along with basic usage examples and their documentation.
✅ 1. Array
An Array is a data structure that stores a fixed-size sequence of elements of the same type.
Documentation:
-
Array in Java is an object that holds a fixed number of values of a single type.
-
Arrays are zero-indexed, meaning the first element is at index
0. -
Arrays are immutable in size once created (i.e., you can't resize them).
Example:
public class Main {
public static void main(String[] args) {
// Declare and initialize an array
int[] arr = {10, 20, 30, 40, 50};
// Accessing array elements
System.out.println("Element at index 2: " + arr[2]);
// Looping through array
for (int i = 0; i < arr.length; i++) {
System.out.println("Element at index " + i + ": " + arr[i]);
}
}
}
✅ 2. List
A List is an ordered collection (sometimes called a sequence). It allows for duplicates and provides positional access to elements.
Key Classes:
-
ArrayList (most commonly used)
-
LinkedList
-
Vector
Documentation:
-
List is part of the
java.utilpackage. -
Lists maintain the order of insertion and allow duplicate elements.
-
You can access elements by their index.
Example (Using ArrayList):
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// Create a List (ArrayList)
ArrayList<String> list = new ArrayList<>();
// Add elements
list.add("Apple");
list.add("Banana");
list.add("Cherry");
// Accessing elements
System.out.println("Element at index 1: " + list.get(1));
// Looping through List
for (String item : list) {
System.out.println(item);
}
}
}
✅ 3. Set
A Set is a collection that does not allow duplicate elements. It models the mathematical set abstraction and is part of the Java Collections Framework.
Key Classes:
-
HashSet (does not maintain any order)
-
LinkedHashSet (maintains insertion order)
-
TreeSet (stores elements in sorted order)
Documentation:
-
Set is part of the
java.utilpackage. -
No duplicates are allowed.
-
Elements are unordered in
HashSetbut sorted inTreeSet.
Example (Using HashSet):
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
// Create a Set (HashSet)
HashSet<String> set = new HashSet<>();
// Add elements
set.add("Apple");
set.add("Banana");
set.add("Apple"); // Duplicate, won't be added
// Looping through Set
for (String item : set) {
System.out.println(item);
}
}
}
✅ 4. Map
A Map is an object that maps keys to values. A Map cannot contain duplicate keys, and each key can map to only one value.
Key Classes:
-
HashMap (does not maintain order)
-
LinkedHashMap (maintains insertion order)
-
TreeMap (stores keys in sorted order)
Documentation:
-
Map is part of the
java.utilpackage. -
Each key in a map must be unique. Values can be duplicated.
-
Common methods include
put(),get(),containsKey(), andremove().
Example (Using HashMap):
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// Create a Map (HashMap)
HashMap<String, Integer> map = new HashMap<>();
// Add key-value pairs
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
// Access value using key
System.out.println("Value for 'Apple': " + map.get("Apple"));
// Looping through Map
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
}
}
Summary of Collections in Java:
| Collection Type | Allows Duplicates | Order Maintained | Key-Value Pair |
|---|---|---|---|
| Array | Yes | No | No |
| List | Yes | Yes | No |
| Set | No | Depends on Implementation | No |
| Map | No (Keys) | Depends on Implementation | Yes (Keys and Values) |
π Useful Documentation Links:
Would you like to see more examples or details on any of these?
Comments
Post a Comment