我的目标是创建地图。
它以一组的reqid
,name
和最低的status
号作为键。
这里的set指的是属于特定Reqid
的行。
映射的值是一个Arraylist,其中包含作为特定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);
}
}
输出如下:
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],...,..]
它仅捕获直到
reqid
456的数据,但是我的代码无法捕获映射中的最后一个唯一的reqid
行数据。SQL数据库:
最佳答案
试试这个,
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);
}
}
更新:如果无法将值设置为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);
}