Is there a way to compare two Strings with each other using java streams?

Hubi :

This method compares two strings. One of them comes from an object. If the strings match, the id is returned by the object.


    private static Long getDefaultKag(Long mandandId) {
        List<MandantKagAccountEntity> mandantKagAccountEntities = new MandantKagAccountManager().findAllKags(mandandId);
        for (MandantKagAccountEntity mandantKagAccountEntity : mandantKagAccountEntities) {
            if (mandantKagAccountEntity.getKagText().equals("Default_kag")) {
                return mandantKagAccountEntity.getMandantKagId();
            }
        }
        return null;
    }


Is there any way to solve this with streams? My approach, but I can't get any further.

    private static long getDefaultKag(Long mandandId) {
        return new MandantKagAccountManager().findAllKags(mandandId).stream()
                .filter(m -> m.getKagText().equals("Default_Kag"))
                ...
                ...
                ...
    }

Do you have any idea how to solve this? I would also like to know which of the two variants is more efficient for large amounts of data.

Andrew Tobilko :

Replace

...
...
...

with

.map(MandantKagAccountEntity::getMandantKagId)
.findFirst()
.orElse(null);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=142284&siteId=1