forked from plusone/plusone-commons
调整集合相关工具类。
This commit is contained in:
@@ -79,21 +79,6 @@ public abstract class AbstractMapWrapper<K, V, T extends AbstractMapWrapper<K, V
|
||||
return Optional.ofNullable(this.map.get(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 {@code map} 中的值。如果 {@code key} 不存在,则抛出异常。
|
||||
*
|
||||
* @param key 键
|
||||
* @return 值
|
||||
* @throws IllegalArgumentException key 不存在时抛出。
|
||||
*/
|
||||
@Nullable
|
||||
public V getOrNull(K key) {
|
||||
if (!this.map.containsKey(key)) {
|
||||
throw new IllegalArgumentException("Key does not exist");
|
||||
}
|
||||
return this.map.get(key);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public final <R> Optional<R> getAndConvert(K key) {
|
||||
return get(key).map(v -> (R) v);
|
||||
|
@@ -0,0 +1,101 @@
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
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;
|
||||
|
||||
public class CollectionTools {
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
private CollectionTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ListTools {
|
||||
|
||||
public static <T> void transformValue(List<T> list, int index, Function<T, ? extends T> func) {
|
||||
list.set(index, func.apply(list.get(index)));
|
||||
}
|
||||
|
||||
private ListTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class MapTools {
|
||||
|
||||
public static <K, V> void transformValue(Map<K, V> map, K key, Function<V, ? extends V> func) {
|
||||
if (map.containsKey(key)) {
|
||||
map.put(key, func.apply(map.get(key)));
|
||||
}
|
||||
}
|
||||
|
||||
private MapTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
import com.google.common.collect.Table;
|
||||
|
||||
public class TableTools {
|
||||
|
||||
public static <R, C, V> Table<R, C, V> synchronizedTable(Table<R, C, V> t) {
|
||||
if (t instanceof SynchronizedTable) {
|
||||
return t;
|
||||
}
|
||||
return SynchronizedTable.of(t);
|
||||
}
|
||||
|
||||
private TableTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user