Difference between HashMap, LinkedHashMap and TreeMap

后端 未结 17 2112
醉话见心
醉话见心 2020-11-22 01:01

What is the difference between HashMap, LinkedHashMap and TreeMap in Java? I don\'t see any difference in the output as all the three

17条回答
  •  北海茫月
    2020-11-22 01:22

    Hash map doesn't preserves the insertion order.
    Example. Hashmap If you are inserting keys as

    1  3
    5  9
    4   6
    7   15
    3   10
    

    It can store it as

    4  6
    5  9
    3  10
    1  3
    7  15
    

    Linked Hashmap preserves the insertion order.

    Example.
    If you are inserting keys

    1  3
    5  9
    4   6
    7   15
    3   10
    

    It will store it as

    1  3
    5  9
    4   6
    7   15
    3   10
    

    same as we insert.

    Tree map stores the vales in Increasing Order Of Keys. Example.
    If you are inserting keys

    1  3
    5  9
    4   6
    7   15
    3   10
    

    It will store it as

    1  3
    3  10
    4   6
    5   9
    7   15
    

提交回复
热议问题