Yuen Lian Wu

Greenhorn
+ Follow
since Jul 11, 2015
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Yuen Lian Wu

Fortitude cooL wrote:Thank you for your attention CampBell. The original code looks like following. I was trying to have 1 block of code doing everything(the commented part) but it doesn't work and have syntax error

8 years ago
Thanks Campbell for your reminder. I have changed my name.

My general purpose is to have one operation does the aggregation and populate the result into list of enriched objects. Little bit like the following example from Oracle, but will aggregated into a list instead.

8 years ago
Thank you for your attention CampBell. The original code looks like following. I was trying to have 1 block of code doing everything(the commented part) but it doesn't work and have syntax error

List<SweepActivity> aggregatedActivities = activities.stream()
.collect(Collectors.groupingBy(sa -> sa.getClientBookID() + sa.getImntID() + sa.getExchangeID()))
.entrySet().stream()
.map(e -> e.getValue().stream()
.reduce((f1, f2) -> new SweepActivityImpl(SweepActivity.EventType.AGGREGATED.name(), f1.getClientBookID(), null, null, null, null,
f1.getImntID(), f1.getPrice(), f1.getRIC(), f1.getInstrumentType(), f1.getExchangeID(), f1.getCountryOfRegistration(), f1.getTradeCurrency(),
f1.getQuantity().add(f2.getQuantity()), f1.getSweepDate())))
.map(f -> f.get())
.collect(Collectors.toList());


// List<PositionSweep> aggregatedActivities = activities.stream()
// .collect(Collectors.groupingBy(sa -> sa.getClientBookID() + sa.getImntID() + sa.getExchangeID()))
// .entrySet().stream()
// .map(e -> e.getValue().stream()
// .reduce((f1, f2) -> new PositionSweepImpl(f1.getClientBookID(), f1.getImntID(), f1.getRIC(), f1.getInstrumentType(), null,
// f1.getExchangeID(), f1.getTradeCurrency(), f1.getQuantity().add(f2.getQuantity()), null, f1.getSweepDate())))
// .map(f -> f.get())
// .collect(Collectors.toList(PositionSweep));

//Populate into PositionSweep object. Will think about how to utilize Stream process to remove this part
List<PositionSweep> positionSweeps = new ArrayList<PositionSweep>();
for (SweepActivity activity:aggregatedActivities) {
PositionSweep ps = new PositionSweepImpl(activity.getClientBookID(), instrument, buySellType.name(),
activity.getTradeCurrency(), activity.getQuantity(), activity.getPrice(), activity.getSweepDate());

positionSweeps.add(ps);
}
8 years ago
Hi All,

I am new to Java8 Stream operation. Basically I want to simplify my line of codes in Java8 by exactly knowing the behaviour and limit of .map operation.

I have a list of trade objects called activities which contain name/ID of the stock, quantity and name of the holder. It might be same holder on same instrument multiple time due to different trade executions in a day.
I want to sum of all the quantities aggregated by stock ID and Holder. During my streaming operation I would like to put the aggregated result in another enriched object called AggregatedTrade object which it have almost identical data structure as the Trade object itself.

Code Snippet
--------------



As you can see, I don't know much about Streaming. I split the process into 2. First aggregated into it's own object then use another for loop to populate into my enriched object.

In fact, can I use stream to achieve group by in single operation? Meaning I can looping thru Trade object and directly populate aggregated result into AggregatedTrade object in 1 shot.


Let me know, Thanks.

Foritude
8 years ago