package com.kingbal.king.dmp;
import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
/**
* <b>Function: </b> todo
*
* @program: 通过当前path获取上一级或下一级
* @Package: com.kingbal.king.dmp
* @author: dingcho
* @date: 2024/12/23
* @version: 1.0
* @Copyright: 2024 www.kingbal.com Inc. All rights reserved.
*/
@Slf4j
public class BaseTest {
public static void main(String[] args) {
List<Node> list = Lists.newArrayList();
list.add(new Node(0L, "1"));
list.add(new Node(0L, "2"));
list.add(new Node(0L, "3_1"));
list.add(new Node(0L, "3_1_1"));
list.add(new Node(0L, "3"));
list.add(new Node(0L, "4"));
list.add(new Node(0L, "4_1"));
list.add(new Node(0L, "4_1_1"));
list.add(new Node(0L, "4_1_1_1"));
list.add(new Node(0L, "4_2"));
list.add(new Node(0L, "4_2_1"));
list.add(new Node(0L, "4_2_1_1"));
Node node = getLast(list, "4_1", true);
System.out.println(node);
// System.out.println("4_1".indexOf("4"));
}
public static Node getLast(List<Node> list, String path , boolean isLast){
Node node = new Node();
String splitStr ="_";
List<Node> listPeekList = list.stream().peek(item -> {
String sortIndex = item.getPath().replace(splitStr, "");
item.setSortIndex(Long.valueOf(sortIndex));
}).toList();
Optional<Node> currentNodeOptional = listPeekList.stream().filter(f-> f.getPath().equals(path)).findFirst();
if(currentNodeOptional.isPresent()) {
Node currentNode = currentNodeOptional.get();
Optional<Node> lastNodeOptional;
if (!path.contains(splitStr)) {
listPeekList = listPeekList.stream().filter(f -> !f.getPath().contains(splitStr)).
sorted(Comparator.comparingLong(Node::getSortIndex)).toList();
if (isLast) {
lastNodeOptional = listPeekList.stream()
.filter(f -> f.getSortIndex() < currentNode.getSortIndex())
.sorted(Comparator.comparingLong(Node::getSortIndex).reversed()).findFirst();
} else {
lastNodeOptional = listPeekList.stream().filter(f -> f.getSortIndex() >
currentNode.getSortIndex()).findFirst();
}
} else {
String prefixStr = path.substring(0, path.indexOf(splitStr));
System.out.println(prefixStr);
listPeekList = listPeekList.stream().filter(f -> f.getPath().indexOf(prefixStr) == 0).
sorted(Comparator.comparingLong(Node::getSortIndex)).toList();
if (isLast) {
lastNodeOptional = listPeekList.stream()
.filter(f -> f.getSortIndex() < currentNode.getSortIndex())
.sorted(Comparator.comparingLong(Node::getSortIndex).reversed()).findFirst();
} else {
lastNodeOptional = listPeekList.stream().filter(f -> f.getSortIndex() >
currentNode.getSortIndex()).findFirst();
}
}
if (lastNodeOptional.isPresent()) {
return lastNodeOptional.get();
}
}
return node;
}
}
@Data
@AllArgsConstructor
class Node{
private Long sortIndex;
private String path;
public Node(){}
}
标签:Node,Java,一级,list,getSortIndex,add,new,path,0L
From: https://blog.csdn.net/yao_guai/article/details/144655562