Java中List根據對象的屬性值進行資料庫group by功能的操作

來源:互聯網
上載者:User

標籤:

  1 public class test {  2     public static void main(String[] args) {  3   4         List<Bill> list = new test().setObject();  5   6         Set<String> set = new HashSet();  7   8         for (Bill bills : list) {  9             set.add(bills.getTradeTime().substring(0, 8)); 10         } 11  12         List list1 = new ArrayList(); 13         int paySuccess = 0; 14         int dfSuccess = 0; 15         int fee = 0; 16         int count = 0; 17         //遍曆set集合中的日期 18         for (String a : set) { 19             for (Bill bill : list) { 20                 if (a.equals(bill.getTradeTime().substring(0, 8))) { 21                     paySuccess += bill.getMoney(); 22                     fee += bill.getFee(); 23                     count++; 24                     if ("承兌或交易成功".equals(bill.getType())) 25                         dfSuccess += bill.getMoney(); 26                 } 27             } 28             Object[] objects = new Object[10]; 29             objects[0] = new BigDecimal(paySuccess); 30             objects[1] = new BigDecimal(dfSuccess); 31             objects[2] = new BigDecimal(fee); 32             objects[3] = new BigDecimal(paySuccess-fee); 33             objects[4] = a; 34             list1.add(objects); 35             System.out.println("支付成功金額:" + objects[0] + "\t" + "承兌或交易成功: " + objects[1] + "\t" + objects[4] + "\t" + "手續約:" + objects[2] + "\t" + "商戶結算" + 36                     "金額:" + objects[3] + "\t" + "交易筆數:" + count + "\t日期:" + a); 37             paySuccess = 0; 38             dfSuccess = 0; 39             fee = 0; 40             count = 0; 41         } 42         String date = null; 43         System.out.println(date == null || date.isEmpty()); 44     } 45  46     public List<Bill> setObject() { 47         List<Bill> list = new ArrayList<Bill>(); 48         Bill bill = new Bill(); 49         bill.setTradeTime("201605050203"); 50         bill.setMoney(2); 51         bill.setPayType("支付成功"); 52         bill.setType("承兌或交易成功"); 53         bill.setFee(1); 54         list.add(bill); 55  56         bill = new Bill(); 57         bill.setTradeTime("201605060203"); 58         bill.setMoney(3); 59         bill.setType(""); 60         bill.setPayType("支付成功"); 61         bill.setFee(1); 62         list.add(bill); 63  64         bill = new Bill(); 65         bill.setMoney(4); 66         bill.setTradeTime("201605050203"); 67         bill.setPayType("支付成功"); 68         bill.setType("承兌或交易成功"); 69         bill.setFee(1); 70         list.add(bill); 71  72         bill = new Bill(); 73         bill.setType(""); 74         bill.setPayType("支付成功"); 75         bill.setTradeTime("201605060203"); 76         bill.setMoney(4); 77         bill.setFee(1); 78         list.add(bill); 79         return list; 80     } 81 } 82  83 class Bill { 84     int money; 85     String type; 86     String tradeTime; 87     String payType; 88     int fee; 89  90     public String getPayType() { 91         return payType; 92     } 93  94     public void setPayType(String payType) { 95         this.payType = payType; 96     } 97  98     public int getMoney() { 99         return money;100     }101 102     public void setMoney(int money) {103         this.money = money;104     }105 106     public String getType() {107         return type;108     }109 110     public void setType(String type) {111         this.type = type;112     }113 114     public String getTradeTime() {115         return tradeTime;116     }117 118     public void setTradeTime(String tradeTime) {119         this.tradeTime = tradeTime;120     }121 122     public int getFee() {123         return fee;124     }125 126     public void setFee(int fee) {127         this.fee = fee;128     }129 130     @Override131     public String toString() {132         return "Bill{" +133                 "money=" + money +134                 ", type=‘" + type + ‘\‘‘ +135                 ", tradeTime=‘" + tradeTime + ‘\‘‘ +136                 ", payType=‘" + payType + ‘\‘‘ +137                 ‘}‘;138     }139 }
View Code

我的思路是先用set集合對對象的某個屬性中不重複的值進行篩選,然後進行計算

 

如果有更好的方法請在下面留言!

Java中List根據對象的屬性值進行資料庫group by功能的操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.