9. Collections
mutable vs immutable
thread-unsafe vs thread-safe
9.1. List
9.1.1. List creation
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.List;
1List<String> list1 = new ArrayList<>();
2list1.add("John");
3list1.add("Joe");
4
5List<String> list2 = new ArrayList<>(){{
6 add("John");
7 add("Joe");
8}};
9
10// immutable
11List<String> list3 = List.of("John", "Joe");
12
13List<String> list4 = Arrays.asList(new String[] { "John", "Joe" });
14
15List<String> list5 = Arrays.asList("John", "Joe");
9.1.2. List iteration with forEach
1import java.util.Arrays;
2import java.util.List;
3import java.util.Optional;
1List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
2
3numbers.forEach(n -> System.out.println(n));
4
5numbers.forEach(System.out::println);
9.1.3. List manipulation
1import java.util.ArrayList;
2import java.util.Arrays;
3import java.util.List;
1List<String> names = new ArrayList<>() {{
2 add("John");
3 add("Joe");
4}};
5
6// access element
7String joe = names.get(1);
8
9// set element
10names.set(0, "Jeremy");
11
12// add element
13names.add("Jack");
14
15// iteration using for loop
16for (int i = 0; i < names.size(); i++) {
17 System.out.println(names.get(i));
18}
19
20// iteration using for-each
21for (var name : names) {
22 System.out.println(name);
23}
9.1.4. List sorting
1import java.util.Arrays;
2import java.util.Collections;
3import java.util.List;
1// unsorted
2List<String> names = Arrays.asList("John", "Jack", "Jimmy");
3
4for (var name : names) {
5 System.out.println(name);
6}
7
8// sorted ascendingly
9Collections.sort(names);
10
11for (var name : names) {
12 System.out.println(name);
13}
14
15// sorted descendingly
16Collections.sort(names, Collections.reverseOrder());
17
18for (var name : names) {
19 System.out.println(name);
20}
21
22// custom sorting, sort by second character
23Collections.sort(names, (lhs, rhs) -> {
24 Character lhsChar = lhs.charAt(1);
25 Character rhsChar = lhs.charAt(1);
26 return lhsChar.compareTo(rhsChar);
27});
28
29for (var name : names) {
30 System.out.println(name);
31}
9.2. Set
9.2.1. Set creation
1import java.util.Arrays;
2import java.util.HashSet;
3import java.util.Set;
1Set<String> set1 = new HashSet<>();
2set1.add("John");
3set1.add("Joe");
4set1.add("John");
5
6Set<String> set2 = new HashSet<>(Arrays.asList("John", "Joe", "John"));
7
8// cannot add duplicates
9// immutable
10Set<String> set3 = Set.of("John", "Joe");
11
12Set<String> set4 = new HashSet<>() {{
13 add("John");
14 add("Joe");
15 add("John");
16}};
9.2.2. Set manipulation
1import java.util.HashSet;
2import java.util.Set;
1Set<String> set = new HashSet<>() {{
2 add("John");
3 add("Joe");
4 add("John");
5}};
6
7// check if element exists
8set.contains("Jack");
9
10// add element
11set.add("Jack");
12
13// remove element
14set.remove("John");
15
16// iteration
17for (var name : set) {
18 System.out.println(name);
19}
9.2.3. Set union
1import java.util.HashSet;
2import java.util.Set;
1Set<String> set1 = new HashSet<>() {{
2 add("John");
3 add("Joe");
4 add("Jack");
5}};
6
7Set<String> set2 = new HashSet<>() {{
8 add("John");
9 add("Joe");
10 add("Mary");
11}};
12
13set1.addAll(set2);
9.2.4. Set intersection
1import java.util.HashSet;
2import java.util.Set;
1Set<String> set1 = new HashSet<>() {{
2 add("John");
3 add("Joe");
4 add("Jack");
5}};
6
7Set<String> set2 = new HashSet<>() {{
8 add("John");
9 add("Joe");
10 add("Mary");
11}};
12
13set1.retainAll(set2);
9.2.5. Set relative complement
1import java.util.HashSet;
2import java.util.Set;
1Set<String> set1 = new HashSet<>() {{
2 add("John");
3 add("Joe");
4 add("Jack");
5}};
6
7Set<String> set2 = new HashSet<>() {{
8 add("John");
9 add("Joe");
10 add("Mary");
11}};
12
13set1.removeAll(set2);
9.3. Map
9.3.1. Map creation
1import java.util.HashMap;
2import java.util.Map;
1Map<String, Integer> map1 = new HashMap<>();
2map1.put("John", 18);
3map1.put("Joe", 21);
4
5Map<String, Integer> map2 = new HashMap<>() {{
6 put("John", 18);
7 put("Joe", 21);
8}};
9.3.2. Map manipulation
1import java.util.HashMap;
2import java.util.Map;
1Map<String, Integer> map = new HashMap<>() {{
2 put("John", 18);
3 put("Joe", 21);
4}};
5
6// check if key exists
7map.containsKey("John");
8
9// check if value exists
10map.containsValue(18);
11
12// add key-value pair
13map.put("Jack", 25);
14
15// remove key-value pair
16map.remove("John");
17
18// replace
19map.put("John", 19);
20
21// iteration
22for (var entry : map.entrySet()) {
23 System.out.println(entry.getKey() + " " + entry.getValue());
24}