按年,

时间:2016-04-21 12:24:02

标签: java arrays

我有一个像这样的数组:

[{auction_qty=1, value_of_sale=250, month=10, trans_qty=1, year=2012, user=335, trans_price=250, ts=1351968521}, {auction_qty=1, value_of_sale=100, month=10, trans_qty=1, year=2012, user=335, trans_price=100, ts=1351970719}, {auction_qty=1, value_of_sale=350, month=10, trans_qty=1, year=2012, user=335, trans_price=350, ts=1351970489}, {auction_qty=1, value_of_sale=135, month=10, trans_qty=1, year=2012, user=335, trans_price=135, ts=1351964112}, {auction_qty=1, value_of_sale=499, month=10, trans_qty=1, year=2012, user=335, trans_price=499, ts=1351965308}, {auction_qty=1, value_of_sale=100, month=10, trans_qty=1, year=2012, user=335, trans_price=100, ts=1352462230}, {auction_qty=1, value_of_sale=100, month=10, trans_qty=1, year=2012, user=335, trans_price=100, ts=1352462298}, {auction_qty=1, value_of_sale=100, month=10, trans_qty=1, year=2012, user=335, trans_price=100, ts=1352462278}, {auction_qty=1, value_of_sale=120, month=10, trans_qty=1, year=2012, user=335, trans_price=120, ts=1352643128}, {auction_qty=1, value_of_sale=300, month=10, trans_qty=1, year=2012, user=335, trans_price=300, ts=1352642805}, {auction_qty=1, value_of_sale=150, month=10, trans_qty=1, year=2012, user=335, trans_price=150, ts=1352234180}, {auction_qty=1, value_of_sale=100, month=10, trans_qty=1, year=2012, user=335, trans_price=100, ts=1353269022}, {auction_qty=1, value_of_sale=350, month=10, trans_qty=1, year=2012, user=335, trans_price=350, ts=1354105741}, {auction_qty=1, value_of_sale=500, month=10, trans_qty=1, year=2012, user=335, trans_price=500, ts=1353864141}, {auction_qty=1, value_of_sale=2000, month=11, trans_qty=1, year=2012, user=335, trans_price=2000, ts=1354541234}, {auction_qty=1, value_of_sale=100, month=11, trans_qty=1, year=2012, user=335, trans_price=100, ts=1354892155}, {auction_qty=1, value_of_sale=100, month=11, trans_qty=1, year=2012, user=335, trans_price=100, ts=1355256131}, {auction_qty=1, value_of_sale=100, month=11, trans_qty=1, year=2012, user=335, trans_price=100, ts=1355051037}, {auction_qty=1, value_of_sale=1000, month=11, trans_qty=1, year=2012, user=335, trans_price=1000, ts=1355001978}, {auction_qty=1, value_of_sale=1000, month=11, trans_qty=1, year=2012, user=335, trans_price=1000, ts=1355001980}, {auction_qty=1, value_of_sale=1000, month=11, trans_qty=1, year=2012, user=335, trans_price=1000, ts=1355001981}, {auction_qty=1, value_of_sale=500, month=11, trans_qty=1, year=2012, user=335, trans_price=500, ts=1355414838}, {auction_qty=1, value_of_sale=400, month=11, trans_qty=1, year=2012, user=335, trans_price=400, ts=1355040754}, {auction_qty=1, value_of_sale=400, month=11, trans_qty=1, year=2012, user=335, trans_price=400, ts=1355040797}, {auction_qty=1, value_of_sale=400, month=11, trans_qty=1, year=2012, user=335, trans_price=400, ts=1355040908}, {auction_qty=1, value_of_sale=100, month=11, trans_qty=1, year=2012, user=335, trans_price=100, ts=1356248959}, {auction_qty=1, value_of_sale=500, month=11, trans_qty=1, year=2012, user=335, trans_price=500, ts=1356713457}, {auction_qty=1, value_of_sale=300, month=11, trans_qty=1, year=2012, user=335, trans_price=300, ts=1356968997}, {auction_qty=1, value_of_sale=150, month=0, trans_qty=1, year=2013, user=335, trans_price=150, ts=1357082245}, {auction_qty=1, value_of_sale=100, month=0, trans_qty=1, year=2013, user=335, trans_price=100, ts=1357082264}, {auction_qty=1, value_of_sale=400, month=0, trans_qty=1, year=2013, user=335, trans_price=400, ts=1357482640}, {auction_qty=1, value_of_sale=400, month=0, trans_qty=1, year=2013, user=335, trans_price=400, ts=1357482642}, {auction_qty=1, value_of_sale=400, month=0, trans_qty=1, year=2013, user=335, trans_price=400, ts=1357482638}, {auction_qty=1, value_of_sale=400, month=0, trans_qty=1, year=2013, user=335, trans_price=400, ts=1357981401}, {auction_qty=2, value_of_sale=400, month=0, trans_qty=1, year=2013, user=335, trans_price=400, ts=1357979518}]

我想按用户,年份和月份对其进行分组。接下来我想在用户,年份和月份中求和(trans_qty)和sum(value_of_sale)。

我创建它的代码如下所示:

for (Long user : users) {
            final Statement stmt = new Statement();
            stmt.setNamespace(xxxx);
            stmt.setSetName(xxxxx);
            stmt.setFilters(Filter.equal("u", user));

            final RecordSet records = aerospikeClient.query(null, stmt);
            StreamSupport.stream(records.spliterator(), true)
                    .flatMap(l -> {
                        final ArrayList<HashMap> auctionsFromRecord = (ArrayList<HashMap>) l.record.getValue("v");
                        return Optional.ofNullable(auctionsFromRecord).isPresent() ? auctionsFromRecord.stream() : Stream.<HashMap>empty();
                    })
                    .map(r -> parseAuction(r))
                    .collect(Collectors.toList())
                    .forEach(row -> {
                        if (row.getT().size() > 0) {

                            for (Transaction trans : row.getT()) {
                                if (trans.getTs() >= tsFrom && trans.getTs() <= tsTo) {
                                    Map record = new HashMap();
                                    Timestamp ts = new Timestamp(trans.getTs() * 1000);
                                    Calendar cal = Calendar.getInstance(Locale.getDefault());
                                    cal.setTime(new Date(ts.getTime()));
                                    int year = cal.get(Calendar.YEAR);
                                    int month = cal.get(Calendar.MONTH);
                                    int day = cal.get(Calendar.DATE);

                                    record.put("year", year);
                                    record.put("month", month);
                                    record.put("day", day);
                                    record.put("user", row.getUser());
                                    record.put("trans_price", trans.getPrice());
                                    record.put("trans_qty", trans.getQty());
                                    record.put("value_of_sale", trans.getQty() * trans.getPrice());
                                    record.put("ts", trans.getTs());
                                    record.put("auction_qty", row.getQty());
                                    table.add(record);
                                }
                            }
                        }
                    });
        }

我应该如何用Java做到这一点?

1 个答案:

答案 0 :(得分:0)

创建一个Map数组,让我们说records来保存您正在创建的每个record地图,然后遍历records并获取要添加的所需属性。简单