gpt4 book ai didi

java - 如何动态地将arraylist放入Hashmap中?

转载 作者:行者123 更新时间:2023-11-30 07:47:03 25 4
gpt4 key购买 nike

public HashMap viewTableData(String tabName)
{
ArrayList<String> arrayList = new ArrayList<String>();
ArrayList<String> arrayList2 = new ArrayList<String>();
ArrayList<String> arrayList3 = new ArrayList<String>();
ArrayList<String> arrayList4 = new ArrayList<String>();
HashMap discoverMap = new HashMap();

try {
int i=1;
con = DAOConnection.sqlconnection();
stmt = con.createStatement();
stmt = con.createStatement();
query = "SELECT * FROM "+tabName;
System.out.println("Qry executed");
ps = con.prepareStatement(query);
rs = ps.executeQuery(query);
while(rs.next()){
while(i<=4){
arrayList.add(rs.getString(i));
//arrayList2.add(rs.getString(2));
discoverMap.put("qrycol"+i, arrayList);
i++;
}
}
// discoverMap.put("qrycol1", arrayList);
// discoverMap.put("qrycol2",arrayList2);
System.out.println("Map result:"+discoverMap);
} catch (SQLException ex) {
Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
//return null;
}
return discoverMap;
}

在这里您可以看到,我编写了一个方法,该方法将从数据库表中获取值并将其放入 Hashmap 中。

如果我初始化 4 个不同的数组列表,并添加 rs.getString(1),rs.getString(2),... 那么它就可以正常工作。但我想要一些动态功能,它将创建所需的数组列表并添加 rs.getString(i)。但它正在运行但没有得到期望的输出。 这里discoverMap显示结果: map 结果:{qrycol1=[1, Indranil, Banerjee, indranil1111@gmail.com], qrycol3=[1, Indranil, Banerjee,indranil1111@gmail.com], qrycol2=[1, Indranil, Banerjee, indranil1111@gmail.com], qrycol4=[1, Indranil, Banerjee, indranil1111@gmail.com]}

但是对于下面显示期望输出的代码:

public HashMap viewTableData(String tabName)
{
ArrayList<String> arrayList = new ArrayList<String>();
ArrayList<String> arrayList2 = new ArrayList<String>();
ArrayList<String> arrayList3 = new ArrayList<String>();
ArrayList<String> arrayList4 = new ArrayList<String>();
HashMap discoverMap = new HashMap();

try {
int i=1;
con = DAOConnection.sqlconnection();
stmt = con.createStatement();
stmt = con.createStatement();
query = "SELECT * FROM "+tabName;
System.out.println("Qry executed");
ps = con.prepareStatement(query);
rs = ps.executeQuery(query);
while(rs.next()){
arrayList.add(rs.getString(1));
arrayList2.add(rs.getString(2));
arrayList3.add(rs.getString(3));
arrayList4.add(rs.getString(4));
}
discoverMap.put("qrycol1", arrayList);
discoverMap.put("qrycol2",arrayList2);
discoverMap.put("qrycol3",arrayList3);
discoverMap.put("qrycol4",arrayList4);
System.out.println("Map result:"+discoverMap);
}

catch (SQLException ex) {
Logger.getLogger(Query.class.getName()).log(Level.SEVERE, null, ex);
//return null;
}
return discoverMap;
}

map 结果:{qrycol1=[1, 2, 3, 4, 6, 5, 7, 8], qrycol3=[Banerjee, JJJ, Saun, Gupta, efgh,aaaa, surname22, Sir], qrycol2=[ Indranil, pora, trao, Som, James, Anup, Abhi, Mia], qrycol4=[indranil111@gmail.com, pc@gmail.com, tit@gmail.com, so@gmail.com, ind@gmail.com, anu@gg.com、abhi@gg.com、mai@gg.com]}

这实际上是正确的。但我想要它是动态的。因为一直以来我们都无法假设需要多少个 arraylist。如果有人在这方面提供帮助,那将非常有帮助。提前致谢 。

数据库表结构:有4列,类型为varchar(但其他表可能有所不同,这就是我问这个问题的原因。)

最佳答案

需要知道从数据库返回的列数,如果知道可以按照:

        int numOfColumns = 6; // it can be any number you need

// initialize the map
for (int colIndex = 1; colIndex <= numOfColumns; ++colIndex) {
String key = "qrycol" + colIndex;
ArrayList<String> list = new ArrayList<>();
discoverMap.put(key, list);
}

while (rs.next()) {
// for each column got from DB, populate the corresponding array list
for (int colIndex = 1; colIndex <= numOfColumns; ++colIndex) {
String key = "qrycol" + colIndex;
ArrayList<String> list = (ArrayList<String>) discoverMap.get(key);
list.add(rs.getString(colIndex));
}
}

关于java - 如何动态地将arraylist放入Hashmap中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33780567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com