Java城市建筑模拟程序:按年份查询的历史数据结构选型咨询
Hey there! Let's walk through the best data structure picks for your university project—focused on meeting your core needs: year-based queries, reusable string keys for buildings, and efficient calculation of metrics like average skyline height and maximum building height.
Top Recommendation: TreeMap<Integer, YearlySkylineData>
What it is:
- The outer
TreeMapuses year integers as keys, which keeps your data sorted chronologically out of the box. - The value is a custom
YearlySkylineDataclass that wraps:- A
HashMap<String, Integer>to map building IDs (your string keys) to their heights. - Cached metrics:
totalHeight(sum of all active building heights),buildingCount(number of active buildings), andmaxHeight(tallest building that year).
- A
Why it works for your project:
- Year-based queries are a breeze: TreeMap supports O(log n) lookups by year—so getting 2016's skyline data or 2020's max height is fast. You can also easily fetch ranges (e.g., all data from 2015-2025) using methods like
subMap(), which is handy if you ever need to analyze trends over time. - Reusable building keys: The inner
HashMaplets you remove a building (when it's demolished) withremove(buildingId), then reuse the same string key for a new building later—no extra hoops needed. Insertions and lookups for individual buildings are O(1), which is efficient even as your city grows. - Cached metrics save time: Instead of recalculating average height (
totalHeight / buildingCount) or max height every time you query, you update these values when buildings are added or removed. This makes metric queries instant, which is clean and efficient for a university project.
Alternative: HashMap<Integer, YearlySkylineData>
If you don't need chronological ordering (e.g., you only ever query specific years, never ranges), a HashMap as the outer structure works too. It has slightly faster O(1) lookups for exact years, but loses the built-in sorted order that TreeMap provides. For most skyline simulations, though, TreeMap's sorted behavior is more useful for visualizing or analyzing changes over time.
Quick High-Level Notes
- When adding a building:
- Get or create the
YearlySkylineDataentry for the target year in the TreeMap. - Add the building to the inner HashMap.
- Update the cached metrics (add to total height, increment count, check if new height is the new max).
- Get or create the
- When demolishing a building:
- Retrieve the
YearlySkylineDatafor the relevant year. - Remove the building from the HashMap, adjust total height and count.
- If the demolished building was the max, re-calculate the new max (a quick loop is acceptable for small project data sizes, or you could use a
TreeSetfor automatic max tracking if you want to optimize further).
- Retrieve the
This setup hits all your requirements, uses standard Java collections (no external libraries needed), and is straightforward to implement without overcomplicating things.
内容的提问来源于stack exchange,提问作者SuperBodo

