有没有一种方法可以使用java流将两个String相互比较?

Hub*_*ubi 2 java list java-8 java-stream

此方法比较两个字符串。其中之一来自物体。如果字符串匹配,则ID由对象返回。


    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;
    }


Run Code Online (Sandbox Code Playgroud)

有什么办法可以解决流?我的方法,但我无能为力了。

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

Run Code Online (Sandbox Code Playgroud)

你有解决的办法吗?我还想知道两个变体中的哪个变体对于大量数据更有效。

And*_*lko 5

更换

...
...
...
Run Code Online (Sandbox Code Playgroud)

.map(MandantKagAccountEntity::getMandantKagId)
.findFirst()
.orElse(null);
Run Code Online (Sandbox Code Playgroud)