Skip to content
CodeKruPro

TreeMap order versus LinkedHashMap order

Predict the outputMediumJava onlyAsked in: Capgemini, Wipro
Main.java
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> tree = new TreeMap<>();
        Map<String, Integer> linked = new LinkedHashMap<>();
        for (Map<String, Integer> m : List.of(tree, linked)) {
            m.put("banana", 1);
            m.put("apple", 2);
            m.put("cherry", 3);
            m.put("banana", 4);
        }
        System.out.println(tree);
        System.out.println(linked);
    }
}

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in