2

as java.util.LinkedHashmap exist and what is the advantage to use import com.google.gson.internal.LinkedTreeMap;

When i searched, i got the results comparison between HashMap, LinkedHashMap and TreeMAp but i want to know the performance of LinkedTreeMap different from either LinkedHashMap or TreeMap.

Please any one clarify me.

  • http://stackoverflow.com/questions/2889777/difference-between-hashmap-linkedhashmap-and-treemap – Anton Belev May 16 '17 at 11:15
  • 1
    Since `LinkedTreeMap` is apparently internal to gson, why do you need to know? – Andy Turner May 16 '17 at 11:27
  • Hi Andy, in our automation scripts my seniors used `LinkedTreeMap` where ever need, at the same place even `LinkedHashMap` is working, i want to know which is better to use. –  May 17 '17 at 05:11
  • It is always better to use classes from a public API as chances are better that it does not change unexpectedly. – Henry May 17 '17 at 05:13
  • Thanks for your reply Henry –  May 17 '17 at 05:19

1 Answers1

2

For the performance part:

Hashmap and LinkedHashMap supports O(1) get/put operations complexity time. LinkedHashMap preserves the order of the inserted items.

TreeMap supports O(log n) get/put operations complexity time. Since it has a mechanism to preserver natural order of the items using Comparable or Comparator.

KAD
  • 10,972
  • 4
  • 31
  • 73
  • but what can be the performance of `LinkedTreeMap`, can you please tell about this. As for as i know even LinkedTreeMap also preserves the order of the insertion. –  May 17 '17 at 05:21
  • From the implementation, it is so similar to the `TreeMap`, it uses `Comparable` and `Comparator` as well. I guess they have the same performance complexity. https://github.com/google/gson/blob/master/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java – KAD May 17 '17 at 05:25
  • Thanks for reply, i have less reputation to upvote your answer, once i get the required reputation, definitely i will upvote your answer. –  May 17 '17 at 05:28