Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #33971
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Rhino <no_offline_contact@example.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Stream question |
| Date | Tue, 15 Mar 2016 14:11:31 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 54 |
| Message-ID | <nc9j3k$orv$1@dont-email.me> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Tue, 15 Mar 2016 18:08:52 -0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="30ccb22752576586cadf10c6ca0a52b5"; logging-data="25471"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/koFSUxxVKh1RihY6Ag/I1HfnVulhK0ss=" |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 |
| X-Mozilla-News-Host | news://news.eternal-september.org:119 |
| Cancel-Lock | sha1:HB5N7TuvKGnZPj2rmNhXyPBhmrc= |
| Xref | csiph.com comp.lang.java.programmer:33971 |
Show key headers only | View raw
I'm new to streams but very intrigued with the possibilities. I'm trying to write a stream that does grouping, counting and summing at the same time. The data involved is actually quite simple but writing the streaming statement I need is proving challenging and I'm not seeing anything really helpful in Google searches. First, let me describe my data, then I'll show you how I've solved two-thirds of the problem. Perhaps you can tell me how to fit in the missing piece. The data is ticket sales from a company that sells concert tickets. Each sale consists of an agency code, an order number, order date and the number of tickets sold. Therefore, it looks like this: AgencyCode OrderNumber OrderDate TicketsSold ---------- ----------- --------- ----------- TW 111111 2016-03-01 4 TW 111112 2016-03-01 2 CP 201000 2016-03-01 3 TW 111113 2016-03-01 8 CP 201001 2016-03-02 2 EL 300001 2016-03-01 4 AS 400000 2016-03-02 2 What I'm trying to get out of this data is a summary showing the total number of orders for each agency code and the total number of tickets sold for that same agency code. Therefore, the values I want to get for this particular set of data is: AgencyCode Orders TicketsSold TW 3 14 CP 2 5 EL 1 4 AS 1 2 I've got the grouping working and also the number of tickets sold. It's just the counting of the orders that I'm trying to get. Here's how I got the tickets sold by agency: Map<String, Integer> salesByAgency = ticketOrders.stream() .collect(Collectors.groupingBy(TicketSale::getAgencyCode, Collectors.summingInt(TicketSale::getTicketsSold))); TicketSale is the class that holds a single ticket order. My collection, ticketOrders, is a LinkedHashSet holding a bunch of TicketSale records. How do I adjust what I have to get the number of orders for each agency code? -- Rhino
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread
Stream question Rhino <no_offline_contact@example.com> - 2016-03-15 14:11 -0400
Re: Stream question Sebastian <news@seyweiler.dyndns.org> - 2016-03-15 21:21 +0100
Re: Stream question Rhino <no_offline_contact@example.com> - 2016-03-16 00:44 -0400
csiph-web