几个小优化:

1.优化CollUtil.map()方法, 减少流中的无意义操作;
2.优化ArrayUtil.get()方法, 使用合理的逻辑判断 代替 使用异常判断null值;
This commit is contained in:
Zjp 2022-11-10 17:27:28 +08:00
parent b7d91489f4
commit ab39b7410f
4 changed files with 15 additions and 11 deletions

View File

@ -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) return StreamUtil.of(collection)
// 检查映射前的结果 // 检查映射前的结果
.filter((e) -> (false == ignoreNull) || null != e) .filter(Objects::nonNull)
.map(mapper) .map(mapper)
// 检查映射后的结果 // 检查映射后的结果
.filter((e) -> (false == ignoreNull) || null != e) .filter(Objects::nonNull)
.collect(Collectors.toList());
}
return StreamUtil.of(collection)
.map(mapper)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@ -433,7 +433,7 @@ public class ListUtil {
* @param element 新元素 * @param element 新元素
* @param paddingElement 填充的值 * @param paddingElement 填充的值
* @return 原List * @return 原List
* @since 58.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 !");

View File

@ -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);
} }
/** /**

View File

@ -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));
} }