Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have a multiset data structure like the one in C++ STL?

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?

like image 296
outlaw Avatar asked Sep 24 '12 13:09

outlaw


People also ask

Does Java have 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.

Is multiset sorted C++?

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.

What is multiset in STL?

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.

What is a multiset data structure?

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.


1 Answers

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>>.

like image 107
TheCrazyProgrammer Avatar answered Oct 08 '22 12:10

TheCrazyProgrammer