how to delete last element in java.util.Set?

You will need to cast back to TreeSet, as Set's don't have any order.

listOfSources.remove( ((TreeSet) listOfSources).last() );

As an alternative you can set listOfSources as a SortedSet

SortedSet<String> listOfSources = new TreeSet<String>();

Then you can use last() method without casting to TreeSet

listOfSources.remove(listOfSources.last());

I think that this is a preferred approach since you suppose that your Set has an order.


For TreeSet you can use pollLast function.

listOfSources.pollLast();

See : http://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#pollLast()