我有一个查询,它将返回如下两个值(A列和B列)
Repository
我正在尝试创建一个Java方法(Java 7),该方法将一次性读取所有这些值并将其存储在集合变量(Map)中,就像以下格式的所有值一样
A B
------------
a aaa
a aaa
a aaa
a aaa
b bbb
c ccc
c ccc
b bbb
c ccc
b bbb
下面是我正在尝试的方法,但首先我什至无法提取所有数据:
(a -> (aaa,aaa,aaa,aaa,aaa),
b -> (bbb,bbb,bbb),
c -> (ccc,ccc,ccc))
上面的代码给出了以下结果
import java.sql.*;
import java.util.ArrayList;
public class CollectionFrame {
public static void main(String[] args) {
try {
// step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
// step2 create the connection object
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr");
// step3 create the statement object
Statement stmt = con.createStatement();
// step4 execute query
// Lists of Lists to store the values
ArrayList<ArrayList<String>> listOLists = new ArrayList<ArrayList<String>>();
ArrayList<String> obj = new ArrayList<String>();
ResultSet rs = stmt.executeQuery("select * from t");
while (rs.next()) {
// System.out.println(rs.getString(1) + " " + rs.getString(2));
obj.add(rs.getString(1));
// obj.add(rs.getString(2));
listOLists.add(obj);
obj.removeAll(obj);
}
// step5 close the connection object
con.close();
System.out.println(listOLists.toString());
} catch (Exception e) {
System.out.println(e);
}
}
}
如果我取消注释行[[a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b], [a, a, a, a, b, c, c, b, c, b]]
,我将得到以下信息:
obj.removeAll(obj);
我被困在这里。有人可以帮助我继续进行或提出更好的解决方案吗?
答案 0 :(得分:2)
您应该为此使用地图。
ResultSet rs = stmt.executeQuery("select * from t");
Map<String, List<String>> valueMap = new HashMap<>();
while (rs.next()) {
String columnAstring = rs.getString(1);
String columnBstring = rs.getString(2);
valueMap.putIfAbsent(columnAstring, new ArrrayList<>());
valueMap.get(columnAstring).add(columnBstring);
}
编辑:所以putifabsent可能效率很低,因为我将创建和丢弃很多arraylist。正如@Andreas指出的那样。因此,这将使清洁程度降低一点点,但执行起来会更有效!
与JAVA 7兼容
ResultSet rs = stmt.executeQuery("select * from t");
Map<String, List<String>> valueMap = new HashMap<>();
while (rs.next()) {
String columnAstring = rs.getString(1);
String columnBstring = rs.getString(2);
if(!valueMap.containsKey(columnAstring)){
valueMap.put(columnAstring, new ArrayList());
}
valueMao.get(columnA).add(columnBstring);
}
使用Java 8 Lambdas
@Mureinik的answer指出了使用以下更干净的方法 computeIfAbsent。
while (rs.next()) {
String columnAstring = rs.getString(1);
String columnBstring = rs.getString(2);
valueMap.computeIfAbsent(columnAstring, k -> new ArrayList<>())
valueMap.get(columnAstring).add(columnBstring);
}
答案 1 :(得分:1)
我将遍历ResultSet
并将更改应用于地图Map<String, List<String>>
。在每次迭代中,如果键(A列)不存在,则需要添加一个空列表,然后在确定有该键的列表之后,将值添加到B列。幸运的是, Java 8对Map
接口的改进使得它非常优雅:
Map<String, List<String>> result = new HashMap<>();
while (rs.next()) {
String a = rs.getString("a");
String b = rs.getString("b");
result.computeIfAbsent(a, k -> new ArrayList<>()).add(b);
}
答案 2 :(得分:1)
也许您可以尝试一下。这使用了HashMap。
public class Main {
public static void main(String[] args) {
//sample CSV strings...pretend they came from a file
String[] csvStrings = new String[] {
"a aaa","a aaa","a aaa","a aaa","b bbb","b bbb","b bbb",
"b bbb","c ccc","c ccc","c ccc"
};
List<List<String>> csvList = new ArrayList<List<String>>();
Map<String,ArrayList<String>> outVal
= new HashMap<String, ArrayList<String>>();
for(String val:csvStrings){
String outList[] = val.split(" ");
if (outVal.containsKey(outList[0])){
outVal.get(outList[0]).add(outList[1]);
} else {
ArrayList<String> inputList = new ArrayList<String>();
inputList.add(outList[1]);
outVal.put(outList[0],inputList);
}
}
System.out.println(outVal.toString());
}
}
这是输出: {a = [aaa,aaa,aaa,aaa],b = [bbb,bbb,bbb,bbb],c = [ccc,ccc,ccc]}