如上图所示,想要检索包含a的文件及文件夹。
1、创建一个文件类。
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* @Author zyh
* @Date 2020/10/19 15:34
*/
@Data
public class FileDTO implements Serializable {
private Long id;
private String name;
private Integer type;
private String fullPath;
private Long pid;
private List<FileDTO> children;
private Integer hasChild;
public FileDTO(Long id,String name,Integer type, String fullPath,Long pid,Integer hasChild){
this.id=id;
this.name=name;
this.type=type;
this.fullPath=fullPath;
this.pid=pid;
this.hasChild=hasChild;
}
}
2、开始处理
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
/**
* @Author zyh
* @Date 2020/10/19 15:36
*/
@Slf4j
public class Test {
//原始数据
private static List<FileDTO> queryData=new ArrayList<>();
//返回数据
private static List<FileDTO> returnData=new ArrayList<>();
private static Gson gson=new Gson();
//关键字
private static String keywords="a";
public static void main(String[] args) {
//顶级目录
dingji(returnData);
//递归补全子孙
digui(returnData);
//删除不包含的枝蔓
if(!StringUtils.isEmpty(keywords)){
del(returnData);
//将不符合条件,子目录又为空的删除掉
delSonNull(returnData);
}
log.info(gson.toJson(returnData));
}
private static void delSonNull(List<FileDTO> list) {
Iterator<FileDTO> iterator = list.iterator();
while(iterator.hasNext()){
FileDTO fileDTO = iterator.next();
String name = fileDTO.getName();
List<FileDTO> children = fileDTO.getChildren();
if(!name.contains(keywords)){
if(Objects.isNull(children)||children.size()==0){
iterator.remove();
}else{
delSonNull(fileDTO.getChildren());
}
}
}
}
private static void del(List<FileDTO> list){
Iterator<FileDTO> iterator = list.iterator();
while(iterator.hasNext()){
FileDTO fileDTO = iterator.next();
String name = fileDTO.getName();
if(!name.contains(keywords)){
if(fileDTO.getHasChild()==2){
iterator.remove();
}else{
List<FileDTO> children = fileDTO.getChildren();
if(Objects.nonNull(children)&&children.size()>0){
del(fileDTO.getChildren());
}
}
}
}
}
private static void dingji(List<FileDTO> list){
queryData.forEach(s->{
if(s.getPid()==0L){
list.add(s);
}
});
}
private static void digui(List<FileDTO> list) {
list.forEach(s->{
Long id = s.getId();
if(s.getHasChild()==1){
List<FileDTO> children=getChildren(id);
s.setChildren(children);
digui(children);
}
});
}
private static List<FileDTO> getChildren(Long pid){
List<FileDTO> children=new ArrayList<>();
queryData.forEach(q->{
if(pid.longValue()==q.getPid().longValue()){
children.add(q);
}
});
return children;
}
static{
FileDTO fileDTO1=new FileDTO(1L,"c",2,",1,",0L,1);
FileDTO fileDTO2=new FileDTO(2L,"a.xls",1,",1,2,",1L,2);
FileDTO fileDTO3=new FileDTO(3L,"1016翻译.xlsx",1,",1,3,",1L,2);
FileDTO fileDTO4=new FileDTO(4L,"b",2,",1,4,",1L,1);
FileDTO fileDTO5=new FileDTO(5L,"b.xlsx",1,",1,4,5,",4L,2);
FileDTO fileDTO6=new FileDTO(6L,"a",2,",1,6,",1L,2);
FileDTO fileDTO7=new FileDTO(7L,"a.xls",1,",7,",0L,2);
FileDTO fileDTO8=new FileDTO(8L,"a8.xls",1,",1,4,8,",4L,2);
queryData.clear();
queryData.add(fileDTO1);
queryData.add(fileDTO2);
queryData.add(fileDTO3);
queryData.add(fileDTO4);
queryData.add(fileDTO5);
queryData.add(fileDTO6);
queryData.add(fileDTO7);
queryData.add(fileDTO8);
}
}
3、最终数据为