在地图中获取最后一行的数据 [英] Getting last row's data in the map

查看:66
本文介绍了在地图中获取最后一行的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是创建地图.

它具有reqidname和最低的status号作为键集.

It have reqid,name and lowest status number of a set as a key.

此处设置是指属于特定Reqid的行.

Here set refers to rows belonging to a specific Reqid.

地图的值是一个 Arraylist ,其中包含所有行作为特定id的对象.

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);
            }
        }

输出类似于:

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行数据.

It captures data only till reqid 456, However my code cant capture last unique reqid row data in the map.

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,则给出第二个答案

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);
        }

这篇关于在地图中获取最后一行的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆