-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathArrayUtility.java
More file actions
47 lines (42 loc) · 1.5 KB
/
ArrayUtility.java
File metadata and controls
47 lines (42 loc) · 1.5 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
package com.zipcodewilmington.arrayutility;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
/**
* Created by leon on 3/6/18.
*/
public class ArrayUtility<E> {
E[] array;
public ArrayUtility(E[] array){
this.array = array;
}
//is it posssible to do this better without an unchecked cast?
//maybe doing my own array conversion method?
public E[] removeValue(E valueToRemove) {
return Arrays
.stream(this.array)
.filter(q-> !q.equals(valueToRemove))
.toArray(q -> Arrays.copyOf(this.array,
this.array.length - getNumberOfOccurrences(valueToRemove)));
}
public Integer countDuplicatesInMerge(E[] arrayToMerge, E valueToEvaluate) {
return Math
.toIntExact(Arrays
.stream(arrayToMerge)
.filter(q-> q.equals(valueToEvaluate))
.count() + getNumberOfOccurrences(valueToEvaluate));
}
public Integer getNumberOfOccurrences(E valueToEvaluate) {
return Math
.toIntExact(Arrays
.stream(this.array)
.filter(q-> q.equals(valueToEvaluate))
.count());
}
public E getMostCommonFromMerge(E[] arrayToMerge) {
return Arrays
.stream(arrayToMerge)
.max(Comparator.comparing(this::getNumberOfOccurrences))
.orElse(null);
}
}