Skip to content
Advertisement

UNable to implementGroupby tradeDate then max of transactionDate if transaction Date are equal then get the max of version

I have the scenario need your help to solve this issue with java code .

We are getting List Of Trade Object Trade consists of 3 attributes :

S.NO., TradeDate, TransactionDate , Version

 1,   20220102,20220103,1
 2,   20220104,20220104,1
 3,   20220105,20220106,1
 4,   20220105,20220107,1
 5,   20220106,20220108,3
 6,   20220106,20220108,4
 7,   20220106,20220118,5

So what I need to do with the above data is:

  1. if the trade date is unique then get the trade date. Output will be [1,2]
  2. if the trade date is duplicate then take the latest TransactionDate Output will be [4] from 3,4 rows
  3. If the trade date and TransactionDate is duplicate then take the max version Output will be [7] from 5,6,7

How will do solve this scenario ? Kindly help to implement

class Trades implements Comparable{

private Date tradeDate;
private Date tranDate;
private int version;


public Trades(Date tradeDate,Date tranDate,int version){

this.tradeDate=tradeDate;
this.tranDate=tranDate;
this.version=version;

}


public Date getTradeDate(){

return this.tradeDate;
}

public boolean equals(Object obj,Object obj1){


}

public int hasCode(){

return 1;

}

}

List<Trades> trade = new ArrayList(); //Assuming all the data is populated.

trade.stream().collect(Collectors.grouping(Trades::getTradeDate), LinkedHashMap::new, Collector.toList());

Advertisement

Answer

In your question there are 3 fields fields involved so the result is a Map<LocalDate, Map<LocalDate, Trades>>.

It is grouped by TradeDate, TransactionDate and aggregated by Version.

This function gives the desired result:

public static Map<LocalDate, Map<LocalDate, Trades>> select(List<Trades> from) {
    return from.stream()
               .collect(Collectors.groupingBy(Trades::getTradeDate,
                        Collectors.toMap(Trades::getTransactionDate, Function.identity(),
                        BinaryOperator.maxBy(Comparator.comparing(Trades::getVersion)))));
}

A complete test class:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Trades {
    public static void main(String[] args) {
        List<Trades> tradeList = new ArrayList<>();
        tradeList.add(new Trades("20220102", "20220103", 1));
        tradeList.add(new Trades("20220104", "20220104", 1));
        tradeList.add(new Trades("20220105", "20220106", 1));
        tradeList.add(new Trades("20220105", "20220107", 1));
        tradeList.add(new Trades("20220106", "20220108", 3));
        tradeList.add(new Trades("20220106", "20220108", 4));
        tradeList.add(new Trades("20220106", "20220118", 5));

        Map<LocalDate, Map<LocalDate, Trades>> result = select(tradeList);
        result.forEach((tdate, xdates) -> xdates
              .forEach((xdate, trades) -> System.out.println(trades)));
    }

    public static Map<LocalDate, Map<LocalDate, Trades>> select(List<Trades> from) {
        return from.stream()
                   .collect(Collectors.groupingBy(Trades::getTradeDate,
                            Collectors.toMap(Trades::getTransactionDate, Function.identity(),
                            BinaryOperator.maxBy(Comparator.comparing(Trades::getVersion)))));
    }

    private LocalDate tradeDate;
    private LocalDate tranDate;
    private int version;

    public Trades(String tradeDate, String tranDate, int version) {
        this.tradeDate = LocalDate.parse(tradeDate, DateTimeFormatter.BASIC_ISO_DATE);
        this.tranDate = LocalDate.parse(tranDate, DateTimeFormatter.BASIC_ISO_DATE);
        this.version = version;
    }

    public LocalDate getTradeDate() { return tradeDate; }

    public LocalDate getTransactionDate() { return tranDate; }

    public int getVersion() { return version; }

    @Override
    public String toString() { return "[" + tradeDate + ", " + tranDate + ", " + version + "]"; }
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement