删除集合转 Map 的工具方法,使用流即可。

dev
ZhouXY108 2024-03-02 23:25:23 +08:00
parent e5c2ba99c3
commit fe190d8f43
2 changed files with 2 additions and 127 deletions

View File

@ -1,17 +1,10 @@
package xyz.zhouxy.plusone.commons.collection; package xyz.zhouxy.plusone.commons.collection;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
public class CollectionTools { public class CollectionTools {
// isEmpty // isEmpty
@ -34,123 +27,6 @@ public class CollectionTools {
return map != null && !map.isEmpty(); return map != null && !map.isEmpty();
} }
// Collection -> Map
public static <K, V> HashMap<K, V> toHashMap( // NOSONAR return implementation
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( // NOSONAR return implementation
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( // NOSONAR return implementation
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( // NOSONAR return implementation
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( // NOSONAR return implementation
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( // NOSONAR return implementation
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( // NOSONAR return implementation
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( // NOSONAR return implementation
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);
}
private CollectionTools() { private CollectionTools() {
throw new IllegalStateException("Utility class"); throw new IllegalStateException("Utility class");
} }

View File

@ -8,8 +8,6 @@ import java.util.function.BiConsumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
public class TreeBuilder<T, TSubTree extends T, TIdentity> { public class TreeBuilder<T, TSubTree extends T, TIdentity> {
private final Function<T, TIdentity> identityGetter; private final Function<T, TIdentity> identityGetter;
private final Function<T, Optional<TIdentity>> parentIdentityGetter; private final Function<T, Optional<TIdentity>> parentIdentityGetter;
@ -23,7 +21,8 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
} }
public List<T> buildTree(Collection<T> nodes) { public List<T> buildTree(Collection<T> nodes) {
Map<TIdentity, T> identityNodeMap = CollectionTools.toHashMap(nodes, identityGetter); Map<TIdentity, T> identityNodeMap = nodes.stream()
.collect(Collectors.toMap(identityGetter, Function.identity(), (n1, n2) -> n1));
List<T> result = nodes.stream() List<T> result = nodes.stream()
.filter(node -> !this.parentIdentityGetter.apply(node).isPresent()) .filter(node -> !this.parentIdentityGetter.apply(node).isPresent())
.collect(Collectors.toList()); .collect(Collectors.toList());