multimap

HashMap with multiple Value [duplicate]

梦想与她 提交于 2019-11-30 19:50:35
问题 This question already has answers here : HashMap with multiple values under the same key (20 answers) Closed 4 years ago . I want to implement Hash table with multiple values in java i.e // if sample is a hashmap sample.put(1,1); sample.put(1,2); and sample.get(1); will return 2 values. How can i achieve this? 回答1: You can use a Multimap instead. It keeps multiple values for a key in a list. There are implementations in commons-collections and in Guava. Multimap<String, String> multimap =

multimap vs map with set

时光总嘲笑我的痴心妄想 提交于 2019-11-30 11:35:05
I'm wondering which is more efficient. std::map< String, std::set<int> > or std::multimap< String, int > EDIT: I do not plan on doing anything out of the ordinary with these maps. Standard insert, delete, modify, search. The size of each set or multi keyed String shouldn't be more than 100. This I believe is implementation dependant, but an (un)educated guess: In practice it depends on the number of integers that you will be keeping in the multimap or the std::set . A multimap will most likely use a linear search of the values after the log(n) search of the key. If you have a large number of

“multiset” & “multimap” - What's the point?

喜夏-厌秋 提交于 2019-11-30 08:07:31
As the question states ... I don't get the point about multiset s / multimap s . So, what's the purpose? Some use cases: multimap With ZIP code as a key, all people which have that ZIP code With account ID as key, all open orders of that person/account A dictionary, with per keyword various explanations multiset is in essence a map with a key and a integer count. The inventory of a shop, all products have their key and the amount still available is the value accumulated sales data of a shop, every time a product is sold the product id get's added to the multiset thereby increasing the amount

How can I get all the unique keys in a multimap

。_饼干妹妹 提交于 2019-11-30 06:28:56
I have a multimap and I want get all the unique keys in it to be stored in a vector. multimap<char,int> mymm; multimap<char,int>::iterator it; char c; mymm.insert(pair<char,int>('x',50)); mymm.insert(pair<char,int>('y',100)); mymm.insert(pair<char,int>('y',150)); mymm.insert(pair<char,int>('y',200)); mymm.insert(pair<char,int>('z',250)); mymm.insert(pair<char,int>('z',300)); How can I do this? there is way to count number of elements with a key but none to count number of unique keys in a multimap. Added: By unique I mean all the keys in multimap once - they can be repeated or occur once in

How to sort guava multimap? (KEY=DATE)

こ雲淡風輕ζ 提交于 2019-11-30 03:44:07
问题 I have a Multimap<Date,Invoice> multimap = ArrayListMultimap.create(); from guava. I was wondering how to SORT the the Date key in the multimap. Currently, I'm doing this: Iterator<Date> dateItr = multimap.keySet().iterator(); Set<Date> treeSet = new TreeSet<Date>(Collections.reverseOrder()); and later I loop through the treeSet iterator. Any idea how to avoid this circumvention? 回答1: Guava team member here. Use TreeMultimap , or if you need to map into List s, use MultimapBuilder : return

What's the advantage of multimap over map of vectors?

一个人想着一个人 提交于 2019-11-29 20:25:01
I don't understand why multimap exists if we can create map of vectors or map of sets. For me only differences are: using equal_range in multimap for getting elements of a key and in map of vectors we simply use [] operator and have vector of elements. using multimap.insert(make_pair(key,value)) in multimap for adding elements and map_of_vectors[key].push_back(value) in map of vectors. So why use multimap? For me it's better to have a vector than two iterators to get all values of a key. This question applies also to unordered_map of vectors and unordered_multimap. I would say it depends on

multimap vs map with set

Deadly 提交于 2019-11-29 17:17:44
问题 I'm wondering which is more efficient. std::map< String, std::set<int> > or std::multimap< String, int > EDIT: I do not plan on doing anything out of the ordinary with these maps. Standard insert, delete, modify, search. The size of each set or multi keyed String shouldn't be more than 100. 回答1: This I believe is implementation dependant, but an (un)educated guess: In practice it depends on the number of integers that you will be keeping in the multimap or the std::set . A multimap will most

Is there an alternative to Dictionary/SortedList that allows duplicates? [duplicate]

和自甴很熟 提交于 2019-11-29 13:22:58
Possible Duplicate: C# Sortable collection which allows duplicate keys Basically I'd like to make a Dictionary work with duplicate keys without going into custom comparer implementations. There is an idea of: Dictionary<key, List<value>> but it still has some overhead. I wish Dictionary had "AllowDuplicates". If you're using .NET 3.5 then Lookup is probably what you're after. .NET 2.0: PowerCollections contains the OrderedMultiDictionary . You still can use SortedList and try to make a unique key by combining your value and a Guid into a class. In this case, you must implement the IComparer

Erasing elements in a multimap while iterating

怎甘沉沦 提交于 2019-11-29 12:32:58
问题 I'm writing a nodal path finding algorithm. I need to run through a multimap and delete elements out of it under certain conditions, but keep iterating through the multimap. Below is my code so far, it seems to work most of the time, but occasionally I get an error when doing nct_it++. Is it safe to erase the iterator pointer from the table before incrementing the iterator? std::list<SinkSourceNodeConn>::iterator it; std::multimap<SysNode*, SysNode*>::iterator nct_it; SysNode* found_node =

using boost multi_index_container to preserve insertion order

扶醉桌前 提交于 2019-11-29 11:11:34
I initially started out using a std::multimap to store many values with the same key, but then I discovered that it doesn't preserve the insertion order among values with the same key. This answer claims it can be done with boost::multi_index::multi_index_container , but gives no example. Looking through the docs, there are no examples of that usage, and I can't make heads or tails of how you're supposed to use this thing. I have come to expect poor documentation from the lesser-used boost libraries, but this takes the cake. Can anyone point me to a tutorial or example that shows it used the