java 动态树与递归平整
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 动态树与递归平整相关的知识,希望对你有一定的参考价值。
import org.junit.Test;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
public class DynamicTreeTest {
@Test
public void testDynamicTree() throws Exception {
// given
DynamicTree<Customer> tree = new DynamicTree<Customer>(
new Customer("Manager"),
new DynamicTree(new Customer("Cashier-001"),
new DynamicTree(new Customer("Cashier-010"),
new DynamicTree(new Customer("Cashier-100")),
new DynamicTree(new Customer("Cashier-101")),
new DynamicTree(new Customer("Cashier-102"))),
new DynamicTree(new Customer("Cashier-011"),
new DynamicTree(new Customer("Cashier-110")),
new DynamicTree(new Customer("Cashier-111")),
new DynamicTree(new Customer("Cashier-112"))),
new DynamicTree(new Customer("Cashier-012"),
new DynamicTree(new Customer("Cashier-120")),
new DynamicTree(new Customer("Cashier-121")),
new DynamicTree(new Customer("Cashier-122")))),
new DynamicTree(new Customer("Cashier-002"),
new DynamicTree(new Customer("Cashier-020"),
new DynamicTree(new Customer("Cashier-200")),
new DynamicTree(new Customer("Cashier-201")),
new DynamicTree(new Customer("Cashier-202"))),
new DynamicTree(new Customer("Cashier-021"),
new DynamicTree(new Customer("Cashier-210")),
new DynamicTree(new Customer("Cashier-211")),
new DynamicTree(new Customer("Cashier-212"))),
new DynamicTree(new Customer("Cashier-022"),
new DynamicTree(new Customer("Cashier-220")),
new DynamicTree(new Customer("Cashier-221")),
new DynamicTree(new Customer("Cashier-222")))),
new DynamicTree(new Customer("Cashier-003"),
new DynamicTree(new Customer("Cashier-030"),
new DynamicTree(new Customer("Cashier-300")),
new DynamicTree(new Customer("Cashier-301")),
new DynamicTree(new Customer("Cashier-302"))),
new DynamicTree(new Customer("Cashier-031"),
new DynamicTree(new Customer("Cashier-310")),
new DynamicTree(new Customer("Cashier-311")),
new DynamicTree(new Customer("Cashier-312"))),
new DynamicTree(new Customer("Cashier-032"),
new DynamicTree(new Customer("Cashier-320")),
new DynamicTree(new Customer("Cashier-321")),
new DynamicTree(new Customer("Cashier-322")))),
new DynamicTree(new Customer("Cashier-004"),
new DynamicTree(new Customer("Cashier-040"),
new DynamicTree(new Customer("Cashier-400")),
new DynamicTree(new Customer("Cashier-401")),
new DynamicTree(new Customer("Cashier-402"))),
new DynamicTree(new Customer("Cashier-041"),
new DynamicTree(new Customer("Cashier-410")),
new DynamicTree(new Customer("Cashier-411")),
new DynamicTree(new Customer("Cashier-412"))),
new DynamicTree(new Customer("Cashier-042"),
new DynamicTree(new Customer("Cashier-420")),
new DynamicTree(new Customer("Cashier-421")),
new DynamicTree(new Customer("Cashier-422"))))
);
// when
List<Customer> customers = tree.flattened()
.map(c -> c.getValue())
.sorted(new Comparator<Customer>() {
@Override
public int compare(Customer c1, Customer c2) {
return c1.getName().compareTo(c2.getName());
}
})
.collect(Collectors.toList());
// then
// customers.forEach(c -> System.out.println("Customer Name = " + c.getName()));
assertThat(customers.size(), is(53));
}
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class DynamicTree<T> {
private T value;
private List<DynamicTree<T>> dynamicTrees;
public DynamicTree(T value, List<DynamicTree<T>> dynamicTrees) {
this.value = value;
this.dynamicTrees = dynamicTrees;
}
public DynamicTree(T value, DynamicTree<T>... dynamicTrees) {
this.value = value;
this.dynamicTrees = Arrays.asList(dynamicTrees);
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public List<DynamicTree<T>> getDynamicTrees() {
return dynamicTrees;
}
public void setDynamicTrees(List<DynamicTree<T>> dynamicTrees) {
this.dynamicTrees = dynamicTrees;
}
public Stream<DynamicTree<T>> flattened() {
return Stream.concat(
Stream.of(this),
dynamicTrees.stream().flatMap(DynamicTree::flattened));
}
}
以上是关于java 动态树与递归平整的主要内容,如果未能解决你的问题,请参考以下文章