Convert ArrayList to TreeSet in Java

I wanted to store Java ArrayList information inside TreeSet in reverse order. To achieve this there is no direct Java API provided in ArrayList or in TreeSet, to achieve this we have to use Collections API in Java.

Assume that i want unique salary in descending order across all the employees and your salary is stored in some XML or JSON(Data is stored in XML or JSON in ascending order). Now first will store salary inside ArrayList ’empSalary’

Now we would sort the entries of ArrayList in reverse order.

Pass the ArrayList to TreeSet as constructor argument

Tagged . Bookmark the permalink.

Leave a Reply