자바 도구 클래스 - List 를 트 리 구조 로 변환
5151 단어 자바
public class TreeModel {
/**
*
*/
private Function superColumn;
/**
*
*/
private Function column;
/**
*
*/
private BiConsumer> children;
/**
*
*/
private List list;
/**
*
*/
private Supplier decoupling;
/**
*
*/
private TreeModel() {
}
/**
*
*
* @param list
* @param superColumn ( :: get )
* @param column ( :: get )
* @param children ( :: set )
* @return
*/
public static TreeModel load(List list, Function superColumn,
Function column, BiConsumer> children) {
TreeModel treeModel = new TreeModel<>();
treeModel.setSuperColumn(superColumn)
.setColumn(column)
.setChildren(children)
.setList(list);
return treeModel;
}
/**
* ( )
*
* @param initValue
* @return
*/
public List getTree(Object initValue) {
List tree = treeAll(initValue);
if (tree != null && tree.size() > 0) {
return tree.stream().filter(ls -> initValue.equals(superColumn.apply(ls))).collect(Collectors.toList());
}
return tree;
}
/**
* ( )
*
* @param initValue
* @param decoupling ( ::new)
* @return
*/
public List getTree(Object initValue, Supplier decoupling) {
this.setDecoupling(decoupling);
return tree(initValue);
}
/**
*
*
* @param initValue
* @return
*/
private List treeAll(Object initValue) {
if (list == null || list.size() < 1) {
return list;
}
List collect = list.stream().filter(f -> initValue.equals(superColumn.apply(f))).collect(Collectors.toList());
if (collect.size() < 1) {
return null;
}
collect.forEach(c -> children.accept(c, treeAll(column.apply(c))));
return collect;
}
private List tree(Object o) {
List childrenList = new ArrayList<>();
if(o == null){
return childrenList;
}
for (T entity : list) {
if (o.equals(superColumn.apply(entity))) {
T now = decoupling.get();
copy(entity, now);
childrenList.add(now);
}
}
for (T cs : childrenList) {
children.accept(cs, tree(column.apply(cs)));
}
return childrenList;
}
/**
* source target
* @param source
* @param target
*/
private void copy(T source, T target) {
if (source == null) {
throw new NullPointerException("dataSource");
}
if (target == null) {
target = decoupling.get();
}
Field[] sourceFields = source.getClass().getDeclaredFields();
Field[] targetFields =[ ](https://www.gendan5.com/p/2021-06-11/248014.html) target.getClass().getDeclaredFields();
Map sourceMap = new HashMap<>(sourceFields.length);
try {
for (Field field : sourceFields) {
if (Modifier.isFinal(field.getModifiers())) {
continue;
}
field.setAccessible(true);
sourceMap.put(field.getName(), field.get(source));
}
for (Field field : targetFields) {
Object o = sourceMap.get(field.getName());
if (o == null) {
continue;
}
field.setAccessible(true);
field.set(target, o);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private TreeModel setSuperColumn(Function superColumn) {
if (superColumn == null) {
throw new NullPointerException("superColumn");
}
this.superColumn = superColumn;
return this;
}
private TreeModel setColumn(Function column) {
if (column == null) {
throw new NullPointerException("column");
}
this.column = column;
return this;
}
private TreeModel setChildren(BiConsumer> children) {
if (children == null) {
throw new NullPointerException("children");
}
this.children = children;
return this;
}
private void setList(List list) {
if (list == null || list.size() < 1) {
throw new NullPointerException("list");
}
this.list = list;
}
private void setDecoupling(Supplier decoupling) {
if (decoupling == null) {
throw new NullPointerException("decoupling");
}
this.decoupling = decoupling;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.