0

I Have Code like below

List<Transaction> ts = transaksiRepo.findAll();
 for (Transaction data5 : ts) {
        if (data5.getStatus().equals("NOT PAID OFF")) {
            log.info("Testing");
        }
 }

I want to get previous data index to condition if else on code in array list

Like this

List<Transaction> ts = transaksiRepo.findAll();
 for (Transaction data5 : ts) {
        if (data5.getStatus().equals("NOT PAID OFF") && previous index.getStatus.equals("NOT PAID OFF")) {
            log.info("Testing getStatementDate");
        }
 }

How do I resolve this?

0

2 Answers 2

3

You can use simple for loop and achieve this easily.

I have started index starting from 1 as we will be comparing previous index i.e. 0th element.

Modify the logic as per your needs.

List<Transaction> ts = transaksiRepo.findAll();
for (int i=1;i<ts.size();i++) {
    if (ts.get(i).getStatus().equals("NOT PAID OFF") && ts.get(i-1).getStatus.equals("NOT PAID OFF")) {
        log.info("Testing getStatementDate");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend that you use ShubhWIP's answer, but if you absolutely prefer the enhanced for loop, you can still use it:

List<Transaction> ts = transaksiRepo.findAll();
Transaction prevTs = null;
for (Transaction data5 : ts) {
    if (data5.getStatus().equals("NOT PAID OFF") && prevTs != null && prevTs.getStatus.equals("NOT PAID OFF")) {
        log.info("Testing getStatementDate");
    }
    prevTs = ts;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.