How to create a Multimap from a Map?

Assuming you have Map<String, Collection<String>> map = …; Multimap<String, String> multimap = ArrayListMultimap.create(); Then I believe this is the best you can do for (String key : map.keySet()) { multimap.putAll(key, map.get(key)); } or the more optimal, but harder to read for (Entry<String, Collection<String>> entry : map.entrySet()) { multimap.putAll(entry.getKey(), entry.getValue()); }

is there an iterator across unique keys in a std::multimap?

You can use upper_bound to increment the iterator position instead of ++: #include <map> #include <string> #include <iostream> using namespace std; int main() { multimap<int,string> mm; mm.insert(make_pair(1, “a”)); mm.insert(make_pair(1, “lemon”)); mm.insert(make_pair(2, “peacock”)); mm.insert(make_pair(3, “angel”)); for( auto it = mm.begin(), end = mm.end(); it != end; it = mm.upper_bound(it->first) ) cout << it->first << ‘ ‘ … Read more

stl::multimap – how do i get groups of data?

pair<Iter, Iter> range = my_multimap.equal_range(“Group1”); int total = accumulate(range.first, range.second, 0); Is one way. Edit: If you don’t know the group you are looking for, and are just going through each group, getting the next group’s range can be done like so: template <typename Pair> struct Less : public std::binary_function<Pair, Pair, bool> { bool operator()(const … Read more

Duplicate keys in .NET dictionaries?

If you’re using .NET 3.5, use the Lookup class. EDIT: You generally create a Lookup using Enumerable.ToLookup. This does assume that you don’t need to change it afterwards – but I typically find that’s good enough. If that doesn’t work for you, I don’t think there’s anything in the framework which will help – and … Read more

Bidirectional multi-valued map in Java

So you need support for many-to-many relationships? Closest you can get is Guava‘s Multimap like @Mechkov wrote – but more specifically Multimap combination with Multimaps.invertFrom. “BiMultimap” isn’t implemented yet, but there is an issue requesting this feature in Google Guava library. At this point you have few options: If your “BiMultimap” is going to immutable … Read more

multimap in .NET

Because it’s almost christmas 🙂 ////////////////////////////////////////////////////////////////////// // Algorithmia is (c) 2008 Solutions Design. All rights reserved. // http://www.sd.nl ////////////////////////////////////////////////////////////////////// // COPYRIGHTS: // Copyright (c) 2008 Solutions Design. All rights reserved. // // The Algorithmia library sourcecode and its accompanying tools, tests and support code // are released under the following license: (BSD2) // ———————————————————————- // … Read more