TreeMultiSet clear is not working
See original GitHub issueI 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:
- Created 7 years ago
- Comments:14 (7 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @rohit5ram, there are two things I can think of which may allow you to improve your code.
Item.compareTo
implementation into it’s own Ordering comparator, like so:The advantage of this is that you can implement
Item.compareTo
separately in terms of all its fields rather than justpriority
, which is what future readers of your code would expectcompareTo
to do, I imagine.ArrayList<CampaignFileItem> campaignFileItems = ... /* your unsorted items */; Collections.sort(campaignFileItems);
withThe 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 arefinal
and they are of primitive types and/or object types which themselves cannot be mutated e.g.String
).To answer your other question, yes, your contract violating comparator is absolutely the reason clear() is not working.