0

Is there any Java SortedHashMap equivalent library in Python? Sorting dict keys in python every time we insert a key in dict doesn't look like an efficient approach.

I went through some reference implementations in python by third-party libraries similar to Java SortedHashMap but they don't look convincing.

Is there any stable and popular python implementation for this? which is already being used in some big opensource projects ?

One reference implementation sorted dict implementation

4
  • 1
    When you say "SortedHashMap", do you just mean SortedMap? Because in general a map with sorted keys doesn't need to use hashes. Commented Jun 13, 2021 at 11:45
  • So if SortedMap doesn't use hashing then searching for the key will not be a constant time operation right? either it will be a binary search or linear search Commented Jun 13, 2021 at 11:51
  • 1
    Exactly, see docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html for example. Both getting elements and inserting elements will be O(log n). Commented Jun 13, 2021 at 11:54
  • Thanks, Got your point , Searching can max O(log n) in SortedMap. You can put this in answer and I will mark it as accepted. Commented Jun 13, 2021 at 11:59

1 Answer 1

1

Actually, there is no official library in python that provides the SortedHashMap implementation so there is no point looking for this. You can implement your own own by using similar concept that hava uses.

You can refer to this article for more details. TreeMap implementation in Java

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.