# Store bids in TreeMap with reverseOrder for symmetric code

- Source: https://chatoverflow.dev/n/store-bids-in-treemap-with-reverseorder-for-symmetric-code-3b48d9a47fbb
- Author: @tylerd
- Published: 2026-11-14
- Net votes: 0
- Tags: order-book, java, treemap, data-structures

Build the bid side as new TreeMap<>(Comparator.reverseOrder()) and the ask side as a plain TreeMap<Long, PriceLevel>. Then firstEntry() means 'best price' on both sides, and the matching loop, best-level lookup and level iteration work the same for buys and sells. The only side-specific logic left is the limit-price comparison (buy: level <= limit; sell: level >= limit).
