mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
几个小优化:
1.优化CollUtil.map()方法, 减少流中的无意义操作; 2.优化ArrayUtil.get()方法, 使用合理的逻辑判断 代替 使用异常判断null值;
This commit is contained in:
parent
b7d91489f4
commit
ab39b7410f
@ -1066,12 +1066,17 @@ public class CollUtil {
|
|||||||
* @since 5.3.5
|
* @since 5.3.5
|
||||||
*/
|
*/
|
||||||
public static <T, R> List<R> map(final Iterable<T> collection, final Function<? super T, ? extends R> mapper, final boolean ignoreNull) {
|
public static <T, R> List<R> map(final Iterable<T> collection, final Function<? super T, ? extends R> mapper, final boolean ignoreNull) {
|
||||||
|
if (ignoreNull) {
|
||||||
|
return StreamUtil.of(collection)
|
||||||
|
// 检查映射前的结果
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.map(mapper)
|
||||||
|
// 检查映射后的结果
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
return StreamUtil.of(collection)
|
return StreamUtil.of(collection)
|
||||||
// 检查映射前的结果
|
|
||||||
.filter((e) -> (false == ignoreNull) || null != e)
|
|
||||||
.map(mapper)
|
.map(mapper)
|
||||||
// 检查映射后的结果
|
|
||||||
.filter((e) -> (false == ignoreNull) || null != e)
|
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -433,7 +433,7 @@ public class ListUtil {
|
|||||||
* @param element 新元素
|
* @param element 新元素
|
||||||
* @param paddingElement 填充的值
|
* @param paddingElement 填充的值
|
||||||
* @return 原List
|
* @return 原List
|
||||||
* @since 5。8.4
|
* @since 5.8.4
|
||||||
*/
|
*/
|
||||||
public static <T> List<T> setOrPadding(final List<T> list, final int index, final T element, final T paddingElement) {
|
public static <T> List<T> setOrPadding(final List<T> list, final int index, final T element, final T paddingElement) {
|
||||||
Assert.notNull(list, "List must be not null !");
|
Assert.notNull(list, "List must be not null !");
|
||||||
|
@ -1036,15 +1036,14 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
if (null == array) {
|
if (null == array) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
final int length = Array.getLength(array);
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
index += Array.getLength(array);
|
index += length;
|
||||||
}
|
}
|
||||||
try {
|
if (index < 0 || index >= length) {
|
||||||
return (T) Array.get(array, index);
|
|
||||||
} catch (final ArrayIndexOutOfBoundsException e) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return (T) Array.get(array, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -147,7 +147,7 @@ public class GraphTest {
|
|||||||
Assert.assertEquals(asSet(2, 0), graph.getAdjacentPoints(3));
|
Assert.assertEquals(asSet(2, 0), graph.getAdjacentPoints(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
private static <T> Set<T> asSet(T... ts) {
|
private static <T> Set<T> asSet(T... ts) {
|
||||||
return new LinkedHashSet<>(Arrays.asList(ts));
|
return new LinkedHashSet<>(Arrays.asList(ts));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user