我有 Mysql 表,我想将查询返回的值推送到 java hashmap 中。 我是java的初学者。我从可用答案中获得了一些帮助并编写了代码。 摘要:我有国家/地区代码列表,如connectedCountryList = [40,203,250,528],它应该是 HashMap 的键 我想迭代这些国家/地区代码并存储来自具有数百万行的 SQL 表的返回值。 Mysql 表示例数据如下所示-
utc date area_in area_out value
2015-12-05T03:00Z 05.12.2015 03:00 276 40 3059
2015-12-04T03:00Z 04.12.2015 03:00 276 40 2523
2015-12-07T03:00Z 07.12.2015 03:00 276 40 2873
2015-12-06T03:00Z 06.12.2015 03:00 276 40 3386
2015-12-01T03:00Z 01.12.2015 03:00 276 40 2936
2015-12-03T03:00Z 03.12.2015 03:00 276 40 2564
2016-04-01T19:00Z 01.04.2016 19:00 276 203 -291
2016-05-02T05:00Z 02.05.2016 05:00 276 203 -276
2016-05-03T05:00Z 03.05.2016 05:00 276 203 -123
2016-04-03T19:00Z 03.04.2016 19:00 276 203 -899
2016-04-02T19:00Z 02.04.2016 19:00 276 203 -1100
2016-05-01T05:00Z 01.05.2016 05:00 276 203 -45
2015-01-16T13:00Z 16.01.2015 13:00 276 203 -339
2016-04-05T19:00Z 05.04.2016 19:00 276 203 734
2015-01-17T13:00Z 17.01.2015 13:00 276 203 -300
2017-04-27T19:00Z 27.04.2017 19:00 276 250 -75
2016-09-25T23:00Z 25.09.2016 23:00 276 250 -174
2015-03-18T12:00Z 18.03.2015 12:00 276 250 338
2016-10-11T09:00Z 11.10.2016 09:00 276 250 1093
2015-03-17T12:00Z 17.03.2015 12:00 276 250 190
2017-05-24T05:00Z 24.05.2017 05:00 276 250 -454
2015-03-14T03:00Z 14.03.2015 03:00 276 528 3336
2016-05-09T00:00Z 09.05.2016 00:00 276 528 2705
2016-11-12T19:00Z 12.11.2016 19:00 276 528 3030
2015-03-15T03:00Z 15.03.2015 03:00 276 528 3182
2016-10-15T05:00Z 15.10.2016 05:00 276 528 2111
实际上我有很多代码,这是我正在处理的一小部分。
connectedCountryList =[40,203,250,528];
String tablename= "mytable";
MarketAreacode = "276"
public class Exchangeflows{
private String utc;
private int value;
public Exchangeflows(){}
public Exchangeflows(String utc,int value ){
this.utc =utc;
this.value=value;
}
public String getutc(){
return utc;
}
public void setutc(String UTC) {
this.utc = UTC;
}
public int getvalue(){
return value;
}
public void setvalue(int value) {
this.value = value;
}
}
final Map<Integer, Exchangeflows> CbExchangePower = new HashMap<Integer, Exchangeflows>();
for (final Integer countryCode : connectedCountryList) {
final String sqlquery = "select * from " + tableName +" where ((area_in = '" + marketAreaCode +"' "+" and area_out in ('"+countryCode+"'))"+
"or (area_out = '"+marketAreaCode+"' "+"and area_in in ('" + countryCode +"'))) order by utc ASC ";
conn.setResultSet(sqlquery);
// SQL query Executes perfect
while (conn.resultSet.next()){
String utc = conn.resultSet.getString("utc");
int value = conn.resultSet.getInt("value");
PowerExchange = new Exchangeflows (utc, value);
CbExchangePower.put(countryCode,PowerExchange);
}
当我运行代码时,它会在每次迭代中正确读取数据,但每次它都会覆盖这些值,并且我会根据 UTC ASC 根据 sql 查询获取最新值。例如[2015-12-07T03:00Z 07.12.2015 03:00 276 40 2873 ]
。我不明白我应该如何在循环中迭代我的sql以将表的所有内容以<countryCode <utc, value>>
的形式保存到hashmap中。 HashMap 将遍历国家/地区列表,因此根据 for 循环,国家/地区代码在每次迭代中都是唯一的,并且必须在 while 循环中添加 sql 表中的每一行。
任何人都可以建议我对代码进行任何更改吗?
最佳答案
为了避免覆盖,您可以相互交换键值。所以,你的 HashMap 将是 CbExchangePower<Exchangeflows, Integer>
然后将数据库中的值按 CbExchangePower.put(PowerExchange,countryCode)
放入.
为了根据值读取Map,您可以执行如下操作:
Map<Integer, List<Exchangeflows>> countryCodes = new HashMap<Integer, List<Exchangeflows>>();
for(Entry<Exchangeflows,Integer> entry : CbExchangePower.entrySet()){
List<Exchangeflows> list = new ArrayList<Exchangeflows>();
if(countryCodes.containsKey(entry.getValue()))
list = countryCodes .get(entry.getValue());
list.add(entry.getKey());
countryCodes .put(entry.getValue(), list);
}
System.out.println(countryCodes);
这应该能够打印包含您所需值的列表。
关于java - 将Mysql表内容(sql查询返回的行)推送到java hashmap中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46214912/