| | |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.ArrayList; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | 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<>(); |
| | | Predicate<T> predicate = node -> fetcher.apply(node).contains(condition); |
| | | for (T node : data) { |
| | | List<T> children = childrenGetter.apply(node); |
| | | public static <T> void treeRemove(List<T> list, String condition, Function<? super T, ? extends String> fetcher, Function<T, List<T>> childrenGetter) { |
| | | Iterator<T> iterator = list.iterator(); |
| | | while (iterator.hasNext()) { |
| | | T next = iterator.next(); |
| | | List<T> children = childrenGetter.apply(next); |
| | | 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); |
| | | treeRemove(children, condition, fetcher, childrenGetter); |
| | | } else { |
| | | if (!fetcher.apply(next).contains(condition)) { |
| | | iterator.remove(); |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |