Skip to content

Commit e833b3a

Browse files
committed
Java Collections examples
1 parent bc127d7 commit e833b3a

3 files changed

Lines changed: 68 additions & 97 deletions

File tree

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,42 @@
11
package com.hellokoding.java.collections;
22

33

4-
import java.util.*;
4+
import java.util.ArrayList;
5+
import java.util.Comparator;
6+
import java.util.List;
57

68
public class ArrayListExample {
79
public static void main(String args[]) {
8-
// create and initialize with ArrayList constructors
9-
10-
List<Integer> lst1 = new ArrayList<>();
11-
lst1.add(1);
12-
lst1.add(2);
13-
lst1.add(3);
14-
15-
List<Integer> lst2 = new ArrayList<>(20);
16-
17-
List<Integer> lst3 = new ArrayList<>(List.of(1, 2, 3));
18-
System.out.println(lst3); // [1, 2, 3]
19-
20-
List<Integer> lst4 = new ArrayList<>(Set.of(1, 2, 3));
21-
System.out.println(lst4); // [3, 2, 1]
22-
23-
// create and initialize with Arrays.asList
24-
25-
List<Integer> lst5 = Arrays.asList(1, 2, 3);
26-
27-
// create and initialize with Collections factory methods
28-
29-
List<Integer> lst6 = Collections.emptyList();
30-
31-
List<Integer> lst7 = Collections.singletonList(1);
32-
33-
// create and initialize with Java 9+ List.of, List.copyOf
34-
35-
List<Integer> lst9 = List.of(1, 2, 3);
36-
List<Integer> lst10 = List.copyOf(lst9);
10+
// create and initialize
11+
List<Integer> lst = new ArrayList<>();
12+
lst.add(3);
13+
lst.add(1);
14+
lst.add(2);
15+
System.out.println(lst);
3716

3817
// iterate with Java 8+ forEach(Consumer)
18+
lst.forEach(e -> System.out.printf("%d ", e));
3919

40-
lst1.forEach(System.out::println);
41-
42-
// iterate with for loop
43-
44-
for(int ele : lst1) {
45-
System.out.printf("%d ", ele);
46-
}
47-
System.out.println();
48-
49-
for (int i = 0; i < lst1.size(); i++) {
50-
System.out.printf("%d ", lst1.get(i));
51-
}
52-
System.out.println();
53-
54-
// iterate with iterator
55-
56-
Iterator<Integer> iter = lst1.iterator();
57-
while (iter.hasNext()) {
58-
System.out.printf("%d ", iter.next());
59-
}
60-
System.out.println();
20+
// sort
21+
lst.sort(Comparator.naturalOrder());
22+
System.out.println(lst);
6123

62-
// add elements into a list
63-
64-
lst1.add(4);
65-
lst1.add(5);
66-
lst1.add(5); // add a duplicate element
67-
lst1.add(null); // add a null element
68-
System.out.println(lst1); // [1, 2, 3, 4, 5, 5, null]
24+
// query
25+
System.out.println(lst.get(0)); // get element by index
26+
System.out.println(lst.indexOf(3)); // get index by element
27+
System.out.println(lst.contains(2));
28+
System.out.println(lst.size());
29+
System.out.println(lst.isEmpty());
6930

7031
// update an element at index
71-
72-
lst1.set(0, 10);
32+
lst.set(0, 10);
33+
System.out.println(lst);
7334

7435
// delete an element by index
75-
76-
lst1.remove(1);
77-
36+
lst.remove(1);
37+
System.out.println(lst);
7838
// delete an element by value
79-
80-
lst1.remove(null);
81-
82-
// get an element by index
83-
84-
int i1 = lst1.get(0);
85-
86-
// check if an element existing
87-
88-
boolean isExist = lst1.contains(3);
89-
90-
// check the list size
91-
int size = lst1.size();
92-
93-
// check if the list is empty
94-
95-
boolean isEmpty = lst1.isEmpty();
96-
97-
// sort a list in ascending order
98-
// NullPointerException will be thrown if the list has null elements
99-
100-
lst1.sort(Comparator.naturalOrder());
101-
Collections.sort(lst1);
102-
System.out.println(lst1);
103-
104-
// sort a list in descending order
105-
106-
lst1.sort(Comparator.reverseOrder());
107-
Collections.sort(lst1, Collections.reverseOrder());
108-
System.out.println(lst1);
39+
lst.remove(Integer.valueOf(3));
40+
System.out.println(lst);
10941
}
11042
}

java-examples/java-core/src/main/java/com/hellokoding/java/collections/HashMapExample.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public static void main(String args[]) {
1313
System.out.println(map); // {k1=1, k2=2}
1414

1515
// iterate
16-
map.entrySet().forEach(System.out::println);
17-
map.keySet().forEach(System.out::println);
18-
map.values().forEach(System.out::println);
16+
map.entrySet().forEach(e -> System.out.printf("%s=%d ", e.getKey(), e.getValue()));
17+
map.keySet().forEach(e -> System.out.printf("%s ", e));
18+
map.values().forEach(e -> System.out.printf("%d ", e));
19+
System.out.println();
1920

2021
// sort
2122
SortedMap<String, Integer> sortedMap = new TreeMap<>(map);
@@ -29,7 +30,7 @@ public static void main(String args[]) {
2930

3031
// update value
3132
map.replace("k2", 20);
32-
System.out.println(map); // {k1=1, k2=2}
33+
System.out.println(map); // {k1=1, k2=20}
3334

3435
// remove entries
3536
map.remove("k2");
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.hellokoding.java.collections;
2+
3+
4+
import java.util.*;
5+
6+
public class HashSetExample {
7+
public static void main(String args[]) {
8+
// create and initialize
9+
Set<Integer> set = new HashSet<>();
10+
set.add(3);
11+
set.add(1);
12+
set.add(2);
13+
System.out.println(set);
14+
15+
// iterate with Java 8+ forEach(Consumer)
16+
set.forEach(e -> System.out.printf("%d ", e));
17+
System.out.println();
18+
19+
// sort
20+
Set<Integer> treeSet = new TreeSet<>(set);
21+
System.out.println(treeSet);
22+
23+
// query
24+
System.out.println(set.iterator().next()); // get first element
25+
System.out.println(set.contains(2));
26+
System.out.println(set.size());
27+
System.out.println(set.isEmpty());
28+
29+
// delete an element
30+
set.remove(1);
31+
System.out.println(set);
32+
33+
// update = delete + add
34+
set.remove(2);
35+
set.add(20);
36+
System.out.println(set);
37+
}
38+
}

0 commit comments

Comments
 (0)