I need a data structure which works like the STL multiset but the TreeSet in Java doesn't allow duplicate elements. Is there any built-in data structure in Java which is equivalent to multiset?
We know that Java doesn't provide Multiset implementation, so programmers often switch to HashMap to store the total number of times each key occurs.
In case of Set, data is stored in sorted order. In case of MultiSet also the data is stored in sorted order. In Set duplicate values are not allowed to get stored.
Multisets are part of the C++ STL (Standard Template Library). Multisets are the associative containers like Set that stores sorted values (the value is itself the key, of type T), but unlike Set which store only unique keys, multiset can have duplicate keys. By default it uses < operator to compare the keys.
A MultiSet is a data structure which stores and manipulates an unordered collection of elements which may be repeated. It is implemented as a Maple object. The procedure exports of a MultiSet are used to create, update, query and otherwise interact with one or more MultiSet objects.
Using Map<E, Integer>
where Integer is the count is a good replacement for Multiset, and it does not need any third party library as well.
Update: If you really want to store the object twice, use a List with a Map like Map<E, List<E>>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With