簡體   English   中英

在這種情況下,最佳數據結構的選擇是什么?

[英]What is the choice of best data structure for this situation..?

看看我的GUI部分 替代文字

一旦用戶從分支JComboBox中選擇了一個特定的分支,我就必須查詢數據庫並獲取適用於該分支的所有年份,並將這些年份添加到下一個JComboBox年,依此類推。 用戶有很大的機會在他選擇的分支之間進行切換,並且我發現自己每次更改選項時都會再次以相同的查詢查詢數據庫,並且數據庫中的數據極不可能出現在這些交換之間進行切換。...所以我決定將它們存儲在某種數據結構中,對於這樣一種數據結構,我有什么最佳選擇? 可能會有2到3個不同的分支機構,4到6個不同的年份,依此類推.....
我最好的選擇是什么?

您如何創建一個地圖,其中分支是關鍵,而年,學期和節則位於VO對象中? 假設VO名為BranchDetails,則可以使用Map<String,BranchDetails>()

BranchDetails可能很簡單,如下所示:

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

如果應用程序是單線程的,請使用HashMap 您可以“嵌套” HashMap ,例如HashMap<String, HashMap<Integer, HashMap<Integer, HashMap<String, Item>>>> 看到這個問題 ,看看如何遍歷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>());

我只是將它們與完整的數據放在單獨的列表中。 (它們很小)。

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

每個對象的“ Branch, Year, Semester, Section都有必要的數據將它們鏈接到另一個對象。 例如,一個Branch可能包含一個list of yearId ,等等,以盡可能接近實際數據庫結構的方式進行映射(以使初始加載變得快速而容易)。

在UI中進行狀態更改后,只需在此數據結構上復制實際的數據庫查詢,然后返回所需的內容即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM