0
Class Order{
List<Transaction> transactions
}

Class Transaction{
public String state;
public Long id;
public Long dateInMillis;
}

I'm looking for an efficient way to do the following

I have a list of orders and each order can have multiple transactions each.Transaction can be in two states i.e. ACTIVE, COMPLETED (and a bunch of states in between we don't care about). For each order, we create a transaction(ACTIVE) when we start the process and create another transaction(COMPLETED) when we end it. I want to generate list of transactions with their requestDate & completionDate. For a transaction in ACTIVE status, requestDate and Completion date is the transaction date itself. For a transaction in COMPLETED state, requestDate is the date of corresponding ACTIVE transaction and completion date is the date of current transaction i.e. COMPLETED state one.

For e.g. two transactions t1 & t2 where t1 has state ACTIVE and t2 has state COMPLETED.

    orders.stream()
    .forEach(order -> {
        order.getTransactions().stream().forEach(transaction -> {
        });

        ResultObject obj = new ResultObject();
        obj.setTransactionId(transaction.getId());

        if(transaction.getState()=="ACTIVE"){
            obj.setRequestDateInMillis(transaction.getDateInMillis);
            obj.setCompletionDateInMillis(transaction.getDateInMillis());
        }else if(transaction.getState()=="COMPLETED"){
            obj.setCompletionDateInMillis(transaction.getDateInMillis());

            TODO: find transaction for this order which is in ACTIVE state and set that time as RequestDate for current transaction
        }
    });             
Nick01
  • 349
  • 2
  • 8
  • 22
  • Don't use a stream for the `TODO`. Create a usefully searchable structure from `orders` and reference it. – Bob Dalgleish Jul 27 '17 at 20:45
  • 4
    Not really about your question but regarding `if(transaction.getState()=="ACTIVE")` you should take a look at [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo Jul 27 '17 at 20:51
  • @Pshemo sure I know. Just did a quick write up for posting here. – Nick01 Jul 27 '17 at 21:14
  • @BobDalgleish can you give an example? – Nick01 Jul 27 '17 at 21:15
  • 2
    @Nick01 You shouldn't use incorrect code for a quick write up. It distracts attention from the actual question. – shmosel Jul 28 '17 at 00:12
  • This is an `X\Y Question` –  Jul 28 '17 at 00:13
  • 1
    There is no problem iterating the transaction list multiple times at all. But how do you identify a “corresponding … transaction”? Or are there only two transactions per order? Then, the whole iteration logic makes no sense, as one or two objects can be processed much simpler. But what are you gonna do with the `ResultObject`? `forEach` does not return a value… – Holger Jul 28 '17 at 11:39

1 Answers1

1
    Transaction active, completed;
    for(Order o : listOfOrders){
        active = null, completed = null;
        for (Transaction t : o.transactions){
            if (t.state.equals("Active"))
                active = t;
            else if (t.state.equals("Completed"))
                completed = t;
            //break?
        }
        if (completed != null){

        }
        else if (active != null){

        }
        else{
            //Error
        }
    }

I think before using streams make the logic work with standard loops. After that if the list of orders is large enough (10K+) then streams may become handy

bichito
  • 1,406
  • 2
  • 19
  • 23