gpt4 book ai didi

java - 从表中获取数据并存储在 hashmap 中

转载 作者:行者123 更新时间:2023-12-01 08:58:55 25 4
gpt4 key购买 nike

我有一个包含 5 列的表格 (id |state_abbrevation | state_name | area_code | 城市)。我必须将所有值存储为键值对,其中 key= state_abbrevationValue = (area_code) cities .

state_abbrevation 也有重复项。示例数据如下所示:

id | state_abbrevation |  state_name  | area_code |       cities        
----+-------------------+--------------+-----------+---------------------
1 | AK | Alaska | 907 | Juneau
2 | AL | Alabama | 205 | Birmingham
3 | AL | Alabama | 251 | Mobile
4 | AL | Alabama | 256 | Huntsville
5 | AL | Alabama | 334 | Montgomery
6 | AL | Alabama | 938 | Florence/Huntsville
7 | AR | Arkansas | 479 | Ft. Smith
8 | AR | Arkansas | 501 | Little Rock
9 | AR | Arkansas | 870 | Jonesboro
10 | AZ | Arizona | 480 | Scottsdale
11 | AZ | Arizona | 520 | Tucson
12 | AZ | Arizona | 602 | Phoenix
13 | AZ | Arizona | 623 | Glendale
14 | AZ | Arizona | 928 | Flagstaff
15 | CA | California | 209 | Modesto
16 | CA | California | 213 | Los Angeles
17 | CA | California | 310 | West Hollywood
18 | CA | California | 323 | Hollywood

存储在键值对中的最佳解决方案是什么,其中 key = AL & Value = 所有 state_abbrevation= AL 的区号和城市。

我想要的 HashMap 示例:核心值(value)AK,(907) 朱诺AL,(205)伯明翰 (251) 手机 (256) 亨茨维尔 [938] 938 佛罗伦萨.......等等。这是我使用 Hibernate 的工作代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class HibernateCriteriaExamples {

public static void main(String[] args) {
try {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

Criteria criteria = session.createCriteria(State.class);
//List<State> stateList = criteria.list();

List<String> stateAbbrevationList = criteria.setProjection(Projections.distinct(Projections.property("stateAbbrevation"))).list();
HashMap<String,List> cityAreacodeAndState = new HashMap<String,List>();

for(int i=0; i<stateAbbrevationList.size();i++)
{
String abbrevation = stateAbbrevationList.get(i);
//System.out.println(abbrevation);
Criteria criteriaareaCodeWithCity = session.createCriteria(State.class);
List<State> stateObject = criteriaareaCodeWithCity.add(Restrictions.eq("stateAbbrevation", abbrevation)).list();
List<String> formattedAreaCodeAndCity =new ArrayList<String>();
for(int j=0; j<stateObject.size();j++)
{
State state = (State)stateObject.get(j);
int a = state.getAreacode();
String b = state.getCities();
String c = "("+a+") "+b;
// System.out.println(c);
formattedAreaCodeAndCity.add(c);
}
cityAreacodeAndState.put(abbrevation, formattedAreaCodeAndCity);

}
System.out.println("---------------PRINTING REQUIRED DATA------------------");

for (HashMap.Entry<String,List> formattedAreaCodeAndCity1 : cityAreacodeAndState.entrySet())
{
System.out.println(formattedAreaCodeAndCity1.getKey() + "," + formattedAreaCodeAndCity1.getValue());
}

tx.commit();
sessionFactory.close();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

最佳答案

您可以使用HashMap以及List

您的 HashMap 键将为 state_abbrevation 并且您的值(即 List)将包含 area_code城市

HashMap<String, List<String>> data = new HashMap<>();
..
while(rs.next()) {
List<String> temp = new ArrayList<>();
temp.add(rs.getString("area_code"));
temp.add(rs.getString("cities"));

data.put(rs.getString("state_abbrevation"), temp);
}

获取与状态CA相关的数据

data.get("CA").get(0) //   -> gives area_code related to state "CA"
data.get("CA").get(1) // -> gives cities related to state "CA"

关于java - 从表中获取数据并存储在 hashmap 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41850783/

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