Paul Taylor
Paul Taylor

Reputation: 13120

How do I convert this Java code from using TreeMap to TreeMultiMap?

I have a method thats returns a widget with the highest value for an attribute call source, this works fine except when there are multiple widgets with the same highest value for source, in this case it only returns the last widget added

private Widget getWidgetWithMostSources(List<Widget> Widgets)
{
    SortedMap<Integer,Widget> mapOfWidgetsSortedBySources = new TreeMap<Integer, Widget>();
    for(Widget next:Widgets)
    {
        mapOfWidgetsSortedBySources.put(next.getSources().intValue(), next);
    }
    //Just return the Widget with the highest source value
    Widget Widget = mapOfWidgetsSortedBySources.get(mapOfWidgetsSortedBySources.lastKey());
    return Widget;
}

So im trying to use Google Guavas TreemultiMap instead but I am having two problems,

  1. It doesnt like the generic variables declared when I contruct the TreeMultiMap. Update I think it is because Widget does not implement comparable but I cannot modify the Widget class this is provided by a third party. I only need sorted key and multivalues for keys, I dont need sorted values so perhaps there is another class I could use ?

  2. there is no lastKey() method, so how can I fix this:

Code:

private List<Widget> getWidgetWithMostSources(List<Widget> Widgets) {
    List<Widget> filteredWidgets = new ArrayList<Widget>();
    TreeMultimap<Integer, Widget> mapOfWidgetsSortedBySources = TreeMultimap.create();
    for (Widget next : Widgets) {
        mapOfWidgetsSortedBySources.put(next.getSources().intValue(), next);
    }
    //Just return the Widget with the highest source linked to the acoustid
    Set<Widget> WidgetsWithHighestSources = mapOfWidgetsSortedBySources.get(mapOfWidgetsSortedBySources.keys().lastKey());
    filteredWidgets.addAll(WidgetsWithHighestSources);
    return filteredWidgets;
}

Update

This compiles and I think is okay for what I want:

TreeMultimap<Integer, Recording> mapOfRecordingsSortedBySources = TreeMultimap.create(Ordering.natural(), Ordering.arbitrary());

Upvotes: 0

Views: 378

Answers (1)

talex
talex

Reputation: 20534

First problem may be solved with TreeMultimap<Integer, Widget> mapOfWidgetsSortedBySources = TreeMultimap.create(Ordering.natural(), Ordering.allEqual());

To solve second use asMap() method to get NavigableMap and then call lastKey()

Or you can use lastEntry and save your time on mapOfWidgetsSortedBySources.get call.

In result we have

private List<Widget> getWidgetWithMostSources(List<Widget> widgets) {
    List<Widget> filteredWidgets = new ArrayList<Widget>();
    TreeMultimap<Integer, Widget> mapOfWidgetsSortedBySources = TreeMultimap.create(Ordering.natural(), Ordering.allEqual());
    for (Widget next : widgets) {
        mapOfWidgetsSortedBySources.put(next.getSources().intValue(), next);
    }
    //Just return the Widget with the highest source linked to the acoustid
    Collection<Widget> widgetsWithHighestSources = mapOfWidgetsSortedBySources.asMap().lastEntry().getValue();
    filteredWidgets.addAll(widgetsWithHighestSources);
    return filteredWidgets;
}

WARNING Use Ordering.arbitrary() instead of Ordering.allEqual()

Or better use MultimapBuilder.treeKeys().arrayListValues().build() instead of TreeMultimap.create(Ordering.natural(), Ordering.allEqual());

Upvotes: 1

Related Questions