- Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathCreateHashSetFromCollectionExample.java
30 lines (25 loc) · 1.01 KB
/
CreateHashSetFromCollectionExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
importjava.util.ArrayList;
importjava.util.HashSet;
importjava.util.List;
importjava.util.Set;
publicclassCreateHashSetFromCollectionExample {
publicstaticvoidmain(String[] args) {
List<Integer> numbersDivisibleBy5 = newArrayList<>();
numbersDivisibleBy5.add(5);
numbersDivisibleBy5.add(10);
numbersDivisibleBy5.add(15);
numbersDivisibleBy5.add(20);
numbersDivisibleBy5.add(25);
List<Integer> numbersDivisibleBy3 = newArrayList<>();
numbersDivisibleBy3.add(3);
numbersDivisibleBy3.add(6);
numbersDivisibleBy3.add(9);
numbersDivisibleBy3.add(12);
numbersDivisibleBy3.add(15);
// Creating a HashSet from another collection (ArrayList)
Set<Integer> numbersDivisibleBy5Or3 = newHashSet<>(numbersDivisibleBy5);
// Adding all the elements from an existing collection to a HashSet
numbersDivisibleBy5Or3.addAll(numbersDivisibleBy3);
System.out.println(numbersDivisibleBy5Or3);
}
}