Sorted Set in Java with Programming Example

Sorted Set Java
In this chapter you will learn
What is Sorted Set in Java?
Programming Example

What is Sorted Set in Java?

SortedSet is a set which stores its element in ascending order or sorted according to the elements natural ordering or according to comparator provided at set creation time. The traverse iterator traverse ascending order and several additional operations are provided to take advantage to the ordering.

All elements must go through the comparable interface before inserting into sorted set and e1.compareTo(e2) (or comparator.compare(e1, e2)) must not throw the ClassCastException.

Programming Example

Output

Java
SQL
VB.Net
_

Summary

In this chapter you learned how to use Sorted Set in java programming. As you can see that we have inserted elements in different order in TreeSet but when traverse using iterator and print result, it displayed result in ascending order. In the next chapter you will learn about Map Interface in Java.


Leave a Reply

Your email address will not be published. Required fields are marked *