generated from mingliqiye/lib-tem
no message
All checks were successful
Gitea Actions Build / Build (push) Successful in 1m2s
All checks were successful
Gitea Actions Build / Build (push) Successful in 1m2s
This commit is contained in:
parent
9d118ed98d
commit
d12fbe0cce
@ -22,4 +22,4 @@
|
|||||||
JDKVERSIONS=1.8
|
JDKVERSIONS=1.8
|
||||||
GROUPSID=com.mingliqiye.utils
|
GROUPSID=com.mingliqiye.utils
|
||||||
ARTIFACTID=mingli-utils
|
ARTIFACTID=mingli-utils
|
||||||
VERSIONS=3.2.5
|
VERSIONS=3.2.6
|
||||||
|
|||||||
@ -25,11 +25,9 @@ package com.mingliqiye.utils.collection;
|
|||||||
import com.mingliqiye.utils.functions.P1Function;
|
import com.mingliqiye.utils.functions.P1Function;
|
||||||
import com.mingliqiye.utils.functions.P2Function;
|
import com.mingliqiye.utils.functions.P2Function;
|
||||||
import com.mingliqiye.utils.functions.P3Function;
|
import com.mingliqiye.utils.functions.P3Function;
|
||||||
import com.mingliqiye.utils.stream.SuperStream;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
import java.util.function.Function;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 集合和映射的增强遍历功能
|
* 集合和映射的增强遍历功能
|
||||||
@ -46,33 +44,33 @@ import java.util.function.Function;
|
|||||||
public class ForEach {
|
public class ForEach {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对给定的集合执行指定的操作,操作包含元素值和索引。
|
* 对给定的可迭代对象执行指定的操作,操作包含元素值和索引。
|
||||||
* 根据集合类型选择最优的遍历方式以提高性能。
|
* 根据可迭代对象类型选择最优的遍历方式以提高性能。
|
||||||
*
|
*
|
||||||
* @param collection 要遍历的集合,可以是 List 或其他 Collection 实现
|
* @param iterable 要遍历的可迭代对象
|
||||||
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数
|
||||||
* @param <T> 集合中元素的类型
|
* @param <T> 可迭代对象中元素的类型
|
||||||
*/
|
*/
|
||||||
public static <T> void forEach(
|
public static <T> void forEach(
|
||||||
Collection<T> collection,
|
Iterable<T> iterable,
|
||||||
P2Function<? super T, Integer> action
|
P2Function<? super T, Integer> action
|
||||||
) {
|
) {
|
||||||
// 参数校验:如果集合或操作为空,则直接返回
|
// 参数校验:如果集合或操作为空,则直接返回
|
||||||
if (collection == null || action == null) {
|
if (iterable == null || action == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果集合实现了 RandomAccess 接口(如 ArrayList),使用索引访问优化性能
|
// 如果集合实现了 RandomAccess 接口(如 ArrayList),使用索引访问优化性能
|
||||||
if (collection instanceof RandomAccess && collection instanceof List) {
|
if (iterable instanceof RandomAccess && iterable instanceof List) {
|
||||||
List<T> list = (List<T>) collection;
|
List<T> list = (List<T>) iterable;
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
action.call(list.get(i), i);
|
action.call(list.get(i), i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果是普通 List,使用迭代器遍历并手动维护索引
|
// 如果是普通 List,使用迭代器遍历并手动维护索引
|
||||||
else if (collection instanceof List) {
|
else if (iterable instanceof List) {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
Iterator<T> it = collection.iterator();
|
Iterator<T> it = iterable.iterator();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
action.call(it.next(), index);
|
action.call(it.next(), index);
|
||||||
index++;
|
index++;
|
||||||
@ -81,7 +79,7 @@ public class ForEach {
|
|||||||
// 其他类型的集合使用增强 for 循环,并手动维护索引
|
// 其他类型的集合使用增强 for 循环,并手动维护索引
|
||||||
else {
|
else {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
for (T element : collection) {
|
for (T element : iterable) {
|
||||||
action.call(element, index);
|
action.call(element, index);
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
@ -89,37 +87,75 @@ public class ForEach {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对给定的集合执行指定的操作,仅处理元素值。
|
* 对给定的可迭代对象执行指定的操作,仅处理元素值。
|
||||||
* 根据集合是否实现 RandomAccess 接口选择最优的遍历方式。
|
* 根据可迭代对象是否实现 RandomAccess 接口选择最优的遍历方式。
|
||||||
*
|
*
|
||||||
* @param collection 要遍历的集合
|
* @param iterable 要遍历的可迭代对象
|
||||||
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
* @param <T> 集合中元素的类型
|
* @param <T> 可迭代对象中元素的类型
|
||||||
*/
|
*/
|
||||||
public static <T> void forEach(
|
public static <T> void forEach(
|
||||||
Collection<T> collection,
|
Iterable<T> iterable,
|
||||||
P1Function<? super T> action
|
P1Function<? super T> action
|
||||||
) {
|
) {
|
||||||
// 参数校验:如果集合或操作为空,则直接返回
|
// 参数校验:如果集合或操作为空,则直接返回
|
||||||
if (collection == null || action == null) {
|
if (iterable == null || action == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果集合实现了 RandomAccess 接口,使用索引访问提升性能
|
// 如果集合实现了 RandomAccess 接口,使用索引访问提升性能
|
||||||
if (collection instanceof RandomAccess) {
|
if (iterable instanceof RandomAccess) {
|
||||||
List<T> list = (List<T>) collection;
|
List<T> list = (List<T>) iterable;
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
action.call(list.get(i));
|
action.call(list.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 否则使用增强 for 循环进行遍历
|
// 否则使用增强 for 循环进行遍历
|
||||||
else {
|
else {
|
||||||
for (T element : collection) {
|
for (T element : iterable) {
|
||||||
action.call(element);
|
action.call(element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对给定的迭代器执行指定的操作,仅处理元素值。
|
||||||
|
* @param iterator 要遍历的迭代器
|
||||||
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
|
* @param <T> 迭代器中元素的类型
|
||||||
|
*/
|
||||||
|
public static <T> void forEach(
|
||||||
|
Iterator<T> iterator,
|
||||||
|
P2Function<? super T, Integer> action
|
||||||
|
) {
|
||||||
|
if (iterator == null || action == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int index = 0;
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
action.call(iterator.next(), index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对给定的迭代器执行指定的操作,处理元素值和索引。
|
||||||
|
* @param iterator 要遍历的迭代器
|
||||||
|
* @param action 要对每个元素执行的操作,只接收元素值作为参数
|
||||||
|
* @param <T> 迭代器中元素的类型
|
||||||
|
*/
|
||||||
|
public static <T> void forEach(
|
||||||
|
Iterator<T> iterator,
|
||||||
|
P1Function<? super T> action
|
||||||
|
) {
|
||||||
|
if (iterator == null || action == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
action.call(iterator.next());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对给定的映射执行指定的操作,操作包含键、值和索引。
|
* 对给定的映射执行指定的操作,操作包含键、值和索引。
|
||||||
* 根据映射类型选择不同的遍历策略。
|
* 根据映射类型选择不同的遍历策略。
|
||||||
|
|||||||
@ -25,6 +25,7 @@ package com.mingliqiye.utils.collection;
|
|||||||
import com.mingliqiye.utils.functions.P1RFunction;
|
import com.mingliqiye.utils.functions.P1RFunction;
|
||||||
import com.mingliqiye.utils.functions.P2RFunction;
|
import com.mingliqiye.utils.functions.P2RFunction;
|
||||||
import com.mingliqiye.utils.functions.P3RFunction;
|
import com.mingliqiye.utils.functions.P3RFunction;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
@ -202,8 +203,7 @@ public class ForEachBreaked {
|
|||||||
// 如果是 ConcurrentMap 或 LinkedHashMap,使用其内置的 forEach 方法
|
// 如果是 ConcurrentMap 或 LinkedHashMap,使用其内置的 forEach 方法
|
||||||
else if (map instanceof ConcurrentMap || map instanceof LinkedHashMap) {
|
else if (map instanceof ConcurrentMap || map instanceof LinkedHashMap) {
|
||||||
forEach(map.entrySet(), i -> {
|
forEach(map.entrySet(), i -> {
|
||||||
if (action.call(i.getKey(), i.getValue())) return true;
|
return action.call(i.getKey(), i.getValue());
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// 遍历其他类型映射的条目集合
|
// 遍历其他类型映射的条目集合
|
||||||
@ -244,6 +244,48 @@ public class ForEachBreaked {
|
|||||||
forEach(action, objects);
|
forEach(action, objects);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对迭代器执行指定的操作,操作包含元素值和索引
|
||||||
|
* 当操作返回 true 时,遍历将提前终止。
|
||||||
|
*
|
||||||
|
* @param iterator 要遍历的迭代器
|
||||||
|
* @param action 要对每个元素执行的操作,接收元素值和索引作为参数,返回 Boolean 值决定是否继续遍历
|
||||||
|
* @param <T> 数组中元素的类型
|
||||||
|
*/
|
||||||
|
public static <T> void forEach(
|
||||||
|
Iterator<T> iterator,
|
||||||
|
P2RFunction<? super T, Integer, Boolean> action
|
||||||
|
) {
|
||||||
|
if (iterator == null || action == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int index = 0;
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
if (action.call(iterator.next(), index)) return;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对迭代器执行指定的操作,操作包含元素值
|
||||||
|
* 当操作返回 true 时,遍历将提前终止。
|
||||||
|
*
|
||||||
|
* @param iterator 要遍历的迭代器
|
||||||
|
* @param action 要对每个元素执行的操作,接收元素值作为参数,返回 Boolean 值决定是否继续遍历
|
||||||
|
* @param <T> 数组中元素的类型
|
||||||
|
*/
|
||||||
|
public static <T> void forEach(
|
||||||
|
Iterator<T> iterator,
|
||||||
|
P1RFunction<? super T, Boolean> action
|
||||||
|
) {
|
||||||
|
if (iterator == null || action == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
if (action.call(iterator.next())) return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对数组执行指定的操作,仅处理元素值
|
* 对数组执行指定的操作,仅处理元素值
|
||||||
* 当操作返回 true 时,遍历将提前终止。
|
* 当操作返回 true 时,遍历将提前终止。
|
||||||
@ -272,8 +314,7 @@ public class ForEachBreaked {
|
|||||||
T... objects
|
T... objects
|
||||||
) {
|
) {
|
||||||
forEach(Lists.toList(objects), (t, i) -> {
|
forEach(Lists.toList(objects), (t, i) -> {
|
||||||
if (action.call(t)) return true;
|
return action.call(t);
|
||||||
return false;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
117
src/main/java/com/mingliqiye/utils/iterator/Range.java
Normal file
117
src/main/java/com/mingliqiye/utils/iterator/Range.java
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
package com.mingliqiye.utils.iterator;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 范围 Range<br>
|
||||||
|
* 类似 KT的 {@code 0..10 = Range.of(0,10)}
|
||||||
|
* @author MingLiPro
|
||||||
|
* @since 3.2.6
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public class Range implements Iterable<Integer> {
|
||||||
|
|
||||||
|
private final int start;
|
||||||
|
private final int end;
|
||||||
|
private int current;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个范围 <br>
|
||||||
|
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||||
|
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||||
|
* @param start 开始 (包含)
|
||||||
|
* @param end 完毕 (包含)
|
||||||
|
* @see Integer
|
||||||
|
*/
|
||||||
|
public Range(int start, int end) {
|
||||||
|
this.start = start;
|
||||||
|
this.current = start;
|
||||||
|
this.end = end + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个范围 {@code 0 - range}<br>
|
||||||
|
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||||
|
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||||
|
* @param range 完毕 (包含)
|
||||||
|
* @see Integer
|
||||||
|
*/
|
||||||
|
public Range(int range) {
|
||||||
|
this(0, range);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个范围 {@code 0 - range}<br>
|
||||||
|
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||||
|
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||||
|
* @param range 完毕 (包含)
|
||||||
|
* @see Integer
|
||||||
|
* @return Range 对象
|
||||||
|
*/
|
||||||
|
public static Range of(int range) {
|
||||||
|
return new Range(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个范围 <br>
|
||||||
|
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||||
|
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||||
|
* @param start 开始 (包含)
|
||||||
|
* @param end 完毕 (包含)
|
||||||
|
* @see Integer
|
||||||
|
* @return Range 对象
|
||||||
|
*/
|
||||||
|
public static Range of(int start, int end) {
|
||||||
|
return new Range(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个范围 {@code 0 - range}<br>
|
||||||
|
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||||
|
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||||
|
* @param range 完毕 (包含)
|
||||||
|
* @see Integer
|
||||||
|
* @return Range 对象
|
||||||
|
*/
|
||||||
|
public static Range range(int range) {
|
||||||
|
return new Range(range);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一个范围 <br>
|
||||||
|
* 最大值{@code Integer.MAX_VALUE = 2147483647 } <br>
|
||||||
|
* 最小值{@code Integer.MIN_VALUE = -2147483648} <br>
|
||||||
|
* @param start 开始 (包含)
|
||||||
|
* @param end 完毕 (包含)
|
||||||
|
* @see Integer
|
||||||
|
* @return Range 对象
|
||||||
|
*/
|
||||||
|
public static Range range(int start, int end) {
|
||||||
|
return new Range(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取迭代器
|
||||||
|
* @return 迭代器
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public @NotNull Iterator<Integer> iterator() {
|
||||||
|
return new Iterator<Integer>() {
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return current < end;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer next() {
|
||||||
|
if (current >= end) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return current++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user