Nick Nick - 4 months ago 21
Java Question

Multiset in Android

I have a structure

class A
{
float key;
Foo data;
}


I need to maintain an array of these structures sorted by
key
. Insertion and deletion of elements into the array must be efficient (say
O(log n)
).
key
is not guaranteed to be unique. Also I need to enumerate the array in the sorted order. Random access by index is not needed.

If I were using C++, I would use
std::multiset
here.

What would you suggest to use for Java for Android?

Answer

You can use Guava Multiset in order to achieve what you want. In Android Studio you just need to add the following dependancy to your build.gradle:

compile 'com.google.guava:guava:19.0'

Then you just need to declare a Multiset of your class, like this:

Multiset<A> multiset = HashMultiset.create();

multiset.add(new A(1.5, "a"));

If you take a look at the docs the available methods should allow you to do what you need.

Comments