Java List, Map, Set
- List: Use when you need an ordered collection of elements.
- Map: Use when you need to associate keys with values.
- Set: Use when you need a collection of unique elements.
- ArrayList vs. LinkedList: Choose ArrayList for random access and LinkedList for insertions and deletions.
- HashMap vs. TreeMap: Choose HashMap for faster lookups and TreeMap for sorted keys.
- HashSet vs. TreeSet: Choose HashSet for faster lookups and TreeSet for sorted elements.
1. Lists (ArrayList and LinkedList):
Lists are ordered collections of elements. ArrayList is generally faster for random access, while LinkedList is more efficient for insertions and deletions.
1: import java.util.ArrayList;
2: import java.util.LinkedList;
3: import java.util.List;
4:
5: public class ListExamples {
6:
7: public static void main(String[] args) {
8: // ArrayList Example
9: List<String> arrayList = new ArrayList<>();
10: arrayList.add("Apple");
11: arrayList.add("Banana");
12: arrayList.add("Orange");
13:
14: System.out.println("ArrayList: " + arrayList);
15:
16: // Accessing elements
17: System.out.println("First element: " + arrayList.get(0));
18:
19: // Removing an element
20: arrayList.remove("Banana");
21: System.out.println("ArrayList after removing Banana: " + arrayList);
22:
23: // LinkedList Example
24: List<Integer> linkedList = new LinkedList<>();
25: linkedList.add(10);
26: linkedList.add(20);
27: linkedList.add(30);
28:
29: System.out.println("LinkedList: " + linkedList);
30:
31: // Adding to the beginning
32: linkedList.add(0, 5);
33: System.out.println("LinkedList after adding to the beginning: " + linkedList);
34:
35: // Removing from the end
36: linkedList.remove(linkedList.size() - 1);
37: System.out.println("LinkedList after removing from the end: " + linkedList);
38: }
39: }
40:
2. Maps (HashMap and TreeMap):
Maps are key-value pairs. HashMap is generally faster for lookups, while TreeMap is sorted by keys.
1: import java.util.HashMap;
2: import java.util.Map;
3: import java.util.TreeMap;
4:
5: public class MapExamples {
6:
7: public static void main(String[] args) {
8: // HashMap Example
9: Map<String, Integer> hashMap = new HashMap<>();
10: hashMap.put("Apple", 10);
11: hashMap.put("Banana", 20);
12: hashMap.put("Orange", 30);
13:
14: System.out.println("HashMap: " + hashMap);
15:
16: // Accessing values
17: System.out.println("Value for Banana: " + hashMap.get("Banana"));
18:
19: // Removing a key-value pair
20: hashMap.remove("Orange");
21: System.out.println("HashMap after removing Orange: " + hashMap);
22:
23: // TreeMap Example
24: Map<String, Integer> treeMap = new TreeMap<>();
25: treeMap.put("Apple", 10);
26: treeMap.put("Banana", 20);
27: treeMap.put("Orange", 30);
28:
29: System.out.println("TreeMap: " + treeMap);
30:
31: // Accessing values
32: System.out.println("Value for Banana: " + treeMap.get("Banana"));
33:
34: // Removing a key-value pair
35: treeMap.remove("Orange");
36: System.out.println("TreeMap after removing Orange: " + treeMap);
37: }
38: }
39:
3. Sets (HashSet and TreeSet):
Sets are unordered collections of unique elements. HashSet is generally faster for lookups, while TreeSet is sorted.
1: import java.util.HashSet;
2: import java.util.Set;
3: import java.util.TreeSet;
4:
5: public class SetExamples {
6:
7: public static void main(String[] args) {
8: // HashSet Example
9: Set<String> hashSet = new HashSet<>();
10: hashSet.add("Apple");
11: hashSet.add("Banana");
12: hashSet.add("Orange");
13: hashSet.add("Apple"); // Adding a duplicate
14:
15: System.out.println("HashSet: " + hashSet);
16:
17: // Removing an element
18: hashSet.remove("Banana");
19: System.out.println("HashSet after removing Banana: " + hashSet);
20:
21: // TreeSet Example
22: Set<String> treeSet = new TreeSet<>();
23: treeSet.add("Apple");
24: treeSet.add("Banana");
25: treeSet.add("Orange");
26: treeSet.add("Apple"); // Adding a duplicate
27:
28: System.out.println("TreeSet: " + treeSet);
29:
30: // Removing an element
31: treeSet.remove("Banana");
32: System.out.println("TreeSet after removing Banana: " + treeSet);
33: }
34: }
35:
Java context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/Java/Index13.htm
|
|