forked from plusone/plusone-commons
调整集合相关工具类。
This commit is contained in:
247
src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java
Normal file
247
src/main/java/xyz/zhouxy/plusone/commons/util/ArrayTools.java
Normal file
@@ -0,0 +1,247 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
||||
|
||||
@Beta
|
||||
public class ArrayTools {
|
||||
|
||||
// isNullOrEmpty
|
||||
|
||||
public static <T> boolean isNullOrEmpty(T[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - float
|
||||
public static boolean isNullOrEmpty(float[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - double
|
||||
public static boolean isNullOrEmpty(double[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - byte
|
||||
public static boolean isNullOrEmpty(byte[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - long
|
||||
public static boolean isNullOrEmpty(long[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNullOrEmpty - int
|
||||
public static boolean isNullOrEmpty(int[] arr) {
|
||||
return arr == null || arr.length == 0;
|
||||
}
|
||||
|
||||
// isNotEmpty
|
||||
|
||||
public static <T> boolean isNotEmpty(T[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - float
|
||||
public static boolean isNotEmpty(float[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - double
|
||||
public static boolean isNotEmpty(double[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - byte
|
||||
public static boolean isNotEmpty(byte[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - long
|
||||
public static boolean isNotEmpty(long[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// isNotEmpty - int
|
||||
public static boolean isNotEmpty(int[] arr) {
|
||||
return arr != null && arr.length > 0;
|
||||
}
|
||||
|
||||
// concat
|
||||
|
||||
public static float[] concatFloatArray(Collection<float[]> arrays) {
|
||||
int length = 0;
|
||||
for (float[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
float[] result = new float[length];
|
||||
int i = 0;
|
||||
for (float[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static double[] concatDoubleArray(Collection<double[]> arrays) {
|
||||
int length = 0;
|
||||
for (double[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
double[] result = new double[length];
|
||||
int i = 0;
|
||||
for (double[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] concatByteArray(Collection<byte[]> arrays) {
|
||||
int length = 0;
|
||||
for (byte[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
byte[] result = new byte[length];
|
||||
int i = 0;
|
||||
for (byte[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long[] concatLongArray(Collection<long[]> arrays) {
|
||||
int length = 0;
|
||||
for (long[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
long[] result = new long[length];
|
||||
int i = 0;
|
||||
for (long[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int[] concatIntArray(Collection<int[]> arrays) {
|
||||
int length = 0;
|
||||
for (int[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
int[] result = new int[length];
|
||||
int i = 0;
|
||||
for (int[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> List<T> concatToList(@Nullable Collection<T[]> arrays) {
|
||||
if (arrays == null || arrays.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
int length = 0;
|
||||
for (T[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
final List<T> result = new ArrayList<>(length);
|
||||
for (T[] arr : arrays) {
|
||||
Collections.addAll(result, arr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// array -> map
|
||||
|
||||
public static <K, V> HashMap<K, V> toHashMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
int initialCapacity) {
|
||||
HashMap<K, V> map = new HashMap<>(initialCapacity);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> HashMap<K, V> toHashMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
return toHashMap(c, keyGenerator, c.length);
|
||||
}
|
||||
|
||||
public static <K, V> SafeConcurrentHashMap<K, V> toConcurrentHashMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
int initialCapacity) {
|
||||
SafeConcurrentHashMap<K, V> map = new SafeConcurrentHashMap<>(initialCapacity);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> SafeConcurrentHashMap<K, V> toConcurrentHashMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
return toConcurrentHashMap(c, keyGenerator, c.length);
|
||||
}
|
||||
|
||||
public static <K extends Comparable<? super K>, V> TreeMap<K, V> toTreeMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
TreeMap<K, V> map = new TreeMap<>();
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> TreeMap<K, V> toTreeMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
Comparator<? super K> keyComparator) {
|
||||
TreeMap<K, V> map = new TreeMap<>(keyComparator);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> void fillIntoEmptyMap(
|
||||
Map<K, ? super V> map, V[] c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
CollectionTools.fillIntoEmptyMap(map, Arrays.asList(c), keyGenerator);
|
||||
}
|
||||
|
||||
private ArrayTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -1,119 +0,0 @@
|
||||
/*
|
||||
* Copyright 2022-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
@Beta
|
||||
public class MoreArrays {
|
||||
|
||||
public static float[] concatFloatArray(Collection<float[]> arrays) {
|
||||
int length = 0;
|
||||
for (float[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
float[] result = new float[length];
|
||||
int i = 0;
|
||||
for (float[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static double[] concatDoubleArray(Collection<double[]> arrays) {
|
||||
int length = 0;
|
||||
for (double[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
double[] result = new double[length];
|
||||
int i = 0;
|
||||
for (double[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] concatByteArray(Collection<byte[]> arrays) {
|
||||
int length = 0;
|
||||
for (byte[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
byte[] result = new byte[length];
|
||||
int i = 0;
|
||||
for (byte[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static long[] concatLongArray(Collection<long[]> arrays) {
|
||||
int length = 0;
|
||||
for (long[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
long[] result = new long[length];
|
||||
int i = 0;
|
||||
for (long[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static int[] concatIntArray(Collection<int[]> arrays) {
|
||||
int length = 0;
|
||||
for (int[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
int[] result = new int[length];
|
||||
int i = 0;
|
||||
for (int[] arr : arrays) {
|
||||
System.arraycopy(arr, 0, result, i, arr.length);
|
||||
i = arr.length;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static <T> List<T> concatToList(@Nullable Collection<T[]> arrays) {
|
||||
if (arrays == null || arrays.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
int length = 0;
|
||||
for (T[] arr : arrays) {
|
||||
length += arr.length;
|
||||
}
|
||||
final List<T> result = new ArrayList<>(length);
|
||||
for (T[] arr : arrays) {
|
||||
Collections.addAll(result, arr);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private MoreArrays() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -1,169 +0,0 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.Table;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
||||
import xyz.zhouxy.plusone.commons.collection.SynchronizedTable;
|
||||
|
||||
public class MoreCollections {
|
||||
|
||||
// isEmpty
|
||||
|
||||
public static boolean isEmpty(@Nullable Collection<?> collection) {
|
||||
return collection == null || collection.isEmpty();
|
||||
}
|
||||
|
||||
public static boolean isEmpty(@Nullable Map<?, ?> map) {
|
||||
return map == null || map.isEmpty();
|
||||
}
|
||||
|
||||
// isNotEmpty
|
||||
|
||||
public static boolean isNotEmpty(@Nullable Collection<?> collection) {
|
||||
return collection != null && !collection.isEmpty();
|
||||
}
|
||||
|
||||
public static boolean isNotEmpty(@Nullable Map<?, ?> map) {
|
||||
return map != null && !map.isEmpty();
|
||||
}
|
||||
|
||||
// Collection -> Map
|
||||
|
||||
public static <K, V> HashMap<K, V> toHashMap(
|
||||
Iterable<V> c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
int initialCapacity) {
|
||||
HashMap<K, V> map = new HashMap<>(initialCapacity);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> HashMap<K, V> toHashMap(
|
||||
Collection<V> c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
return toHashMap(c, keyGenerator, c.size());
|
||||
}
|
||||
|
||||
public static <K, V> SafeConcurrentHashMap<K, V> toConcurrentHashMap(
|
||||
Iterable<V> c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
int initialCapacity) {
|
||||
SafeConcurrentHashMap<K, V> map = new SafeConcurrentHashMap<>(initialCapacity);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> SafeConcurrentHashMap<K, V> toConcurrentHashMap(
|
||||
Collection<V> c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
return toConcurrentHashMap(c, keyGenerator, c.size());
|
||||
}
|
||||
|
||||
public static <K extends Comparable<? super K>, V> TreeMap<K, V> toTreeMap(
|
||||
Iterable<V> c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
TreeMap<K, V> map = new TreeMap<>();
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> TreeMap<K, V> toTreeMap(
|
||||
Iterable<V> c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
Comparator<? super K> keycComparator) {
|
||||
TreeMap<K, V> map = new TreeMap<>(keycComparator);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> void fillIntoEmptyMap(
|
||||
Map<K, ? super V> map,
|
||||
Iterable<V> c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
Preconditions.checkNotNull(map);
|
||||
Preconditions.checkNotNull(c);
|
||||
Preconditions.checkNotNull(keyGenerator);
|
||||
Preconditions.checkArgument(map.isEmpty(), "The map should be empty.");
|
||||
for (V v : c) {
|
||||
map.put(keyGenerator.apply(v), v);
|
||||
}
|
||||
}
|
||||
|
||||
// array -> map
|
||||
|
||||
public static <K, V> HashMap<K, V> toHashMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
int initialCapacity) {
|
||||
HashMap<K, V> map = new HashMap<>(initialCapacity);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> HashMap<K, V> toHashMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
return toHashMap(c, keyGenerator, c.length);
|
||||
}
|
||||
|
||||
public static <K, V> SafeConcurrentHashMap<K, V> toConcurrentHashMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
int initialCapacity) {
|
||||
SafeConcurrentHashMap<K, V> map = new SafeConcurrentHashMap<>(initialCapacity);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> SafeConcurrentHashMap<K, V> toConcurrentHashMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
return toConcurrentHashMap(c, keyGenerator, c.length);
|
||||
}
|
||||
|
||||
public static <K extends Comparable<? super K>, V> TreeMap<K, V> toTreeMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
TreeMap<K, V> map = new TreeMap<>();
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> TreeMap<K, V> toTreeMap(
|
||||
V[] c,
|
||||
Function<? super V, K> keyGenerator,
|
||||
Comparator<? super K> keyComparator) {
|
||||
TreeMap<K, V> map = new TreeMap<>(keyComparator);
|
||||
fillIntoEmptyMap(map, c, keyGenerator);
|
||||
return map;
|
||||
}
|
||||
|
||||
public static <K, V> void fillIntoEmptyMap(
|
||||
Map<K, ? super V> map, V[] c,
|
||||
Function<? super V, K> keyGenerator) {
|
||||
fillIntoEmptyMap(map, Arrays.asList(c), keyGenerator);
|
||||
}
|
||||
|
||||
public static <R, C, V> Table<R, C, V> synchronizedTable(Table<R, C, V> t) {
|
||||
if (t instanceof SynchronizedTable) {
|
||||
return t;
|
||||
} else {
|
||||
return SynchronizedTable.of(t);
|
||||
}
|
||||
}
|
||||
|
||||
private MoreCollections() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -8,6 +8,8 @@ import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
|
||||
public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||
private final Collection<T> nodes;
|
||||
private final Function<T, TIdentity> identityGetter;
|
||||
@@ -23,7 +25,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||
}
|
||||
|
||||
public List<T> buildTree() {
|
||||
Map<TIdentity, T> identityNodeMap = MoreCollections.toHashMap(nodes, identityGetter);
|
||||
Map<TIdentity, T> identityNodeMap = CollectionTools.toHashMap(nodes, identityGetter);
|
||||
List<T> result = this.nodes.stream()
|
||||
.filter(node -> !this.parentIdentityGetter.apply(node).isPresent())
|
||||
.collect(Collectors.toList());
|
||||
|
Reference in New Issue
Block a user