从sql数据库中获取属性值作为hashmap中的用户定义对象 [英] Get attributes value from sql database as user defined object in hashmap

查看:49
本文介绍了从sql数据库中获取属性值作为hashmap中的用户定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Dashboard {
   int REQUEST_ID, PRICE, PROCESSED;           
   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 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<>();
      while (rs.next()) {
        Integer id = rs.getInt(1);
        if (!map.containsKey(id)) {
            dashRec= new ArrayList<Dashboard>();
            map.put(id, dashRec);
        }
        Dashboard dash = new Dashboard();
        dash.setREQUEST_ID(id);
        dash.setLOGIN_USER(rs.getString(2));
        dash.setPRICE(rs.getInt(3));
        dash.setPROCESSED(rs.getInt(4));
        map.get(id).add(dash);
      }
   }
  }
}

我想在上面的哈希图中添加名称和状态作为用户定义的对象.名称必须像 A 表示前 3 组,B 表示后 2 组行.我要插入的状态必须是相同 ID 的行集中的最低编号.即对于 ID 123,我们需要将状态为 1 作为对象插入到 hashmap 中,对于 id 456,我们需要状态 2.如何实现?

I want to add name and status as user defined object in above hashmap. The name must be like A for first 3 set, B for next 2 set of rows.The status I want to insert must be lowest number in the set of rows of same ID. That is with ID 123, we need status as 1 to be inserted as object in hashmap and for id 456 we need status 2. How would it be done?

推荐答案

如果我清楚你的需求,你想按照 requestIdMap 进行排序> 然后按照 status 的升序对具有相同 requestIdDashboard 记录列表进行排序.如果是这样,下面给出了相同的解决方案:

If I understood your requirement clearly, you want to sort the Map in the ascending order of the requestId and then sort the list of Dashboard records with the same requestId in ascending order of status. If so, given below is the solution for the same:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

class Dashboard {
    private int requestId, price, processed;
    String loginUser;

    public Dashboard(int requestId, String loginUser, int price, int processed) {
        this.requestId = requestId;
        this.price = price;
        this.processed = processed;
        this.loginUser = loginUser;
    }

    public int getRequestId() {
        return requestId;
    }

    public int getPrice() {
        return price;
    }

    public int getProcessed() {
        return processed;
    }

    public String getLoginUser() {
        return loginUser;
    }

    @Override
    public String toString() {
        return requestId + "\t" + price + "\t" + processed + "\t" + loginUser;
    }
}

public class Main {
    public static void main(String[] args) {
        Map<Object, List<Dashboard>> map = new TreeMap<>();
        List<Dashboard> list;

        list = new ArrayList<Dashboard>();
        list.add(new Dashboard(456, "B", 25, 2));
        list.add(new Dashboard(456, "B", 20, 3));
        map.put(456, list);

        list = new ArrayList<Dashboard>();
        list.add(new Dashboard(123, "A", 10, 2));
        list.add(new Dashboard(123, "A", 15, 3));
        list.add(new Dashboard(123, "A", 5, 1));
        map.put(123, list);

        list = new ArrayList<Dashboard>();
        list.add(new Dashboard(789, "C", 30, 1));
        map.put(789, list);

        // Sort the list of Dashboard records with the same requestId in ascending order
        // of status
        for (Entry<Object, List<Dashboard>> entry : map.entrySet()) {
            Collections.sort(entry.getValue(), Comparator.comparing(Dashboard::getProcessed));
        }

        // Display Result
        System.out.println("reqid\tname\tprice\tstatus");
        for (List<Dashboard> recordList : map.values()) {
            for (Dashboard record : recordList) {
                System.out.println(record.getRequestId() + "\t" + record.getLoginUser() + "\t" + record.getPrice()
                        + "\t" + record.getProcessed());
            }
        }
    }
}

输出:

reqid   name    price   status
123     A       5       1
123     A       10      2
123     A       15      3
456     B       25      2
456     B       20      3
789     C       30      1

注意事项:

  1. 我使用了 TreeMap根据其键的自然顺序排序.由于键是 int requestId,我们不需要做任何事情来比较它们.TreeSet 会自动按照升序来处理.
  2. 要按照status的升序对具有相同requestIdDashboard记录列表进行排序,我得到了的列表Dashboard 使用 requestId 作为键记录,然后使用 Comparator.comparing(Dashboard::getProcessed)List 进行排序.
  1. I have used TreeMap which is sorted according to the natural ordering of its keys. Since the keys are int requestId, we don't need to do anything to compare them. The TreeSet will automatically take care of keeping them in ascending order.
  2. To sort the list of Dashboard records with the same requestId in ascending order of status, I have got the list of Dashboard records using the requestId as the key and then sorted the List using Comparator.comparing(Dashboard::getProcessed).

这篇关于从sql数据库中获取属性值作为hashmap中的用户定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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