Skip to content

Commit 81a7e19

Browse files
rozagerardomaibin
authored andcommitted
[BAEL-2235] | core-java | Common Array Operations in Java (eugenp#5599)
* * added all array operations and tests * fixed details, adn added some more methods to java array operations examples * * moved arrays operations package to core-java-collections submodule
1 parent c7b7d94 commit 81a7e19

3 files changed

Lines changed: 563 additions & 1 deletion

File tree

‎core-java-collections/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<properties>
6262
<openjdk.jmh.version>1.19</openjdk.jmh.version>
6363
<junit.platform.version>1.2.0</junit.platform.version>
64-
<commons-lang3.version>3.5</commons-lang3.version>
64+
<commons-lang3.version>3.8.1</commons-lang3.version>
6565
<commons-collections4.version>4.1</commons-collections4.version>
6666
<collections-generic.version>4.01</collections-generic.version>
6767
<avaitility.version>1.7.0</avaitility.version>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
package com.baeldung.array.operations;
2+
3+
import java.lang.reflect.Array;
4+
import java.util.Arrays;
5+
import java.util.HashSet;
6+
import java.util.LinkedHashSet;
7+
import java.util.Random;
8+
import java.util.Set;
9+
import java.util.function.Function;
10+
import java.util.function.IntPredicate;
11+
import java.util.function.Predicate;
12+
13+
import org.apache.commons.lang3.ArrayUtils;
14+
15+
public class ArrayOperations {
16+
17+
// Get the first and last item of an array
18+
public static <T> T getFirstObject(T[] array) {
19+
return array[0];
20+
}
21+
22+
public static int getFirstInt(int[] array) {
23+
return array[0];
24+
}
25+
26+
public static <T> T getLastObject(T[] array) {
27+
return array[array.length - 1];
28+
}
29+
30+
public static int getLastInt(int[] array) {
31+
return array[array.length - 1];
32+
}
33+
34+
// Append a new item to an array
35+
public static <T> T[] appendObject(T[] array, T newItem) {
36+
return ArrayUtils.add(array, newItem);
37+
}
38+
39+
public static int[] appendInt(int[] array, int newItem) {
40+
int[] newArray = Arrays.copyOf(array, array.length + 1);
41+
newArray[newArray.length - 1] = newItem;
42+
return newArray;
43+
}
44+
45+
public static int[] appendIntWithUtils(int[] array, int newItem) {
46+
return ArrayUtils.add(array, newItem);
47+
}
48+
49+
// Compare two arrays to check if they have the same elements
50+
public static <T> boolean compareObjectArrays(T[] array1, T[] array2) {
51+
return Arrays.equals(array1, array2);
52+
}
53+
54+
public static boolean compareIntArrays(int[] array1, int[] array2) {
55+
return Arrays.equals(array1, array2);
56+
}
57+
58+
// Deep Compare (for nested arrays)
59+
public static <T> boolean deepCompareObjectArrayUsingArrays(T[][] array1, T[][] array2) {
60+
// We can use Objects.deepEquals for a broader approach
61+
return Arrays.deepEquals(array1, array2);
62+
}
63+
64+
public static boolean deepCompareIntArrayUsingArrays(int[][] array1, int[][] array2) {
65+
return Arrays.deepEquals(array1, array2);
66+
}
67+
68+
// Check if array is empty
69+
public static <T> boolean isEmptyObjectArrayUsingUtils(T[] array1) {
70+
return ArrayUtils.isEmpty(array1);
71+
}
72+
73+
public static boolean isEmptyIntArrayUsingUtils(int[] array1) {
74+
return ArrayUtils.isEmpty(array1);
75+
}
76+
77+
// Remove duplicates
78+
public static Integer[] removeDuplicateObjects(Integer[] array) {
79+
// We can use other ways of converting the array to a set too
80+
Set<Integer> set = new HashSet<>(Arrays.asList(array));
81+
return set.toArray(new Integer[set.size()]);
82+
}
83+
84+
public static int[] removeDuplicateInts(int[] array) {
85+
// Box
86+
Integer[] list = ArrayUtils.toObject(array);
87+
// Remove duplicates
88+
Set<Integer> set = new HashSet<Integer>(Arrays.asList(list));
89+
// Create array and unbox
90+
return ArrayUtils.toPrimitive(set.toArray(new Integer[set.size()]));
91+
}
92+
93+
// Remove duplicates preserving the order
94+
public static Integer[] removeDuplicateWithOrderObjectArray(Integer[] array) {
95+
// We can use other ways of converting the array to a set too
96+
Set<Integer> set = new LinkedHashSet<>(Arrays.asList(array));
97+
return set.toArray(new Integer[set.size()]);
98+
}
99+
100+
public static int[] removeDuplicateWithOrderIntArray(int[] array) {
101+
// Box
102+
Integer[] list = ArrayUtils.toObject(array);
103+
// Remove duplicates
104+
Set<Integer> set = new LinkedHashSet<Integer>(Arrays.asList(list));
105+
// Create array and unbox
106+
return ArrayUtils.toPrimitive(set.toArray(new Integer[set.size()]));
107+
}
108+
109+
// Print an array
110+
public static String printObjectArray(Integer[] array) {
111+
return ArrayUtils.toString(array);
112+
}
113+
114+
public static String printObjectArray(Integer[][] array) {
115+
return ArrayUtils.toString(array);
116+
}
117+
118+
public static String printIntArray(int[] array) {
119+
return ArrayUtils.toString(array);
120+
}
121+
122+
public static String printIntArray(int[][] array) {
123+
return ArrayUtils.toString(array);
124+
}
125+
126+
// Box or Unbox values
127+
public static int[] unboxIntegerArray(Integer[] array) {
128+
return ArrayUtils.toPrimitive(array);
129+
}
130+
131+
public static Integer[] boxIntArray(int[] array) {
132+
return ArrayUtils.toObject(array);
133+
}
134+
135+
// Map array values
136+
@SuppressWarnings("unchecked")
137+
public static <T, U> U[] mapObjectArray(T[] array, Function<T, U> function, Class<U> targetClazz) {
138+
U[] newArray = (U[]) Array.newInstance(targetClazz, array.length);
139+
for (int i = 0; i < array.length; i++) {
140+
newArray[i] = function.apply(array[i]);
141+
}
142+
return newArray;
143+
}
144+
145+
public static String[] mapIntArrayToString(int[] array) {
146+
return Arrays.stream(array)
147+
.mapToObj(value -> String.format("Value: %s", value))
148+
.toArray(String[]::new);
149+
}
150+
151+
// Filter array values
152+
public static Integer[] filterObjectArray(Integer[] array, Predicate<Integer> predicate) {
153+
return Arrays.stream(array)
154+
.filter(predicate)
155+
.toArray(Integer[]::new);
156+
}
157+
158+
public static int[] filterIntArray(int[] array, IntPredicate predicate) {
159+
return Arrays.stream(array)
160+
.filter(predicate)
161+
.toArray();
162+
}
163+
164+
// Insert item between others
165+
public static int[] insertBetweenIntArray(int[] array, int... values) {
166+
return ArrayUtils.insert(2, array, values);
167+
}
168+
169+
@SuppressWarnings("unchecked")
170+
public static <T> T[] insertBetweenObjectArray(T[] array, T... values) {
171+
return ArrayUtils.insert(2, array, values);
172+
}
173+
174+
// Shuffling arrays
175+
public static int[] shuffleIntArray(int[] array) {
176+
// we create a different array instance for testing purposes
177+
int[] shuffled = Arrays.copyOf(array, array.length);
178+
ArrayUtils.shuffle(shuffled);
179+
return shuffled;
180+
}
181+
182+
public static <T> T[] shuffleObjectArray(T[] array) {
183+
// we create a different array instance for testing purposes
184+
T[] shuffled = Arrays.copyOf(array, array.length);
185+
ArrayUtils.shuffle(shuffled);
186+
return shuffled;
187+
}
188+
189+
// Get random number
190+
public static int getRandomFromIntArray(int[] array) {
191+
return array[new Random().nextInt(array.length)];
192+
}
193+
194+
public static <T> T getRandomFromObjectArray(T[] array) {
195+
return array[new Random().nextInt(array.length)];
196+
}
197+
}

0 commit comments

Comments
 (0)