102 lines
3.0 KiB
Java
Raw Normal View History

2024-02-07 09:27:42 +08:00
package xyz.zhouxy.plusone.commons.collection;
2023-04-29 18:38:14 +08:00
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;
2023-04-29 18:38:14 +08:00
import javax.annotation.Nullable;
2023-07-06 10:07:37 +08:00
import com.google.common.base.Preconditions;
2024-02-07 09:27:42 +08:00
public class CollectionTools {
// isEmpty
2023-04-29 18:38:14 +08:00
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
2023-04-29 18:38:14 +08:00
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
2023-07-06 10:07:37 +08:00
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;
}
2023-07-06 10:07:37 +08:00
public static <K, V> HashMap<K, V> toHashMap(
Collection<V> c,
Function<? super V, K> keyGenerator) {
return toHashMap(c, keyGenerator, c.size());
}
2023-07-06 10:07:37 +08:00
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;
}
2023-07-06 10:07:37 +08:00
public static <K, V> SafeConcurrentHashMap<K, V> toConcurrentHashMap(
Collection<V> c,
Function<? super V, K> keyGenerator) {
return toConcurrentHashMap(c, keyGenerator, c.size());
}
2023-07-06 10:07:37 +08:00
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;
}
2023-07-06 10:07:37 +08:00
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;
}
2023-07-06 10:07:37 +08:00
public static <K, V> void fillIntoEmptyMap(
Map<K, ? super V> map,
Iterable<V> c,
Function<? super V, K> keyGenerator) {
2023-07-06 10:07:37 +08:00
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);
}
}
2024-02-07 09:27:42 +08:00
private CollectionTools() {
2023-04-29 18:38:14 +08:00
throw new IllegalStateException("Utility class");
}
}