Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33971

Stream question

From Rhino <no_offline_contact@example.com>
Newsgroups comp.lang.java.programmer
Subject Stream question
Date 2016-03-15 14:11 -0400
Organization A noiseless patient Spider
Message-ID <nc9j3k$orv$1@dont-email.me> (permalink)

Show all headers | 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


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