2016-04-21 14 views
0

I`v bekamen einen Array wie folgt:Java Gruppierung Array nach Jahr, Monat

[{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}] 

ich es zu einer Gruppe von Benutzern, Jahr und Monat mag. Als nächstes möchte ich Summe (trans_qty) und Summe (value_of_sale) in Benutzer, Jahr und Monaten addieren.

Mein Code, es sieht aus wie schafft:

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); 
           } 
          } 
         } 
        }); 
     } 

Wie kann ich tun, dass in Java?

+0

von wo haben Sie dieses Array – esprittn

+0

Look up Ich fügte Schleife, die es erstellen – rad11

Antwort

0

Erstellen Sie ein Array von Maprecords sagen lässt jede record Karte zu halten, die Sie erstellen, nach dem Verfahrweg durch records und den Wert des gewünschten Attribut erhalten, die Sie hinzufügen möchten. Simple

+0

Ja, ich weiß, ist einfach, wenn wir darüber nachdenken, aber ich bin neu in Java und für mich ist es schwer ich weiß nicht, wie ich Daten gruppieren kann mehrdimensionales Array, das ich zeige, vielleicht einige angegeben, wie geht das? Ich denke, der beste Weg sollte in ForEach hinzugefügt werden – rad11