question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TreeMultiSet clear is not working

See original GitHub issue

I have a TreeMultiSet as a member of the singleton object. When I clear the set using clear(), it doesn’t clear any.

class Campaign {
   private static Campaign singleton;
   private TreeMultiset<Item> items;
  
   public Campaign() {
        items = TreeMultiSet.create();
   }
   public void destroy() {
      items.clear();
   }
   public void setItem(Item item) {
      items.add(item);
   }
   public static Campaign getSingleton() {
     if (singleton == null) {
            synchronized (Campaign.class) {
                if (singleton == null) {
                    singleton = new Campaign();
                }
            }
        }
        return singleton;
   }
}

Item item = new Item();
Campaign.getSingleton().setItem(item);
Campaign.getSingleton().destroy(); //its not clearing the set. no errors either.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jbduncancommented, Jan 23, 2017

Hi @rohit5ram, there are two things I can think of which may allow you to improve your code.

  1. I’d consider moving your current Item.compareTo implementation into it’s own Ordering comparator, like so:
private static final Ordering<Item> ITEM_BY_PRIORITY_REVERSE_ORDERING =
  new Ordering<Item>() {
    @Override
    public int compare(Item left, Item right) {
      Preconditions.checkNotNull(left, "left");
      Preconditions.checkNotNull(right, "right");
      return Ints.compare(left.priority, right.priority);
    }
  }.reverse();

The advantage of this is that you can implement Item.compareTo separately in terms of all its fields rather than just priority, which is what future readers of your code would expect compareTo to do, I imagine.

  1. I’d then consider replacing ArrayList<CampaignFileItem> campaignFileItems = ... /* your unsorted items */; Collections.sort(campaignFileItems); with
ImmutableList<Item> campaignFileItems = ITEM_BY_PRIORITY_REVERSE_ORDERING.immutableSortedCopy(... /* your unsorted items */);

The advantage of this is you end up with an immutable data structure, which is easier to reason about as you can never accidentally change it (assuming Item itself is immutable, i.e. all its fields are final and they are of primitive types and/or object types which themselves cannot be mutated e.g. String).

1reaction
lowassercommented, Jan 20, 2017

To answer your other question, yes, your contract violating comparator is absolutely the reason clear() is not working.

Read more comments on GitHub >

github_iconTop Results From Across the Web

multiset clear() function in C++ STL - GeeksforGeeks
The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container.
Read more >
std::multiset::clear - C++
Removes all elements from the multiset container (which are destroyed), leaving the container with a size of 0. Parameters. none. Return value. none ......
Read more >
multiset - Guava: Access elements in TreeMultiset via position ...
Let's take an example: say the multiset has contents [5 x a, 3 x b, 7 x c, 2 x d, 5 x...
Read more >
multiset clear() function in C++ STL - Tutorialspoint
clear () removes all the elements from the elements of the multiset container and makes the size of the multiset container as 0....
Read more >
std::multiset<Key,Compare,Allocator>::clear - cppreference.com
std::multiset<Key,Compare,Allocator>::clear ... LWG 224, C++98, the complexity was log(size()) + N, but N was not defined ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found