检索地图中的数据 [英] Retrieving data in a map
问题描述
public class Dashboard {
int REQUEST_ID, PRICE, Status;
String LOGIN_USER;
public int getREQUEST_ID() {
return REQUEST_ID;
}
public void setREQUEST_ID(int rEQUEST_ID) {
REQUEST_ID = rEQUEST_ID;
}
//all getters and setters
public class MapKey {
private Integer id;
private String name;
public MapKey(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override Hashcode and equals
public class DBConnection {
public ArrayList<Dashboard> getStoreResult() {
ArrayList<Dashboard> dashRec;
try{
Class.forName("");
Connection con=DriverManager.getConnection("");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("");
HashMap<Object, List<Dashboard>> map = new HashMap<>();
HashMap<Integer, Integer> mapStatus = new HashMap<>();
while (rs.next()) {
Integer id = rs.getInt(1);
MapKey key = new MapKey(rs.getInt(1), rs.getString(2));
if (!map.containsKey(key)) {
dashRec= new ArrayList<Dashboard>();
map.put(key, dashRec);
}
Dashboard dash = new Dashboard();
dash.setREQUEST_ID(id);
dash.setLOGIN_USER(rs.getString(2));
dash.setPRICE(rs.getInt(3));
dash.setStatus(rs.getInt(4));/////STATUS
map.get(id).add(dash);
if (!mapStatus.containsKey(id) || mapStatus.get(id)>dash.getPROCESSED()){
mapStatus.put(id, dash.getPROCESSED());
}
}
}
}
}
我想创建一个新的Hashmap,其键为req_id + name +最低状态的特定集合No.Here set指的是特定ReqId的行数。例如:对于需求123,我们有7个行构成一个集合。最低状态编号为1.因此值键应为123 A 1.类似地,对于键456,我需要值456 B 2.因此对于示例数据哈希图必须包含3个键和3个值。我想要n个Reqid。每个键的值是arraylist,它由特定id的行作为对象组成。该值在代码中定义。
I want to create a new Hashmap with key as req_id+name+lowest status no of a specific set.Here set refers to number of rows of a particular ReqId. For e.g.:For reqid 123,we have 7rows constituting as a set. The lowest status no is 1.So value key would be 123 A 1.Similarly, for key 456,I need value 456 B 2.So for sample data hashmap must contain 3 keys with 3 values. I want this for n number of Reqid.The value for each key is arraylist which consists of rows of a specific id as objects.The value is defined in the code.
我只想更改地图的键。
推荐答案
首先在外循环中创建地图
Create a map first outside loop
Map<Integer, Integer> mapStatus = new HashMap<>();
然后循环检查并更新地图以获取新的ID集合或更大的现有状态
And in loop check and update map for new id set or existing status in larger
while (rs.next()) {
... // your existing codes
if (!mapStatus.containsKey(id) || mapStatus.get(id)>dash.getStatus()) {
mapStatus.put(id, dash.getStatus());
}
}
这篇关于检索地图中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!