| | |
| | | import java.util.Optional; |
| | | import java.util.function.BiConsumer; |
| | | import java.util.function.Function; |
| | | import java.util.function.Predicate; |
| | | |
| | | /** |
| | | * Created by vincent on 2023/3/14 |
| | |
| | | return result; |
| | | } |
| | | |
| | | // public static <T> List<T> treeRemove(List<T> data, String condition, |
| | | // Function<? super T, ? extends String> fetcher, |
| | | // Function<T, List<T>> childrenGetter, |
| | | // BiConsumer<T, List<T>> childrenSetter) { |
| | | // List<T> result = new ArrayList<>(); |
| | | // for (T node : data) { |
| | | // List<T> children = childrenGetter.apply(node); |
| | | // |
| | | // if (children != null && !children.isEmpty()) { |
| | | // List<T> newChildren = treeRemove(children, condition, fetcher, childrenGetter, childrenSetter); |
| | | // childrenSetter.accept(node, newChildren); |
| | | // } |
| | | // |
| | | // if (fetcher.apply(node).contains(condition)) { |
| | | // result.add(node); |
| | | // } |
| | | // } |
| | | // return result; |
| | | // } |
| | | |
| | | public static <T> List<T> treeRemove(List<T> data, String condition, |
| | | Function<? super T, ? extends String> fetcher, |
| | | Function<T, List<T>> childrenGetter, |
| | | BiConsumer<T, List<T>> childrenSetter) { |
| | | List<T> result = new ArrayList<>(); |
| | | Predicate<T> predicate = node -> fetcher.apply(node).contains(condition); |
| | | for (T node : data) { |
| | | List<T> children = childrenGetter.apply(node); |
| | | if (children != null && !children.isEmpty()) { |
| | | List<T> newChildren = treeRemove(children, condition, fetcher, childrenGetter, childrenSetter); |
| | | childrenSetter.accept(node, newChildren); |
| | | } |
| | | if (predicate.test(node)) { |
| | | result.add(node); |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 数组倒序 |
| | | * @param bytes |