Java生成股权穿透图
时间: 2023-07-04 16:13:17 浏览: 259
d3+jQuery类似天眼查企查查的股权穿透图代码
5星 · 资源好评率100%
生成股权穿透图需要先构建出股权关系树,然后将树转换为穿透图。下面是一个简单的Java代码示例,用于生成股权穿透图:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class EquityTree {
private String name;
private double equity;
private List<EquityTree> children;
public EquityTree(String name, double equity) {
this.name = name;
this.equity = equity;
this.children = new ArrayList<>();
}
public void addChild(EquityTree child) {
this.children.add(child);
}
public String getName() {
return name;
}
public double getEquity() {
return equity;
}
public List<EquityTree> getChildren() {
return children;
}
// 将股权关系树转换为穿透图
public HashMap<String, Double> toEquityMap() {
HashMap<String, Double> map = new HashMap<>();
map.put(this.name, this.equity);
for (EquityTree child : this.children) {
HashMap<String, Double> childMap = child.toEquityMap();
for (String key : childMap.keySet()) {
double childEquity = childMap.get(key);
if (map.containsKey(key)) {
map.put(key, map.get(key) + childEquity);
} else {
map.put(key, childEquity);
}
}
}
return map;
}
public static void main(String[] args) {
// 构建股权关系树
EquityTree root = new EquityTree("A", 1.0);
EquityTree b = new EquityTree("B", 0.5);
EquityTree c = new EquityTree("C", 0.3);
EquityTree d = new EquityTree("D", 0.2);
EquityTree e = new EquityTree("E", 0.1);
root.addChild(b);
root.addChild(c);
b.addChild(d);
c.addChild(e);
// 将树转换为穿透图
HashMap<String, Double> equityMap = root.toEquityMap();
for (String key : equityMap.keySet()) {
System.out.println(key + ": " + equityMap.get(key));
}
}
}
```
在这个示例中,我们首先构建了一个简单的股权关系树,然后将其转换为穿透图。`toEquityMap`方法递归遍历整个树,并将每个节点的股权值累加到其所有祖先节点中,最终返回一个HashMap,其中键是公司名称,值是股权比例。
阅读全文