java - Comparison Logic Error with Economic data - Comparison method violates its general contract -
java - Comparison Logic Error with Economic data - Comparison method violates its general contract -
i know have problem in compareto method, not sure where..
here's info i'm trying sort:
i'm looking @ many .txt files (each around 20,000 lines) each line has single point of data. extracting financial year (formatted yyyyqx, x 1-4 financial quarter) , storing string. extracting industry code (six-digit integer) , cost index (stored double). stored in datapoint object.
i want output 3 columns, 1 of financial year, 1 of industry code, , 1 of cost index. want info formatted financial years in order (1991q1, 1991q2,..., 1992q1, etc.), industry codes ordered in to the lowest degree greatest value. so, financial year column have many 1991q1 entries each industry code cost index @ quarter. then, when industry codes 1991q1 have been exhausted, industry codes 1991q2 listed, etc.
to accomplish this, build datapoint compareto method follows:
public int compareto(datapoint p) { int fiscalresult = comparefiscal(p.getfiscalquarter()); if (fiscalresult > 0) { homecoming fiscalresult; } else if (fiscalresult < 0) { homecoming fiscalresult; } else { if (sectorcode > 0) { if (sectorcode > p.getsectorcode()) { homecoming sectorcode - p.getsectorcode(); } else if (sectorcode < p.getsectorcode()){ homecoming p.getsectorcode() - sectorcode; } else { homecoming 0; // should never happen } } else if (industrycode > 0) { if (industrycode > p.getindustrycode()) { homecoming industrycode - p.getindustrycode(); } else if (industrycode < p.getindustrycode()) { homecoming p.getindustrycode() - industrycode; } else { homecoming 0; // should never happen } } // these should never reached else if (p.getsectorcode() > 0) { homecoming -1; } else if (p.getindustrycode() > 0) { homecoming -1; } else { homecoming 0; } } }
where comparefiscal(string) method just:
public int comparefiscal(string otherfiscal) { homecoming fiscalquarter.compareto(otherfiscal); }
fiscalquarter name of string variable containing yyyyqx financial year.
when said industry code earlier, there either sector code (which four-digit integer) or industry code (six-digit integer). datapoint not have both (the 1 doesn't have initialized 0), check sectorcode or industrycode's value in compareto method.
i can sort list of these points in 1 file without problem, @ end of programme take info points every file , set them new arraylist (two lists, 1 sector codes , 1 industry codes. @ no point sector , industry codes sorted together), , phone call collections.sort on list. point throws error.
here 1 point trying phone call collections.sort method (for industry list, identical 1 used sector list). datalist object represents 1 file, containing 2 lists, 1 of sector datapoints , 1 of industry datapoints. list of datalists has of datalists created every file. don't think illuminates anything, relevance:
public static list<datapoint> formatindustrydata(list<datalist> datalists) { list<datapoint> info = new arraylist<>(); (datalist list : datalists) { data.addall(list.getindustrypoints()); } collections.sort(data); homecoming data; }
can see logic goes wrong in compareto method?
edit: forgot mention there never point, if financial year different, sector/industry code equal another. (e.g. there never 2 cost indices same sector code @ same financial year, because wouldn't create much sense).
furthermore, @ no point datapoints industry values compared datapoints sector values - stored in separate lists, , compared , sorted among each other.
your logic seems incomplete.
if sectorcode of this
instance > 0, compare sectorcode, don't handle case p.sectorcode <= 0.
similarly, if industrycode of this
instance > 0, compare industrycode, don't handle case p.industrycode <= 0.
you should decide of 2 properties - sectorcode , industrycode - takes precedence.
suppose object has sectorcode 5 , industrycode 0. object b has sectorcode 0 , industrycode 6.
a.compareto(b) returns 1 b.compareto(a) returns 1
this breaks contract of compareto, since a>b , b>a can't both true.
if want compare sectorcode first, code should :
if (sectorcode > 0) { if (sectorcode > p.getsectorcode()) { homecoming 1; } else { homecoming -1; } } else if (p.getsectorcode() > 0) { homecoming -1; } else if (industrycode > 0) { if (industrycode > p.getindustrycode()) { homecoming 1; } else { homecoming -1; } } else if (p.getindustrycode() > 0) { homecoming -1; } else { homecoming 0; // should never happen }
in addition, should homecoming 0 if industrycode == p.getindustrycode()
(when both positive) or sectorcode == p.getsectorcode()
(when both positive).
java compare illegalargumentexception compareto
Comments
Post a Comment