java - TreeSet contains method doesn't work for me -
java - TreeSet contains method doesn't work for me -
i want set custom's info treeset
. when custom number same, add together volume of trade.
here tradenode
class implements comparable
interator.
import java.util.comparator; public class tradenode implements comparable<tradenode> { private string cstm; // custom number private integer mon = 0; // trade public tradenode() {} 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; homecoming 0; } else if (this.mon == o.mon) { homecoming this.cstm.compareto(o.cstm); } else { //return (o.mon - this.mon); homecoming o.mon.compareto(this.mon); } } @override public boolean equals(object obj) { if (this == obj) { homecoming true; } if (obj == null) { homecoming false; } if (!(obj instanceof tradenode)) { homecoming false; } tradenode other = (tradenode) obj; if (cstm == null) { if (other.cstm != null) { homecoming false; } } else if (!cstm.equals(other.cstm)) { homecoming false; } homecoming true; } @override public int hashcode() { final int prime = 31; int result = 1; result = prime * result + ((cstm == null) ? 0 : cstm.hashcode()); homecoming result; } @override public string tostring() { homecoming "[" + cstm + "] [" + mon + "]"; } public int getmon() { homecoming mon; } public void setmon(integer mon) { this.mon = mon; } public string getcstm() { homecoming cstm; } }
and test class :
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); treeset<tradenode> tree = new treeset<tradenode>(); tree.add(nd1); tree.add(nd2); tree.add(nd3); (tradenode node : tree) { system.out.println(node); } }
i supposed output shoud :
[b] [1010] [a] [100]
but output
[b] [1000] [a] [100] [b] [10]
could help me , point me out fault is?
if alter compareto() method this, still doesn't work.
public int compareto(tradenode o) { if (o.cstm.equals(this.cstm)) { homecoming 0; } else { homecoming o.mon.compareto(this.mon); } }
and result is:
[b] [1000] [a] [100] [b] [10]
i tryed ben xu's method, , here code: new compareto() method:
public int compareto(tradenode o) { if (o.cstm.equals(this.cstm)) { homecoming 0; } else { homecoming this.mon.compareto(o.mon); } }
my new testtree class:
public class testtree { 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); tradenode nd7 = new tradenode("44010234235687", 10000); tradenode nd8 = new tradenode("44010234235687", 360000); tradenode nd9 = new tradenode("44010234235687", 53400); map<string, integer> map = new hashmap<string, integer>(); addtradenode(map, nd1); addtradenode(map, nd2); addtradenode(map, nd3); addtradenode(map, nd4); addtradenode(map, nd5); addtradenode(map, nd6); addtradenode(map, nd7); addtradenode(map, nd8); addtradenode(map, nd9); iterator<entry<string, integer>> iterator = map.entryset().iterator(); tradenode t; list<tradenode> list = new arraylist<tradenode>(); while(iterator.hasnext()) { map.entry<string, integer> m = iterator.next(); t = new tradenode(m.getkey(),m.getvalue()); list.add(t); } collections.sort(list); for(tradenode tn : list) { system.out.println(tn); } } private static void addtradenode(map<string, integer> map, tradenode node) { integer integer = map.get(node.getcstm()); if (integer == null) { map.put(node.getcstm(), node.getmon()); } else { map.remove(node.getcstm()); map.put(node.getcstm(), integer.intvalue() + node.getmon()); } } }
and result is:
[44010234235687] [423400] [44010358010481] [570995] [44010039275685] [995000]
finally, satisfied requirement. still don't know why new compareto() method doesn't work in next test method:
public class testtree2 { public static void main(string[] args) { tradenode nd1 = new tradenode("a", 100); tradenode nd2 = new tradenode("b", 10); tradenode nd3 = new tradenode("b", 1000); treeset<tradenode> tree = new treeset<tradenode>(); tree.add(nd1); tree.add(nd2); tree.add(nd3); (tradenode node : tree) { system.out.println(node); } } }
and result is:
[b] [10] [a] [100] [b] [1000]
and supposed :
[b] [10] [a] [100]
could tell me fault in new compareto() methods? lot , helping me.
hahaha,i've got reply javaranch. there's named henry told me answer. think when utilize contains() method in treeset, doesn't search in set, searched sorted value.
the new testtree3 class :
public class testtree3 { public static void main(string[] args) { tradenode nd1 = new tradenode("a", 100); tradenode nd2 = new tradenode("b", 200); tradenode nd3 = new tradenode("b", 1000); treeset<tradenode> tree = new treeset<tradenode>(); tree.add(nd1); tree.add(nd2); tree.add(nd3); (tradenode node : tree) { system.out.println(node); } } }
and result :
[a] [100] [b] [200]
haha. i'll go find codes behinds treeset.
the run result , can check run programme again.
[b] [1000] [a] [100] [b] [10]
the result due treeset uses implemented comparator
i don't know want do.
but there @ to the lowest degree 1 obvious bad practice:
public int compareto(tradenode o) { if (o.cstm.equals(this.cstm)) { o.mon += this.mon; homecoming 0; } else if (this.mon == o.mon) { homecoming this.cstm.compareto(o.cstm); } else { //return (o.mon - this.mon); homecoming o.mon.compareto(this.mon); } }
you should not alter value in compareto method “ o.mon += this.mon; ”, confusing.
if want sum treenode same name, don't utilize collection , utilize map instead.
for example, utilize hashmap, key name or(treenode since equals , hashcode uses cstm ), value num. every time add together treenode, check if same name exsit, if not , add together map, else add together value.
following 1 illustration code using map:
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); map<string, integer> map = new hashmap<string, integer>(); addtreenode(map, nd1); addtreenode(map, nd2); addtreenode(map, nd3); system.out.println(map); } private static void addtreenode(map<string, integer> map, tradenode node) { integer integer = map.get(node.getcstm()); if (integer == null) { map.put(node.getcstm(), node.getmon()); } else { map.remove(node.getcstm()); map.put(node.getcstm(), integer.intvalue() + node.getmon()); } } }
java contains treeset
Comments
Post a Comment