Tuesday, 30 August 2016

TreeSet accept only Comparable Elements


Elements that are  added into TreeSet must be Comparable, implementing Comparable interface; or transferring a Comparator  that implements Comparator interface via TreeSet argument, i.e.
public TreeSet(Comparator comparator)

The underlying reason; TreeSet is implemented as a black-red tree, it always compares two elements, and knowing bigger or smaller in order to put a bigger one in the right sub-tree of a root node, but a smaller one on the left side.

If you don't add a comparable element in the TreeSet, then getting a compiling error.






No comments:

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...