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

15,567

Solution 1

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

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

Solution 2

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.

Solution 3

For TreeSet you can use pollLast function.

listOfSources.pollLast();

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

Solution 4

The most efficient is to use NavigableSet's pollLast method.

For best practice, you should declare your variable as a NavigableSet instead as a Set.

It is better than casting your variable to a TreeSet for several reasons:

  • you should keep the choice of the implementation in only one place. If you cast to TreeSet as has been advised in other answers, and you later change the chosen implementation at the place where you define your variable but forget to change it below, it will throw an exception
  • essentially the fact that your logic requires you to remove the last element means that the behavior you expect from your variable is that of a NavigableSet, not just of a Set. Thus the choice of that variable's type should make that explicit.

Solution 5

Another possibility is to use Stack class. (Although less efficient for the problem in question)

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

     Stack<String> stack = new Stack<String>();
     stack.addAll(listOfSources);
     ...
     String lastElement = stack.pop();

pop() method will get the last element and remove it from the stack.

Share:
15,567

Related videos on Youtube

Ramesh Kotha
Author by

Ramesh Kotha

● 10+ years of experience in object-oriented design, development, deployment and maintenance of Web and JEE applications using process methodologies ● Expert in development of applications using JEE technologies like Java, JSP, Servlets, JDBC, JNDI, and JavaMail ● Expert in developing Angular2 single page applications. ● Expert in developing Google Maps V3 / OpenLayers map based applications. ● Experience in developing and deploying applications using Tomcat, Web Logic. ● Proficiency in Oracle and MySQL ● Expertise in Various IDE’s likes Eclipse and MyEclipse. ● Experience in developing applications using three tier architectural frameworks such as MVC (Model View Controller) and SPRING framework and Hibernate. ● Experience in JQuery, Javascript, DWR, AJAX and XML. ● Experience working extensively on Windows environments ● Worked on all phases of Systems Development life cycle (SDLC) ● Prepared test case scenarios and internal documentation for validation and reporting ● Experienced in User Support and training end users for efficient use of developed applications. ● Strong knowledge of Gang of Four design Patterns like Façade, Singleton, DAO ● Versed with development methodologies namely SDLC

Updated on June 04, 2022

Comments

  • Ramesh Kotha
    Ramesh Kotha over 1 year

    i want to delete every last element of this set.

            Set<String> listOfSources = new TreeSet<String>();
            for(Route route:listOfRoutes){
                Set<Stop> stops = routeStopsService.getStops(route);
                for(Stop stop:stops)
                   listOfSources.add(stop.getStopName());
             }
    

    here i want to remove last element from listOfSources.

  • Dan Hardiker
    Dan Hardiker almost 12 years
    This would be less efficient (as you're creating a shallow copy of the collection, and then sorting it) and redundant - as the sorted structure is already in place, just needs accessing properly.
  • Rogel Garcia
    Rogel Garcia almost 12 years
    It's just for reference by other users that have similar problems. Maybe the Stack can be a option in other cases. In this one this is really less efficient.
  • Vic Seedoubleyew
    Vic Seedoubleyew over 4 years
    For this to work one would need to declare the variable as a NavigableSet