问题
My objective is to create a map.
It have reqid
,name
and lowest status
number of a set as a key.
Here set refers to rows belonging to a specific Reqid
.
The value of the map is an Arraylist which consists of all rows as objects of a specific id
.
public class MapKey {
private Integer id;
private String name;
private Integer status;
public MapKey(Integer id, String name,Integer status) {
this.id = id;
this.name = name;
this.status=status;
}
@Override toString,hashCode,equals
public class Dashboard {
int REQUEST_ID;
String name;
int price;
int status;
public int getREQUEST_ID() {
return REQUEST_ID;
}
public void setREQUEST_ID(int rEQUEST_ID) {
REQUEST_ID = rEQUEST_ID;
}
//getters and setters
public class DBConnection {
public ArrayList<Dashboard> getStoreResult() {
ArrayList<Dashboard> dashRec=new ArrayList<Dashboard>();
Dashboard dash = new Dashboard();
try{
Class.forName("");
Connection con=DriverManager.getConnection("");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("");
HashMap<Object, List<Dashboard>> map = new HashMap<>();
int status=100;
int reqid=0;
String name1=null;
while (rs.next()) {
Integer id = rs.getInt(1);
String name=rs.getString(2);
if(reqid==0) {
reqid=id;
name1=name;
}
if(reqid !=id) {
MapKey key = new MapKey(reqid,name1,status);
map.put(key, dashRec);
reqid=id;
name1=name;
dashRec= new ArrayList<Dashboard>();
status=100;
}
dash = new Dashboard();
dash.setREQUEST_ID(id);
dash.setLOGIN_USER(name);
dash.setPRICE(rs.getInt(3));
dash.setSTATUS(rs.getInt(4));
dashRec.add(dash);
if(rs.getInt(4)<=status) {
status=rs.getInt(4);
}
}
The output is like:
MapKey [reqid=123, name=A,status=1]:[Dashboard [reqid=123, NAME=A,
PRICE=5,STATUS=2],Dashboard [reqid=123, NAME=A, PRICE=10,STATUS=3],...,..]
It captures data only till reqid
456, However my code cant capture last unique reqid
row data in the map.
SQL Database:
回答1:
try this,
Map<MapKey, List<Dashboard>> map = new HashMap<>();
while (rs.next()) {
int reqId = rs.getInt(1);
String name = rs.getString(2);
int price = rs.getInt(3);
int status = rs.getInt(4);
//iterate map look for existing data
boolean exist = false;
for (Map.Entry<MapKey, List<Dashboard>> entry : map.entrySet()) {
MapKey mapKey = entry.getKey();
List<Dashboard> dashboards = entry.getValue();
if (mapKey.getId()==reqId && mapKey.getName().equals(name)) {
exist = true;
//check for status and update it if lower than existing status
if (status < mapKey.getStatus())
mapKey.setStatus(status);
//add dashboard
Dashboard dashboard = new Dashboard();
dashboard.setREQUEST_ID(reqId);
dashboard.setName(name);
dashboard.setPrice(price);
dashboard.setStatus(status);
dashboards.add(dashboard);
}
}
if (!exist) {
MapKey mapKey = new MapKey(reqId, name, status);
Dashboard dashboard = new Dashboard();
dashboard.setREQUEST_ID(reqId);
dashboard.setName(name);
dashboard.setPrice(price);
dashboard.setStatus(status);
List<Dashboard> dashboards = new ArrayList<>();
dashboards.add(dashboard);
map.put(mapKey, dashboards);
}
}
Update: 2nd answer if cannot set values to MapKey
Map<Map<String, Object>, List<Dashboard>> map = new HashMap<>();
while (rs.next()) {
int reqId = rs.getInt(1);
String name = rs.getString(2);
int price = rs.getInt(3);
int status = rs.getInt(4);
//iterate map look for existing data
boolean exist = false;
for (Map.Entry<Map<String, Object>, List<Dashboard>> entry : map.entrySet()) {
Map<String,Object> mapKey = entry.getKey();
List<Dashboard> dashboards = entry.getValue();
if (mapKey.get("id").toString().equals(reqId) && mapKey.get("name").toString().equals(name)) {
exist = true;
//check for status and update it if lower than existing status
if (status < Integer.valueOf(mapKey.get("status").toString()))
mapKey.put("status", status);
//add dashboard
Dashboard dashboard = new Dashboard();
dashboard.setREQUEST_ID(reqId);
dashboard.setName(name);
dashboard.setPrice(price);
dashboard.setStatus(status);
dashboards.add(dashboard);
}
}
if (!exist) {
Map<String,Object> mapKey = new HashMap<>();
mapKey.put("id", reqId);
mapKey.put("name", name);
mapKey.put("status", status);
Dashboard dashboard = new Dashboard();
dashboard.setREQUEST_ID(reqId);
dashboard.setName(name);
dashboard.setPrice(price);
dashboard.setStatus(status);
List<Dashboard> dashboards = new ArrayList<>();
dashboards.add(dashboard);
map.put(mapKey, dashboards);
}
}
Map<MapKey, List<Dashboard>> map0 = new HashMap<>();
for (Map.Entry<Map<String, Object>, List<Dashboard>> entry : map.entrySet()) {
Map<String,Object> mapKey = entry.getKey();
List<Dashboard> dashboards = entry.getValue();
MapKey mapKey0 = new MapKey(
Integer.valueOf(mapKey.get("id").toString()),
mapKey.get("name").toString(),
Integer.valueOf(mapKey.get("status").toString()));
map0.put(mapKey0, dashboards);
}
来源:https://stackoverflow.com/questions/61851846/getting-last-rows-data-in-the-map