3

Let's consider the following code:

//...

public Map<String, Integer> getFruits() throws SomeException {
    QueryResult[] queryResults = queryFruits();
    Map<String, Integer> fruits = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    for (QueryResult qr : queryResults) {
        fruits.put(qr.getField("Name").toString(), (Integer) rec.getField("ArticleNumber"));
    }
    return fruits;
}

//...

public static void main(String args[]) {
    App app = new App();
    Map<String, Integer> originalFruits = app.getFruits();
    System.out.println(originalFruits.keySet());
}

– the result of execution will be

[Apple, banana, cherry, Dragon_Fruit, Papaya ]

After that I'm calling getApprovedFuits() and passing originalFruits to it, along with whiteListedFruitNames:

public Map<String, Integer> getApprovedFruits(Map<String, Integer> fruits, Set<String> whiteListedFruitNames) {
    Map<String, Integer> approvedFruits = new TreeMap<>(fruits);
    approvedFruits.keySet().retainAll(whiteListedFruitNames);
    return approvedFruits;
}

//...

public static void main(String[] args) {
    App app = new App();
    Map<String, Integer> originalFruits = app.getFruits();

    // v

    Set<String> whiteListedFruitNames = new HashSet<>(Arrays.asList("Apple",
                                                                    "banana",
                                                                    "cherry",
                                                                    "Dragon_Fruit",
                                                                    "kiwi",
                                                                    "Pineapple"));

    Map<String, Integer> approvedFruits = getApprovedFruits(originalFruits, whiteListedFruitNames);
    System.out.println(approvedFruits.keySet());

}

– the result of the latter println() will look like this:

[Apple, Dragon_Fruit, banana, cherry]

– and I expected to see this:

[Apple, banana, cherry, Dragon_Fruit]

And here is my question: how to make map constructor TreeMap<>(fruits) respect the sorting order of the map that is passed to it? Is there an elegant way to create a new map based on the original one, with the same sorting order?

4

2 Answers 2

6

TreeMap has a constructor from a SortedMap that retains the same Comparator (and thus, the ordering). However, since you're passing your TreeMap as a Map, this constructor is not used - instead, the constructor from a Map is called, and the ordering is lost.

To make a long story short - change getApprovedFruits' signature to use a SortedMap and you should be fine:

public Map<String, Integer> getApprovedFruits
    (SortedMap<String, Integer> fruits, Set<String> whiteListedFruitNames) {
0
3

Two other options in addition to @Mureinik's answer:

  1. Construct your new TreeMap with an explicit comparator:

    Map<String, Integer> approvedFruits = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    approvedFruits.putAll(fruits);
    
  2. If you're not planning on adding to the resulting map, you can use LinkedHashMap which will simply keep the initial insertion order:

    Map<String, Integer> approvedFruits = new LinkedHashMap<>(fruits);
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.