DEV Community

Basu
Basu

Posted on

Stop, think about it. Are you using the right Data Structure?

A very trivial thing but knowing which data structure or implementation maintains the order of insertion, allows duplicates, or does not accept null helps prevent unexpected outcomes. Had a similar first-hand experience a few months ago. 💀

For example, lets take Map interface. There are three classes that implements Map in java - HashMap, LinkedHashMap and TreeMap.

♨️ HashMap doesn't maintain insertion order, LinkedHashMap does, TreeMap maintains order in sorted manner.
♨️ HashMap and LinkedHashMap allows one null key object (K,V), TreeMap doesn't (it may have multiple null values, never the keys).
♨️ HashMap and LinkedHashMap both have O(1) insertion and search. TreeMap, has O(logN). Bonus - TreeMap is implemented using RB-Tree, HM - LinkedLists, LHM - Doubley-LL. These implementation are the reason for these TC.

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay