对于这种情况,最佳数据结构的选择是什么?

发布于 2024-10-10 23:59:46 字数 337 浏览 1 评论 0 原文

看看我的 GUI 部分 alt text

一旦用户从分支 JComboBox 中选择特定分支,我必须查询数据库并获取所有适用的年份到该分支并将这些年份添加到下一个 JComboBox 年份,依此类推。用户有相当多的机会在他选择的分支之间进行交换,并且我会发现每次他更改选项时自己都会再次查询数据库相同的查询,并且数据库中的数据不太可能发生变化在这些交换之间进行更改......所以我决定将它们存储在某种数据结构中,对于这样的数据结构,我的最佳选择是什么?可能有2到3个不同的分支,4到6个不同的年份等等......
我最好的选择是什么?

Once have a look at the part of my GUI
alt text

Once a user selects a particular branch from the branch JComboBox, i have to query the database and get all the years applicable to that branch and add those years to the next JComboBox year and so forth. There is quite a bit of chance for the user to swap between his selection of branch, and i would find myself querying the database the same query again each time he changes his option, and it is highly unlikely that the data in the database is going to change in between these swapping.... So i decided that i store these in some data structure, what is the best choice i have for one such datastructure? there may be 2 to 3 different branches, 4 to 6 different years and so on.....
What is my best choice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江湖彼岸 2024-10-17 23:59:46

创建一个 Map,其中分支是键,年份、学期和部分位于 VO 对象中,怎么样?假设 VO 名为 BranchDetails,那么您可以使用类似 Map() 的内容。

BranchDetails 可以像下面这样简单:

class BranchDetails{
   List<Integer> years;
   List<Integer> semesters;
   List<Integer> sections;
   //getters and setters omitted for brevity 
}

How about you create a Map where branch is the key and years, semesters and sections are in a VO object? Let's assume the VO is named BranchDetails then you could use something like Map<String,BranchDetails>().

BranchDetails could be something as simple as below:

class BranchDetails{
   List<Integer> years;
   List<Integer> semesters;
   List<Integer> sections;
   //getters and setters omitted for brevity 
}
§对你不离不弃 2024-10-17 23:59:46

如果应用程序是单线程的,请使用 HashMap。您可以“嵌套”HashMap,例如HashMap>>。请参阅这个问题来了解如何迭代HashMap

下面是一些示例代码,仅使用两个嵌套来为您提供想法:

HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, String>>>> map1 = new HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, String>>>>();
map1.put("Computers", new HashMap<Integer, HashMap<Integer, HashMap<String, String>>>());
map1.get("Computers").put(2011, new HashMap<Integer, HashMap<String, String>>());
map1.get("Computers").get(2011).put(2, new HashMap<String, String>());

If the application is single-threaded, use a HashMap. You can "nest" the HashMaps, e.g. HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, Item>>>>. See this question to see how to iterate over the HashMap.

Here's some sample code using just two nestings to give you the idea:

HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, String>>>> map1 = new HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, String>>>>();
map1.put("Computers", new HashMap<Integer, HashMap<Integer, HashMap<String, String>>>());
map1.get("Computers").put(2011, new HashMap<Integer, HashMap<String, String>>());
map1.get("Computers").get(2011).put(2, new HashMap<String, String>());
梦行七里 2024-10-17 23:59:46

我只是将它们与完整数据保存在单独的列表中。 (它们很小)。

List<Branch> branches;
List<Year> years;
List<Semester> semesters;
List<Section> sections;

其中每个对象Branch、Year、Semester、Section都有必要的数据将它们链接到另一个。例如,Branch 可以包含一个 yearId 列表 等,尽可能接近实际的数据库结构(以使初始加载快速而简单)。

一旦 UI 中的状态发生更改,您只需复制此数据结构上的实际数据库查询并返回您需要的内容。

I would just keep them in separate lists with the complete data. (they are a small).

List<Branch> branches;
List<Year> years;
List<Semester> semesters;
List<Section> sections;

where each object Branch, Year, Semester, Section has the neccessary data to link them to another. For example a Branch could contains a list of yearId, etc mapping as closely as possible the actual database structure (to make the initial loading quick and easy).

Having that once you have a state change in the UI you just replicate the actual database query on this data structure and return what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文