Skip to content

Commit a8db981

Browse files
committed
add new methods to array utils
1 parent 98c0064 commit a8db981

1 file changed

Lines changed: 43 additions & 3 deletions

File tree

rlib-common/src/main/java/com/ss/rlib/common/util/ArrayUtils.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import org.jetbrains.annotations.NotNull;
88
import org.jetbrains.annotations.Nullable;
99

10-
import java.util.Arrays;
11-
import java.util.Comparator;
12-
import java.util.Objects;
10+
import java.util.*;
1311
import java.util.function.*;
1412

1513
/**
@@ -244,6 +242,48 @@ public static <T, F> void fill(@NotNull T[] array, @Nullable F argument, @NotNul
244242
return result;
245243
}
246244

245+
/**
246+
* Combine two arrays to one single array with uniq elements.
247+
*
248+
* @param <T> the base array's component type.
249+
* @param <E> the added array's component type.
250+
* @param base the base array.
251+
* @param added the additional array.
252+
* @return the combined array with uniq elements.
253+
*/
254+
public static <T, E extends T> @NotNull T[] combineUniq(@NotNull T[] base, @Nullable E[] added) {
255+
return combineUniq(base, added, getComponentType(base));
256+
}
257+
258+
/**
259+
* Combine two arrays to one single array with uniq elements.
260+
*
261+
* @param <T> the base array's component type.
262+
* @param <E> the added array's component type.
263+
* @param base the base array.
264+
* @param added the additional array.
265+
* @param type the base array's component type.
266+
* @return the combined array with uniq elements.
267+
*/
268+
public static <T, E extends T> @NotNull T[] combineUniq(
269+
@Nullable T[] base,
270+
@Nullable E[] added,
271+
@NotNull Class<T> type
272+
) {
273+
274+
if (base == null) {
275+
return added == null ? create(type, 0) : added;
276+
} else if (added == null || added.length < 1) {
277+
return base;
278+
}
279+
280+
var result = new HashSet<T>(base.length + added.length);
281+
result.addAll(Arrays.asList(base));
282+
result.addAll(Arrays.asList(added));
283+
284+
return result.toArray(create(type, result.size()));
285+
}
286+
247287
/**
248288
* Check the array on contains the value.
249289
*

0 commit comments

Comments
 (0)