I have the following scenario where I try to order map values by the key. After The key order the first value has to be value which was order by.
import java.util.Comparator;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
public class MyEnumKeySortTest {
enum Key{
key1, key2
}
enum Door{
door1, door2
}
public static void main(String[] args) {
Map<Key, Door> test = ImmutableMap.<Key, Door> of(Key.key1, Door.door1, Key.key2, Door.door2);
printValues(test);
Comparator<Key> comparator = Ordering.explicit(Key.key1);
/*
* should print
*
* door1
* door2
*/
test = Maps.newTreeMap(comparator);
printValues(test);
Comparator<Key> comparator2 = Ordering.explicit(Key.key2);
/*
* should print
*
* door2
* door1
*/
test = Maps.newTreeMap(comparator2);
printValues(test);
}
private static void printValues(Map<Key, Door> test) {
System.out.println("==========================================================");
for(Door r : test.values()){
System.out.println(r.name());
}
}
}