This commit is contained in:
Looly 2022-11-17 01:03:42 +08:00
parent 97e0243529
commit 93c6b7e3e7
4 changed files with 26 additions and 23 deletions

View File

@ -1078,14 +1078,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()); .collect(Collectors.toList());
} }
return StreamUtil.of(collection).map(mapper).collect(Collectors.toList());
}
/** /**
* 获取给定Bean列表中指定字段名对应字段值的列表<br> * 获取给定Bean列表中指定字段名对应字段值的列表<br>

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

@ -1028,7 +1028,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @param <T> 数组元素类型 * @param <T> 数组元素类型
* @param array 数组对象 * @param array 数组对象
* @param index 下标支持负数 * @param index 下标支持负数
* @return * @return 越界返回null
* @since 4.0.6 * @since 4.0.6
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -1037,14 +1037,14 @@ public class ArrayUtil extends PrimitiveArrayUtil {
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

@ -15,7 +15,7 @@ public class GraphTest {
@Test @Test
public void testPutEdge() { public void testPutEdge() {
Graph<Integer> graph = new Graph<>(); final Graph<Integer> graph = new Graph<>();
graph.putEdge(0, 1); graph.putEdge(0, 1);
graph.putEdge(1, 2); graph.putEdge(1, 2);
graph.putEdge(2, 0); graph.putEdge(2, 0);
@ -30,7 +30,7 @@ public class GraphTest {
// 0 -- 1 // 0 -- 1
// | | // | |
// 3 -- 2 // 3 -- 2
Graph<Integer> graph = new Graph<>(); final Graph<Integer> graph = new Graph<>();
graph.putEdge(0, 1); graph.putEdge(0, 1);
graph.putEdge(1, 2); graph.putEdge(1, 2);
graph.putEdge(2, 3); graph.putEdge(2, 3);
@ -53,7 +53,7 @@ public class GraphTest {
@Test @Test
public void removeEdge() { public void removeEdge() {
Graph<Integer> graph = new Graph<>(); final Graph<Integer> graph = new Graph<>();
graph.putEdge(0, 1); graph.putEdge(0, 1);
Assert.assertTrue(graph.containsEdge(0, 1)); Assert.assertTrue(graph.containsEdge(0, 1));
@ -66,7 +66,7 @@ public class GraphTest {
// 0 -- 1 // 0 -- 1
// | | // | |
// 3 -- 2 // 3 -- 2
Graph<Integer> graph = new Graph<>(); final Graph<Integer> graph = new Graph<>();
graph.putEdge(0, 1); graph.putEdge(0, 1);
graph.putEdge(1, 2); graph.putEdge(1, 2);
graph.putEdge(2, 3); graph.putEdge(2, 3);
@ -87,7 +87,7 @@ public class GraphTest {
// 0 -- 1 // 0 -- 1
// | | // | |
// 3 -- 2 // 3 -- 2
Graph<Integer> graph = new Graph<>(); final Graph<Integer> graph = new Graph<>();
graph.putEdge(0, 1); graph.putEdge(0, 1);
graph.putEdge(1, 2); graph.putEdge(1, 2);
graph.putEdge(2, 3); graph.putEdge(2, 3);
@ -113,7 +113,7 @@ public class GraphTest {
// 0 -- 1 // 0 -- 1
// | | // | |
// 3 -- 2 // 3 -- 2
Graph<Integer> graph = new Graph<>(); final Graph<Integer> graph = new Graph<>();
graph.putEdge(0, 1); graph.putEdge(0, 1);
graph.putEdge(1, 2); graph.putEdge(1, 2);
graph.putEdge(2, 3); graph.putEdge(2, 3);
@ -130,7 +130,7 @@ public class GraphTest {
// 0 -- 1 // 0 -- 1
// | | // | |
// 3 -- 2 // 3 -- 2
Graph<Integer> graph = new Graph<>(); final Graph<Integer> graph = new Graph<>();
graph.putEdge(0, 1); graph.putEdge(0, 1);
graph.putEdge(1, 2); graph.putEdge(1, 2);
graph.putEdge(2, 3); graph.putEdge(2, 3);
@ -147,8 +147,8 @@ 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(final T... ts) {
return new LinkedHashSet<>(Arrays.asList(ts)); return new LinkedHashSet<>(Arrays.asList(ts));
} }