1

I am trying to find a way to get a sorted multimap. I checked Guava which offers two separate collection data structures that combined could answer my problem. What's striking is that SortedSetMultimap can not contain identical keys because it's a Set while Multimap is in the same symbol. What's up with that?

EDIT

In C++, I've got something like that, which hopefully will tell you how I intend to use similar functionality in Java:

  struct KeyCompare : public binary_function<pair<double,double>, pair<double,double>, bool>
  {
    bool operator()(const pair<double,double>& p1, const pair<double,double>& p2) const;
  };

  multimap<pair<double,double>, Object*, KeyCompare> _list;

  .../...

  KeyCompare key_compare;

  while (( ! _list.empty() && key_compare(_list.begin()->first, k))) {

    u = _list.begin()->second;
    v = _list.begin()->first;

In English: I need a map with keys being pairs of doubles referencing some object contained in the value. The map can contains multiple keys that are identicals (same pair of double numbers happens), so this needs to be a multimap or bag. When this occurs, the element can be inserted in the collection and when picking the top element, it should return one of those (doesn't matter which one). I need for the collection to be mutable because for each iteration, I am picking the lesser element and removing it from the map.

John Difool
  • 5,572
  • 5
  • 45
  • 80

1 Answers1

3

Are you possibly looking for MultimapBuilder.treeKeys().arrayListValues().build() ?

If you needed to pass a comparator, that might look like

 MultimapBuilder.treeKeys(new Comparator<Pair<Double, Double>>() {
      @Override public int compare(Pair<Double, Double> p1, Pair<Double, Double> p2) {
        return ComparisonChain.start()
            .compare(p1.first(), p2.first())
            .compare(p1.second(), p2.second())
            .result();
      }
    }).arrayListValues().build();
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • I have yet to figure out how I can build a multimap sorted by keys that are pairs with this construct. However one big downside is the first sentence in the description: "MultimapBuilder instances are immutable". I need to remove the first element for each iteration. Copying the collection each time would be very expensive. – John Difool Jul 18 '15 at 19:42
  • It is the builder itself that is immutable, not the built multimap. And to use pairs, all you have to do is pass a comparator for your pair type to treeKeys. – Louis Wasserman Jul 18 '15 at 19:44
  • Would you mind adding a sample code to your reply. I am not quite up-to-speed with Guava collections. – John Difool Jul 18 '15 at 19:50
  • Is that what you're looking for? – Louis Wasserman Jul 18 '15 at 23:16
  • This looks really helpful. I'll give it a shot. I'll do more reading because the arrayListValues() purpose isn't clear to me? – John Difool Jul 19 '15 at 00:11
  • 1
    You have to decide how you should collect multiple values for a given key -- in a set, in a list, etc. – Louis Wasserman Jul 19 '15 at 00:12
  • I'm thinking about removing the [tag:android-guava] tag and replacing it everywhere with [tag:guava] and [tag:android]... do you think there's a good reason to keep the tags distinct? – durron597 Aug 08 '15 at 23:13
  • @durron597 it seems to be a reference to an entirely independent project, but then, Guava is working on improving its utility for Android independently. – Louis Wasserman Aug 08 '15 at 23:16
  • Android-guava isn't run by Google? It didn't even occur to me that might be the case. Well, if that's so, they should definitely be separate tags. – durron597 Aug 08 '15 at 23:18