SiteMap  | About    

Treeset in Java

How to Java Treeset

TreeSet class implements the Set interface, backed by a TreeMap . It creates a collection that uses a tree for storage. Objects are stored in sorted, ascending order according to natural order. The TreeSet implementation is sorting by the lexicographic order of the string values you insert. Optionally, we can change the natural order of a TreeSet by using a Comparable or Comparator interfaces. Moreover, it contains unique elements only like HashSet. If you need a sorted set, then TreeSet should be used.

TreeSet might not be used when our application has requirement of modification of set in terms of frequent addition of elements. Because this implementation is not synchronized. If multiple threads access a TreeSet concurrently, and at least one of the threads modifies the TreeSet, it must be synchronized externally.

The following Java program illustrates several of the methods supported by this TreeSet collection Framework (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.