我有一个包含5列的表格(id
| state_abbrevation
| state_name
| area_code
| cities
)。我必须将所有值存储为key= state_abbrevation
和Value
= (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&值=区号和城市所有state_abbrevation = AL。
我想要的Hashmap示例: 核心价值 AK,(907)朱诺 AL,(205)伯明翰 (251)移动 (256)亨茨维尔 (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();
}
}
}
答案 0 :(得分:1)
您可以使用HashMap
以及List
。
您的HashMap
密钥为state_abbrevation
,您的值(即List
)将包含area_code
和cities
。
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"
答案 1 :(得分:0)
正如评论中提到的,您可以使用值对象来保存状态数据:
public Map<String, State> getStatesMap() {
... execute query and get resultSet
Map<String, State> result = new HashMap<>();
while (resultSet.next()) {
result.put(
resultSet.getString("state_abbrevation"),
new State(
resultSet.getString("area_code"),
resultSet.getString("cities")
)
);
}
return result;
}
private class State {
private final String area;
private final String cities;
public State(String area, String cities) {
this.area = area;
this.cities = cities;
}
public String getArea() {
return area;
}
public String getCities() {
return cities;
}
}