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.

ETO
  • 6,970
  • 1
  • 20
  • 37
Payal
  • 133
  • 7
  • 3
    Sort by version before `findFirst()` – Rob Audenaerde Jun 16 '20 at 12:11
  • Does this answer your question? [How to use a Java8 lambda to sort a stream in reverse order?](https://stackoverflow.com/questions/28607191/how-to-use-a-java8-lambda-to-sort-a-stream-in-reverse-order) – Rob Audenaerde Jun 16 '20 at 12:14
  • No., actually for sorting by version I need version field which is not present in HrcRestatementKey (given by user). – Payal Jun 16 '20 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? – Abra Jun 16 '20 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? – ETO Jun 16 '20 at 14:41

3 Answers3

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);
Hadi J
  • 16,989
  • 4
  • 36
  • 62
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 +
                    '}';
        }
    }
ameet chaubal
  • 1,440
  • 16
  • 37
  • Do not rely on `(o1, o2) -> o2.version - o1.version` to `compare`! See [HadiJ's answer](https://stackoverflow.com/a/62408592/1746118) for a better and correct offering. – Naman Jun 16 '20 at 12:47
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);
ETO
  • 6,970
  • 1
  • 20
  • 37