import java.util.Comparator; public class TradeNode implements Comparable<TradeNode> { private String cstm; // 客戶號 private Integer mon = 0; // 交易金額 public TradeNode(String cstm, int mon) { this.mon = mon; this.cstm = cstm; } public int compareTo(TradeNode o) { if (o.cstm.equals(this.cstm)) { o.mon += this.mon; return 0; } else if (this.mon == o.mon) { return this.cstm.compareTo(o.cstm); } else { return (o.mon - this.mon); } } @Override public String toString() { // TODO Auto-generated method stub return "[" + cstm + "] [" + mon + "]"; } public int getMon() { return mon; } public void setMon(Integer mon) { this.mon = mon; } public String getCstm() { return cstm; } } |
import java.util.TreeSet; public class LsTree extends TreeSet<TradeNode> { // 添加子節(jié)點(diǎn) public boolean add(TradeNode node) { // System.out.println("add node="+node); if (contains(node)) { return true; } return super.add(node); } } |
public class testtree2 { public static void main(String[] args) { TradeNode nd1 = new TradeNode("44010358010481", 150354); TradeNode nd2 = new TradeNode("44010358010481", 150641); TradeNode nd3 = new TradeNode("44010358010481", 270000); TradeNode nd4 = new TradeNode("44010039275685", 10000); TradeNode nd5 = new TradeNode("44010039275685", 980000); TradeNode nd6 = new TradeNode("44010039275685", 5000); LsTree tree = new LsTree(); tree.add(nd1); tree.add(nd2); tree.add(nd3); tree.add(nd4); tree.add(nd5); tree.add(nd6); for (TradeNode node : tree) { System.out.println(node); } } } |
[44010039275685] [980000] [44010358010481] [570995] [44010039275685] [15000] |
public class testtree { public static void main(String[] args) { TradeNode nd1 = new TradeNode("A", 100); TradeNode nd2 = new TradeNode("B", 10); TradeNode nd3 = new TradeNode("B", 1000); LsTree tree = new LsTree(); tree.add(nd1); tree.add(nd2); tree.add(nd3); for (TradeNode node : tree) { System.out.println(node); } } } |
歡迎光臨 Chinaunix (http://72891.cn/) | Powered by Discuz! X3.2 |