简体   繁体   English

仅当列表的第一个元素相等时,Stream才返回对象

[英]Stream returns object only if first element of list is equal

I got simple data structure 我有简单的数据结构

private InMemoryDataBase() {
    Account account1 = new Account("222", "222");
    account1.getPlayers().add(new Player("john"));
    account1.getPlayers().add(new Player("rachel"));
    Account account2 = new Account("111", "111");
    account2.getPlayers().add(new Player("max"));
    accounts.add(account1);
    accounts.add(account2);
}

public class Account {
    private String accountNumber;
    private String password;
    private List<Player> players;

    public Account(String accountNumber, String password){
        this.accountNumber = accountNumber;
        this.password = password;
        players = new ArrayList<>();
    }
}

And method that finds Account if exists 找到Account方法是否存在

 private Optional<Account> findAccount() {
        return dataBase.getAccounts()
                .stream().findAny()
                .filter(a -> a.getAccountNumber()
                        .equals(inputData.getAccountNumber()));
    }

and then simple logic involved with this 然后是简单的逻辑

public void process() {
        Optional<Account> account = findAccount();
        if(account.isPresent()) {
           if(validatePassword(account.get().getPassword())) {               
               outputData = new PlayersList(ConnectionInfo.Source.SERVER, inputData.getSourceID(), players);
           } else {
               outputData = new Message(ConnectionInfo.Source.SERVER, inputData.getSourceID(), "Incorrect password");
           }
        } else {
            outputData = new Message(ConnectionInfo.Source.SERVER, inputData.getSourceID(), "Account not found");
        }
    }

Long story short, that's part of Client-Server communication, inputData is data sent from Client application. 简而言之,这是Client-Server通信的一部分, inputData是从客户端应用程序发送的数据。 And it seems to work, but only if I enter accountNumber equal to "222" (first one in InMemoryDataBase.accounts . 它似乎工作,但只有当我输入accountNumber等于“222”( InMemoryDataBase.accounts第一个)。

I'v did some testing, I can switch those values in my data base, I'll get positive result only if I request the first one in the list. 我做了一些测试,我可以在我的数据库中切换这些值,只有当我请求列表中的第一个时,我才会得到肯定的结果。 The other one ends up with message "Account not found". 另一个最终会收到“未找到帐户”的消息。

It seems like you should instead be doing 看起来你应该这样做

.filter(a -> a.getAccountNumber().equals(inputData.getAccountNumber()))
.findAny();

What you are currently ending up using is Optional.filter which filters in the only Optional you've got using findAny() , based on the predicate if the value exists. 什么您目前结束了使用是Optional.filter它过滤的唯一Optional您使用得findAny()如果该值存在,基于谓词。

Instead of which you should make use of Stream.filter which filters in the stream of content and then you can use findAny() over it to get the Optional value. 而不是你应该使用在内容流中过滤的Stream.filter ,然后你可以使用findAny()来获取Optional值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Stream Lambda 中的 min() 和 max() 方法带有比较器,仅返回第一个和最后一个元素不返回最小/最大元素 - Stream min() and max() method in Lambda Expression with Comparator , returns first and last element only does not return min/max element 使用 Stream 将列表中的元素移动到第一个 position - Move an element in the list to the first position using Stream Java Stream:如何仅更改 stream 的第一个元素? - Java Stream: How to change only first element of stream? Spring Integration 2-拆分器仅返回第一个元素 - Spring Integration 2 - Splitter only returns the first element 仅使用一个元素在列表上调用stream()。reduce() - Calling stream().reduce() on a list with only one element 为什么Stream.iterate返回null作为第一个元素 - Why Stream.iterate returns null as the first element JPA 只返回 @ManyToOne 对象的第一个实例 - JPA only returns first instance of @ManyToOne object 按第一个元素对对象列表进行排序,但如果相等则按第二个元素排序 - Sorting a List of objects by the First element but if Equal sort by the second 有没有办法只使用迭代器删除列表中的第一个元素? - Is there a way to only remove the first element in a List with Iterator? 休眠仅保存列表中的第一个对象 - hibernate only saves the first object from the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM