NedRedmond 6 лет назад
Родитель
Сommit
819e6f616f

+ 83
- 2
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Просмотреть файл

@@ -1,7 +1,88 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
6
+import java.util.Map;
7
+import java.util.function.Function;
8
+import java.util.stream.Collectors;
9
+import java.util.stream.Stream;
10
+
11
+import static java.lang.Math.toIntExact;
12
+
3 13
 /**
4 14
  * Created by leon on 3/6/18.
5 15
  */
6
-public class ArrayUtility {
7
-}
16
+public class ArrayUtility<T> {
17
+
18
+    T[] inputArray;
19
+
20
+    public ArrayUtility(T[] inputArray) {
21
+        this.inputArray = inputArray;
22
+    }
23
+
24
+    private Stream<T> getTStream(T[] array) {
25
+        return Arrays.stream(array);
26
+    }
27
+
28
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
29
+        Stream<T> inputStream = getTStream(inputArray);
30
+        Stream<T> toMergeStream = getTStream(arrayToMerge);
31
+        Stream<T> mergedStream = Stream.concat(inputStream,toMergeStream);
32
+        return mergedStream
33
+                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
34
+                .entrySet()
35
+                .stream()
36
+                .max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1)
37
+                .get()
38
+                .getKey()
39
+                ;
40
+    }
41
+
42
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
43
+        Stream<T> inputStream = getTStream(inputArray);
44
+        Stream<T> toMergeStream = getTStream(arrayToMerge);
45
+        Stream<T> mergedStream = Stream.concat(inputStream,toMergeStream);
46
+        Long result =  mergedStream
47
+                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
48
+                .entrySet()
49
+                .stream()
50
+                .max((e1, e2) -> e1.getValue() > e2.getValue() ? 1 : -1)
51
+                .get()
52
+                .getValue()
53
+                ;
54
+        return toIntExact(result);
55
+    }
56
+
57
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
58
+        return toIntExact(getTStream(inputArray)
59
+                    .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
60
+                    .entrySet()
61
+                    .stream()
62
+                    .filter(e -> e.getKey().equals(valueToEvaluate))
63
+                    .map(Map.Entry::getValue)
64
+                    .findFirst()
65
+                    .orElse(null)
66
+                );
67
+    }
68
+
69
+    public T[] removeValue(T valueToRemove) {
70
+        // UGH this doesn't work for stupid Java reasons
71
+//        T[] actual = (T[]) getTStream(inputArray)
72
+//                .filter(t -> !t.equals(valueToRemove))
73
+//                .toArray((Object[]::new));
74
+
75
+        ArrayList<T> arrayList = new ArrayList<>();
76
+
77
+        for (T t : inputArray) {
78
+            if (!t.equals(valueToRemove)) {
79
+                arrayList.add(t);
80
+            }
81
+        }
82
+
83
+        inputArray = (T[]) Array.newInstance(inputArray.getClass().getComponentType(), arrayList.size());
84
+
85
+        return arrayList.toArray(inputArray);
86
+    }
87
+
88
+}

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Просмотреть файл

@@ -1,6 +1,6 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
-import com.zipcodewilmington.UnitTestingUtils;
3
+//import com.zipcodewilmington.UnitTestingUtils;
4 4
 import org.junit.Test;
5 5
 
6 6
 /**