Wednesday, 15 July 2015

java - Why descendingSet smaller than original set? -



java - Why descendingSet smaller than original set? -

i have collection 191 elements:

navigableset<taxon> _set = new concurentskiplistset<>(); ... _set.size(); // 191 _set.descendingset().size(); // 191 _set.descendingset().toarray().length; // 53 _set.toarray().length; // 191

taxon.java:

class taxon implements comparable<taxon> { public final int x; public final int y; public final int r; public final list<long> ips; public taxon(int x, int y, int r, list<long> ips) { this.x = x; this.y = y; this.r = r; this.ips = ips; } @override public int compareto(taxon o) { homecoming this.ips.size() > o.ips.size() ? 1 : -1; } }

why _sel.descendingset().toarray() smaller? when iterate _sel.descendingset() it's breaken on 53 element. how iterate whole descending collection?

update sure there no modification on set, during check

this issue due wrong implementation of compareto() method. compareto() contract not followed here.

contract states if x.comparedto(y) == -(y.comparedto(x)). lets consider next illustration suppose have next objects class taxon insertion order described below

insertion order taxon ips list size taxon0 3 taxon1 1 taxon2 1 taxon3 3 taxon4 1

taxon0.comparedto(taxom3) -1 , taxon3.compareto(taxon0) -1. means taxon0 smaller taxom3 , taxom3 smaller taxom0 wrong.

_set.descendingset().size() , _set.descendingset().toarray().length returns different results because _set.descendingset().toarray() method internally utilize iterator current snapshot of set. iterator internally uses compareto() method iterate on set. size() method not utilize iterator , count number of elements.hence both of them have different homecoming values.

concurrentskiplistset arranges elements sorted natural ordering or based on comparator. when add together taxom objects concurrentskiplistset, arranged in next order (skipping index node here)

head -> taxon4 -> taxon2 -> taxon1 -> taxon3 -> taxon0 -> null

_set.descendingset().toarray().length set pointer @ taxon0 , after taxom0.comparedto(taxon3) returns -1. hence iterator decides taxom0 before taxom3 , taxom3 skipped during iteration. iterator comes know taxom1 precedes taxom0. after pointer shifts taxom1 , wise taxon1 compared taxom4(not taxom2 due skip list algorithm, downwards , right pointers) , taxom1.comparedto(taxom4) says taxom1 smaller taxom4 contradicts actual arrangement , pointer moves head. hence iterator homecoming 2 elements.

here have 2 kinds on length ips list 1,3. hence 2 elements. in example, may have 91 kinds of different lengths ips list. duplicates skipped due compareto() method.

you can add together more attributes in compareto() method decide natural ordering of taxon per business logic.

java collections set

No comments:

Post a Comment