java - compareTo not working - Testing all combinations of two strings -
java - compareTo not working - Testing all combinations of two strings -
my class defined follows...
public static class connection implements comparable<connection> here compareto method.
@override public int compareto(connection o) { list<string> combinations = new arraylist<string>(); for(string lcp : lcp_order){ for(string state : state_order){ combinations.add(lcp + state); } } int result = combinations.indexof(this.lifecycle + this.conneditstate) - combinations.indexof(o.getlifecycle() + o.getconneditstate()); homecoming result; } so on output have..
this lcp = pr state = null o lcp = advertisement o state = null result = 0 this should not homecoming 0 because lcp different. need way test combinations of 2 strings. suggestions?
in list of combinations, you're not adding variants 1 of 2 values null.
in test you're executing, state null, combination not found. phone call indexof homecoming -1 in both cases, , - 1 - (-1) result in 0.
as side note:
if possible, want store combinations constant instead of building list every timecompareto called. you might want separate lcp state sort of separator character avoid clashes (the case lcp1 + state1 == lcp2 + state2, e.g. "ab" + "c" == "a" + "bc") if such risk exists. you don't need build list of combinations, , can implement compareto method comparing indexes of lcp , state in lcp_order , state_order separately:
@override public int compareto(connection o) { int orderindex = lcp_order.indexof(this.lifecyle) - lcp_order.indexof(o.lifecyle); if (orderindex != 0) { homecoming orderindex; } int stateindex = state_order.indexof(this.conneditstate) - state_order.indexof(o.conneditstate); homecoming stateindex; } java
Comments
Post a Comment