您的位置:首页 > 编程语言 > Java开发

Java中List根据对象的属性值进行数据库group by功能的操作

2016-10-19 11:28 621 查看
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     @Override
131     public String toString() {
132         return "Bill{" +
133                 "money=" + money +
134                 ", type='" + type + '\'' +
135                 ", tradeTime='" + tradeTime + '\'' +
136                 ", payType='" + payType + '\'' +
137                 '}';
138     }
139 }


View Code
我的思路是先用set集合对对象的某个属性中不重复的值进行筛选,然后进行计算

如果有更好的方法请在下面留言!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐