You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java城市建筑模拟程序:按年份查询的历史数据结构选型咨询

Data Structure Recommendations for Your Java City Skyline Simulation

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 TreeMap uses year integers as keys, which keeps your data sorted chronologically out of the box.
  • The value is a custom YearlySkylineData class 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), and maxHeight (tallest building that year).

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 HashMap lets you remove a building (when it's demolished) with remove(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:
    1. Get or create the YearlySkylineData entry for the target year in the TreeMap.
    2. Add the building to the inner HashMap.
    3. Update the cached metrics (add to total height, increment count, check if new height is the new max).
  • When demolishing a building:
    1. Retrieve the YearlySkylineData for the relevant year.
    2. Remove the building from the HashMap, adjust total height and count.
    3. 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 TreeSet for automatic max tracking if you want to optimize further).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:22:31