2016-04-21 65 views
0

I`v有像這樣的數組:逐年Java的分組排列,月

[{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)和總和(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中?

+0

從哪裏得到這個數組 – esprittn

+0

查找我添加了循環創建它 – rad11

回答

0

創建的Map陣列可以說records通過records持有每record地圖,你創造,是橫越以後,讓你想添加所需的屬性值。簡單

+0

是啊我知道很簡單,當我們考慮它,但林新的Java和我的硬我不知道我怎麼可以將數據分組我看到的多維數組可能有些表明瞭如何做到這一點?我認爲最好的方法應該加入forEach – rad11