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

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

问题描述

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插入哈希图中作为对象,而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?

推荐答案

如果我清楚地了解您的要求,则希望按requestId的升序对Map进行排序,然后对Dashboard的列表进行排序以status的升序记录具有相同requestId的记录.如果是这样,下面给出的是相同的解决方案:

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记录列表进行排序,我已经使用requestId作为键获取了Dashboard记录列表,然后对List进行了排序>使用Comparator.comparing(Dashboard::getProcessed).
  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数据库中获取属性值作为哈希图中的用户定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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