Omega Collision
Omega Collision

Reputation: 125

Sorting the values inside a TreeMap java

EDIT: Solved. I needed to switch the order of the definition to Integer,Action

I have created a one-to-one TreeMap where the keys are Enums and the values are Integers. I want to loop through the values from least to greatest and I'm having some trouble.

Function which creates the Map

public TreeMap<Action,Integer> mapMoves(int position, ArrayList<Action> directions){
    TreeMap<Action,Integer> map = new TreeMap<>();
    for(Action a : directions){
        switch(a){
            case UP:
                map.put(a,board.get(position-3));
                break;
            case DOWN:
                map.put(a,board.get(position+3));
                break;
            case LEFT:
                map.put(a,board.get(position-1));
                break;
            case RIGHT:
                map.put(a,board.get(position+1));
                break;
        }
    }
    return map;
}

When I run the following for loop it does not print the values in ascending order.

TreeMap<Action, Integer> map = current.hashMoves(emptyIndex, possibleMoves);
for (Map.Entry<Action, Integer> entry : map.entrySet()) {
    System.out.println(entry.getKey() + ": " + entry.getValue());
 }

Upvotes: 1

Views: 175

Answers (1)

James W.
James W.

Reputation: 3055

I'm guessing Action is Enum, Enum already implements Comparable.

It uses the order the enum constants are defined in, but sadly you can't override the compareTo-method to achieve lexicographical ordering because its is defined as final.

But you can pass a custom Comparator to the TreeMap.

Either switch Map to <Integer,Action> or sort by value using

static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
    Comparator<K> valueComparator = (k1, k2) -> {
        int compare = map.get(k2).compareTo(map.get(k1));
        if (compare == 0) return 1;
        else return compare;
    };
    Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
    sortedByValues.putAll(map);
    return sortedByValues;
}
static TreeMap<Action,Integer> mapMoves() {
    List<Action> directions = new ArrayList<>();
    directions.add(Action.DOWN);
    directions.add(Action.UP);
    directions.add(Action.UP);
    directions.add(Action.UP);
    directions.add(Action.LEFT);
    directions.add(Action.LEFT);
    directions.add(Action.RIGHT);
    directions.add(Action.RIGHT);
    TreeMap<Action,Integer> map = new TreeMap<>();
    for(Action a : directions){
        switch (a){
            case UP:
                map.put(a, 10);
                break;
            case DOWN:
                map.put(a, 2);
                break;
            case LEFT:
                map.put(a, 30);
                break;
            case RIGHT:
                map.put(a, 4);
                break;
        }
    }
    return map;
}

enum Action {
    UP, DOWN, LEFT, RIGHT
}

main

    TreeMap<Action, Integer> map = mapMoves();
    map.entrySet().stream().forEach(e -> System.out.println("e = " + e.getKey() + ": " + e.getValue()));
    System.out.println("- - -");
    Map<Action, Integer> sortedMapByValuesDescOrder = sortByValues(map);
    sortedMapByValuesDescOrder.entrySet().stream().forEach(e -> System.out.println("e = " + e.getKey() + ": " + e.getValue()));

output will be

e = UP: 10
e = DOWN: 2
e = LEFT: 30
e = RIGHT: 4
- - -
e = LEFT: 30
e = UP: 10
e = RIGHT: 4
e = DOWN: 2

Upvotes: 1

Related Questions