将Java Stream过滤为1并且只有1个元素 [英] Filter Java Stream to 1 and only 1 element

查看:4054
本文介绍了将Java Stream过滤为1并且只有1个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java 8 以查找 LinkedList 中的元素。不过,我想保证过滤条件只有一个匹配。

I am trying to use Java 8 Streams to find elements in a LinkedList. I want to guarantee, however, that there is one and only one match to the filter criteria.

请输入以下代码:

public static void main(String[] args) {

    LinkedList<User> users = new LinkedList<>();
    users.add(new User(1, "User1"));
    users.add(new User(2, "User2"));
    users.add(new User(3, "User3"));

    User match = users.stream().filter((user) -> user.getId() == 1).findAny().get();
    System.out.println(match.toString());
}







static class User {

    @Override
    public String toString() {
        return id + " - " + username;
    }

    int id;
    String username;

    public User() {
    }

    public User(int id, String username) {
        this.id = id;
        this.username = username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public int getId() {
        return id;
    }
}

此代码找到用户根据他们的ID。但无法保证有多少用户匹配过滤器。

This code finds a User based on their ID. But there are no guarantees how many Users matched the filter.

将过滤行更改为:

User match = users.stream().filter((user) -> user.getId() < 0).findAny().get();

将抛出 NoSuchElementException (好!)

如果有多个匹配,我希望它抛出一个错误。有办法吗?

I would like it to throw an error if there are multiple matches, though. Is there a way to do this?

推荐答案

创建自定义 收藏家



Create a custom Collector

public static <T> Collector<T, ?, T> toSingleton() {
    return Collectors.collectingAndThen(
            Collectors.toList(),
            list -> {
                if (list.size() != 1) {
                    throw new IllegalStateException();
                }
                return list.get(0);
            }
    );
}

我们使用 Collectors.collectingAndThen 通过

We use Collectors.collectingAndThen to construct our desired Collector by

    $ b构建我们想要的收藏家 $ b
  1. 使用 Collectors.toList()收集器在列表中收集对象。

  2. 在结尾处应用额外的修整器,返回单个元素 - 或抛出 IllegalStateException 如果 list.size!= 1

  1. Collecting our objects in a List with the Collectors.toList() collector.
  2. Applying an extra finisher at the end, that returns the single element — or throws an IllegalStateException if list.size != 1.

用作:

User resultUser = users.stream()
        .filter(user -> user.getId() > 0)
        .collect(toSingleton());

然后你可以自定义这个收藏家如你所愿,例如在构造函数中将异常作为参数,调整它以允许两个值,等等。

You can then customize this Collector as much as you want, for example give the exception as argument in the constructor, tweak it to allow two values, and more.

您可以使用涉及 peek() AtomicInteger的'变通办法',但实际上你不应该使用它。

You can use a 'workaround' that involves peek() and an AtomicInteger, but really you shouldn't be using that.

你可以做什么istead只是在 List <中收集它/ code>,如下所示:

What you could do istead is just collecting it in a List, like this:

LinkedList<User> users = new LinkedList<>();
users.add(new User(1, "User1"));
users.add(new User(2, "User2"));
users.add(new User(3, "User3"));
List<User> resultUserList = users.stream()
        .filter(user -> user.getId() == 1)
        .collect(Collectors.toList());
if (resultUserList.size() != 1) {
    throw new IllegalStateException();
}
User resultUser = resultUserList.get(0);

这篇关于将Java Stream过滤为1并且只有1个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆