Search code examples
javatreetreemaplinkedhashmap

TreeMap and LinkedHashMap classes


I found a Java question J couldn't understand what should be answer

The TreeMap and LinkedHashMap classes:

  1. enable iteration of a map's entries based on the insertion order of elements only.
  2. enable iteration of a map's entries based on natural ordering of keys only.
  3. enable iteration of a map's entries in a deterministic order.
  4. enable iteration of a map's entries based either natural ordering of keys OR natural ordering of values depending on the arguments sent to the contructor.

Solution

  • In Java:

    1. TreeMap is a Map that automatically sorts the Map entries according to its natural ordering using Comparable<T> interface
    2. LinkedHashMap is Map that guarantees that the entries will be returned in the same order as they were added