2017-05-05 15 views
0

我有以下情形:如何獲取所有值的HashMap使用globalMap.get()

Map<String, HashSet<String>> B = new HashMap<String, HashSet<String>>(); 

if(!B.containsKey(row5.issue)){ 
    B.put(row5.issue,new HashSet<String>()); 
} 

B.get(row5.issue).add(row5.name); 
globalMap.put("BMap", B); 

現在,我已經在另一個子作業以下腳本:

"select * from Table where name in ('" + BMap.values() + "');" 

粗體部分有問題,任何人都可以提供建議?非常感謝。

+0

什麼'BMap.values()'返回? –

+0

我希望它返回在第一段腳本中定義的HashMap B中的所有值。感謝您的回覆! – Fulton

+0

還不清楚,你提供一個例子嗎? –

回答

1

遍歷您的BMap並基於它的值來構建一個字符串來查詢數據庫。我假設您的HashSet<String>在您的HashMap<String, HashSet<String>>中包含名稱。

String sql_statement = ""; 
for(HashSet<String> names : BMap.values()){ 
    for(String name : names){ 
    sql_statement += "'" + name + "',"; 
    } 
} 
sql_statement = sql_statement.substring(0, sql_statement.length-1); // to remove the comma at the end 

和SELECT語句將是這樣的:

"select * from Table where name in (" + sql_statement +");" 

我沒有測試過這一點,但它應該給你如何解決這個問題的總體思路。

+0

非常感謝您的詳細回覆,您是否知道我們是否可以在sql腳本中解決這個問題,我的意思是在where子句中? – Fulton

+0

SQL腳本是否與Java程序分離? – DCON

+0

是的,Java程序在第一份工作中,而sql腳本在第二份工作中。 – Fulton