3

Let's say I have below records in a table in DB.

Empl table

Here Emplid and date combination is my PK. With every change I maintain version of records. (in last 3 rows there are total 3 version as change in number).

Now I want to fetch record where emplid = 1111 date = 28-02-2019 with latest version (user don't know how many version are there in db).

How can I do it with lambda expression.

HrcKey matchedKey = hrcKeyList.stream()
             .filter(x -> (currEmplid.equals(x.getEmplid()) && 
                     format.format(updateListFromUI.getDate())
                                  .equals(format.format(x.getDate()))))
             .findFirst()
             .orElse(null);

this will give me vesrion 1. but how can I get latest version.

5
  • 3
    Sort by version before findFirst() Commented Jun 16, 2020 at 12:11
  • Does this answer your question? How to use a Java8 lambda to sort a stream in reverse order? Commented Jun 16, 2020 at 12:14
  • No., actually for sorting by version I need version field which is not present in HrcRestatementKey (given by user). Commented Jun 16, 2020 at 12:38
  • 2
    If the source of your data is the database, why don't you utilize SQL and write a query that returns the data you require? SQL is very good at that. Or is this purely an exercise in using Java's stream API? Commented Jun 16, 2020 at 13:30
  • What is the type of your date fields? Why don't you use equals on them instead of comparing the formatted strings? Commented Jun 16, 2020 at 14:41

3 Answers 3

1

You can use max to get latest object by comparing version value.

hrcRestatementKeyList.stream()
            .filter(x -> (currEmplid.equals(x.getEmplid()) &&
                    format.format(updatedHrcDbpRestatement.getExtr_effdt())
                            .equals(format.format(x.getExtr_effdt()))))
            .max(Comparator.comparingInt(HrcRestatementKey::getVersion))
            .orElse(null);
Sign up to request clarification or add additional context in comments.

Comments

0

in short you can do something like this,

employees.stream().
                filter(hrc -> hrc.empid == 11 && hrc.date.equals("28-02-2000")).
                min((o1, o2) -> o2.version - o1.version).orElse(null);

full example,

public static void main(String[] args) {
        RnD rnD = new RnD();
        rnD.test();
    }
    private void test() {
        List<Hrc> employees = new ArrayList<>();
        employees.add(new Hrc(11, "28-02-2000", 1));
        employees.add(new Hrc(11, "28-02-2000", 2));
        employees.add(new Hrc(11, "28-02-2000", 3));
        employees.add(new Hrc(11, "30-02-2000", 1));
        employees.add(new Hrc(22, "11-02-2000", 1));
        Hrc topVersion= employees.stream().
                filter(hrc -> hrc.empid == 11 && hrc.date.equals("28-02-2000")).
                min((o1, o2) -> o2.version - o1.version).orElse(null);
        System.out.println(topVersion);
    }
    private static class Hrc {
        int empid;
        String date;
        int version;

        public Hrc(int empid, String date, int version) {
            this.empid = empid;
            this.date = date;
            this.version = version;
        }

        @Override
        public String toString() {
            return "Hrc{" +
                    "empid=" + empid +
                    ", date='" + date + '\'' +
                    ", version=" + version +
                    '}';
        }
    }

1 Comment

Do not rely on (o1, o2) -> o2.version - o1.version to compare! See HadiJ's answer for a better and correct offering.
0

I'd go with this code snippet:

yourList.stream()
        .filter(x -> currEmplid.equals(x.getEmplid())
        .filter(x -> updatedHrcDbpRestatement.getExtr_effdt()
                                             .equals(x.getExtr_effdt())
        .max(comparingInt(HrcRestatementKey::getVersion))
        .orElse(null);

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.