-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImperativeVsDeclarativeExample2.java
More file actions
49 lines (43 loc) · 1.32 KB
/
ImperativeVsDeclarativeExample2.java
File metadata and controls
49 lines (43 loc) · 1.32 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.learn.imperativevsdeclarative;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* <p>
* Remove Duplicates from a list of integers.
* </p>
*/
public class ImperativeVsDeclarativeExample2 {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 4, 5, 5, 5, 2, 4, 1, 5, 7, 8, 9, 9, 10);
System.out.println(removeDuplicatesUsingImperativeApproach(integerList));
System.out.println(removeDuplicatesUsingDeclarativeApproach(integerList));
}
/**
* Imperative Approach
* @param list
* @return
*/
private static List<Integer> removeDuplicatesUsingImperativeApproach(List<Integer> list) {
Set<Integer> uniqueList = new HashSet<>();
for (Integer num : list) {
if (!uniqueList.contains(num)) {
uniqueList.add(num);
}
}
return new ArrayList<>(uniqueList);
}
/**
* Declarative Approach
* @param list
* @return
*/
private static List<Integer> removeDuplicatesUsingDeclarativeApproach(List<Integer> list) {
return list.stream()
.distinct()
.collect(Collectors.toList());
}
}