如果你看看你的代碼,你會找到一個叫做「代碼氣味」(重複代碼)的東西。你有自己的三個參數結構繼續重複自己:步驟,關鍵字和對象。只是看到我建議把在一類,以便有:
public class YourClass{
private String keyword;
private String step;
private String object;
//It´s your choice whether to have setters or have everything in through constructor
public String getKeyword(){
return this.keyword;
}
.... //continue with getters for step and object
}
現在你可以有HashMap中的你的常規結構,從而有地圖,這意味着他們爲尋找到地圖的關鍵將是一個字符串「TC_01」,「TC_02」,當你得到YourClass返回時,你所要做的就是調用.getKeyword()...來獲取其餘的信息。
例子:
YourClass yourClassVariable = new YourClass();
yourClassVariable.setStep("TS01");
yourClassVariable.setKeyword("navigate");
yourClassVariable.setObject("search_fare");
Map<String,YourClass> mp=new HashMap<String, YourClass>();
// adding or setting elements in Map by put method key and value pair
mp.put("TC_01", yourClassVariable);
:)
你要什麼用數據做什麼?到目前爲止,你有沒有嘗試過任何東西,以至於遇到麻煩? – SimonC 2013-02-18 13:50:27
該特定示例對我來說看起來像一個Map>,但在大多數實際應用程序中,您都希望將其轉換爲具有真實屬性的實際業務對象。 –
2013-02-18 13:51:55
這不會是JSON格式,而是一個數組列表......我需要在一個循環中構建這個結構來執行基於關鍵字 – 2013-02-18 13:54:30