lambda -- Filter Java Stream to 1 and only 1 element


up vote10down votefavorite

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

Take this code:

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

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

Changing the filter line to:

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

Will throw a NoSuchElementException (good!)

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

java lambda stream java-8


share|improve this question

edited Mar 27 at 17:40

asked Mar 27 at 17:25

ryvantage

2,58921335

   

count() is a terminal operation so you can‘t do that. The stream can‘t be used after. – ZouZou Mar 27 at 17:42 
   

Ok, thanks @ZouZou. I wasn‘t entirely certain what that method did. Why is there noStream::size ? –  ryvantage Mar 27 at 17:44
2  

@ryvantage Because a stream can only be used once: calculating its size means "iterating" over it and after that you can‘t use the stream any longer. –  assylias Mar 27 at 17:45
   

Wow. That one comment helped me understand Streams so much more than I did before... –  ryvantage Mar 27 at 17:50

add a comment

6 Answers

activeoldestvotes


up vote13down voteaccepted

Technically there‘s an ugly ‘workaround‘ that involves peek() and an AtomicInteger, but really you shouldn‘t be using that.

What I do in these cases 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);

I am not aware of a way to do this in the API, meanwhile I will work on another example involving a custom element.

Update, You should create your own Collector for this:

public static <T> Collector<T, List<T>, T> singletonCollector() {
    return Collector.of(
            ArrayList::new,
            List::add,
            (left, right) -> { left.addAll(right); return left; },
            list -> {
                if (list.size() != 1) {
                    throw new IllegalStateException();
                }
                return list.get(0);
            }
    );
}

What it does is:

  • It mimicks the Collectors.toList() collector.
  • It applies an extra finisher at the end, that throws an exception, or if no exception, returns the first element of the list.

Used as:

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

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

New update, I revised my old answer once more for singletonCollector(), it can actually be obtained like this:

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


share|improve this answer

edited Mar 27 at 20:20

answered Mar 27 at 17:33

skiwi

9,37332767

   

@ryvantage I updated my answer with how I would do it with writing the own custom Collector, which I believe is the correct method. –  skiwi Mar 27 at 18:03
   

what do you think of my reduction? –  assylias Mar 27 at 18:32
1  

@assylias I commented on your answer, I think this one i smore concise and more straight forward though and less error prone. –  skiwi Mar 27 at 18:38
   

The difference main between the two collectors is that the first will stop as soon as it finds a second matching item whereas the second will iterate through the whole list. – assylias Mar 27 at 22:57

add a comment


up vote9down vote

Wow, such complexity! :-) The other answers that involve writing a custom Collector are probably more efficient (such as Louis Wasserman‘s, +1), but if you want brevity, I‘d suggest the following:

List<User> result = users.stream()
    .filter(user -> user.getId() == 1)
    .limit(2)
    .collect(Collectors.toList());

Then verify the size of the result list.


share|improve this answer

answered Mar 28 at 15:39

Stuart Marks

12.5k11850

   

What‘s the point of limit(2) in this solution? What difference would it make whether the resulting list was 2 or 100? If it‘s greater than 1. –  ryvantage Mar 28 at 18:31
2  

It stops immediately if it finds a second match. This is what all the fancy collectors do, just using more code. :-) –  Stuart Marks Mar 29 at 3:24 

add a comment


up vote6down vote

You could roll your own Collector for this:

<E> Collector<E, ?, Optional<E>> getOnly() {
  return Collector.of(
    AtomicReference::new,
    (ref, e) -> {
      if (!ref.compareAndSet(null, e)) {
         throw new IllegalArgumentException("Multiple values");
      }
    },
    (ref1, ref2) -> {
      if (ref1.get() == null) {
        return ref2;
      } else if (ref2.get() != null) {
        throw new IllegalArgumentException("Multiple values");
      } else {
        return ref1;
      }
    },
    ref -> Optional.ofNullable(ref.get()),
    Collector.Characteristics.UNORDERED);
}

...or using your own Holder type instead of AtomicReference. You can reuse that Collector as much as you like.


share|improve this answer

answered Mar 27 at 17:51

Louis Wasserman

71k894155

   

@skiwi‘s singletonCollector was smaller and easier to follow than this, that‘s why I gave him the check. But good to see consensus in the answer: a custom Collector was the way to go. –  ryvantage Mar 27 at 20:37
   

Fair enough. I was primarily aiming for speed, not conciseness. –  Louis WassermanMar 27 at 20:40
   

Yeah? Why is yours faster? –  ryvantage Mar 27 at 20:45
   

Mostly because allocating an all-up List is more expensive than a single mutable reference. –  Louis Wasserman Mar 27 at 20:52 
   

I was unable to get yours to compile –  ryvantage Mar 28 at 18:28

add a comment


up vote5down vote

The "escape hatch" operation that lets you do weird things that are not otherwise supported by streams is to ask for an Iterator:

Iterator<T> it = users.stream().filter((user) -> user.getId() < 0).iterator();
if (!it.hasNext())
    throw new NoSuchElementException();
else {
    result = it.next();
    if (it.hasNext())
        throw new TooManyElementsException();
}

Guava has a convenience method to take an Iterator and get the only element, throwing if there are zero or multiple elements, which could replace the bottom n-1 lines here.


share|improve this answer

edited Oct 3 at 17:32

David Conrad

3,8651220

answered May 24 at 19:26

Brian Goetz

8,86341733

 
add a comment

up vote3down vote

The exception is thrown by Optional#get, but if you have more than one element that won‘t help. You could collect the users in a collection that only accepts one item, for example:

User match = users.stream().filter((user) -> user.getId() > 1)
                  .collect(toCollection(() -> new ArrayBlockingQueue<User>(1)))
                  .poll();

which throws a java.lang.IllegalStateException: Queue full, but that feels too hacky.

Or you could use a reduction combined with an optional:

User match = Optional.ofNullable(users.stream().filter((user) -> user.getId() > 1)
                .reduce(null, (u, v) -> {
                    if (u != null && v != null)
                        throw new IllegalStateException("More than one ID found");
                    else return u == null ? v : u;
                })).get();

The reduction essentially returns:

  • null if no user is found
  • the user if only one is found
  • throws an exception if more than one is found

The result is then wrapped in an optional.

But the simplest solution would probably be to just collect to a collection, check that its size is 1 and get the only element.


share|improve this answer

edited Mar 27 at 18:31

answered Mar 27 at 17:37

assylias

110k10153289

   

I would add an identity element (null) to prevent using get(). Sadly your reduce is not working as you think it does, consider a Stream that has null elements in it, maybe you think that you covered it, but I can be [User#1, null, User#2, null, User#3], now it will not throw an exception I think, unless I‘m mistaken here. –  skiwiMar 27 at 18:36
   

@Skiwi if there are null elements the filter will throw a NPE first. –  assylias Mar 27 at 18:37

add a comment


up vote0down vote

Have you tried this

    long c = users.stream().filter((user) -> user.getId() == 1).count();
    if(c>1){
        throw new IllegalStateException();
    }
long count()
Returns the count of elements in this stream. This is a special case of a reduction and is equivalent to:

     return mapToLong(e -> 1L).sum();

This is a terminal operation.


share|improve this answer

answered Mar 28 at 7:03

pardeep131085

826134

   

It was said that count() is not good to use because it is a terminal operation. – ryvantage Mar 28 at 18:29

add a comment

时间: 2024-08-24 15:25:53

lambda -- Filter Java Stream to 1 and only 1 element的相关文章

Java之Lambda表达式和Stream类简单例子

开心一笑 提出问题 java的lambda表达式和Stream如何使用??? 解决问题 Lambda表达式的语法 基本语法: (parameters) -> expression 或 (parameters) ->{ statements; } 看例子学习吧! 例一:定义一个AyPerson类,为之后的测试做准备. package com.evada.de; import java.util.Arrays; import java.util.List; class AyPerson{ priv

Java Stream 使用详解

Stream是 Java 8新增加的类,用来补充集合类. Stream代表数据流,流中的数据元素的数量可能是有限的,也可能是无限的. Stream和其它集合类的区别在于:其它集合类主要关注与有限数量的数据的访问和有效管理(增删改),而Stream并没有提供访问和管理元素的方式,而是通过声明数据源的方式,利用可计算的操作在数据源上执行,当然BaseStream.iterator()和BaseStream.spliterator()操作提供了遍历元素的方法. Java Stream提供了提供了串行和

Java Stream &amp; Method Reference

目录 Java Stream & Method Reference 1. Stream流 1.1 概述 1.2 流式思想的概述 1.3 获取流 1.4 常用方法 1.5 练习:集合元素处理(传统方式) 1.6 练习:集合元素处理(Stream流方式) 2. 方法引用 2.1 基本介绍 2.2 通过对象名引用[成员方法] 2.3 通过类名称引用[静态方法] 2.4 通过super引用父类的普通成员方法 2.5 通过this引用本类的普通成员方法 2.6 类的构造器(构造方法)引用 2.7 数组的构

Java Stream函数式编程第三篇:管道流结果处理

一.Java Stream管道数据处理操作 在本号之前写过的文章中,曾经给大家介绍过 Java Stream管道流是用于简化集合类元素处理的java API.在使用的过程中分为三个阶段.在开始本文之前,我觉得仍然需要给一些新朋友介绍一下这三个阶段,如图: 第一阶段(图中蓝色):将集合.数组.或行文本文件转换为java Stream管道流 第二阶段(图中虚线部分):管道流式数据处理操作,处理管道中的每一个元素.上一个管道中的输出元素作为下一个管道的输入元素. 第三阶段(图中绿色):管道流结果处理操

Java Stream流式思想

说到Stream便容易想到I/O Stream,而实际上,谁规定“流”就一定是“IO流”呢?在Java 8中,得益于Lambda所带来的函数式编程,引入了一个全新的Stream概念,用于解决已有集合类库既有的弊端. 引言 传统集合的多步遍历代码 几乎所有的集合(如 Collection 接口或 Map 接口等)都支持直接或间接的遍历操作.而当我们需要对集合中的元 素进行操作的时候,除了必需的添加.删除.获取外,最典型的就是集合遍历.例如: import java.util.List; impor

一文带你入门Java Stream流,太强了

两个星期以前,就有读者强烈要求我写一篇 Java Stream 流的文章,我说市面上不是已经有很多了吗,结果你猜他怎么说:“就想看你写的啊!”你看你看,多么苍白的喜欢啊.那就“勉为其难”写一篇吧,嘻嘻. 单从“Stream”这个单词上来看,它似乎和 java.io 包下的 InputStream 和 OutputStream 有些关系.实际上呢,没毛关系.Java 8 新增的 Stream 是为了解放程序员操作集合(Collection)时的生产力,之所以能解放,很大一部分原因可以归功于同时出现

java stream 原理

java stream 原理 需求 从"Apple" "Bug" "ABC" "Dog"中选出以A开头的名字,然后从中选出最长的一个,并输出其长度 1. 最直白的实现 缺点 迭代次数过多 频繁产生中间结果,性能无法接受 2. 平常写法 int longest = 0; for(String str : strings){ if(str.startsWith("A")){// 1. filter(), 保留以

java stream的常用例子

一.标题 java stream的常用例子 二.描述 stream在当前互联网技术社区传播的已经很广泛了,且有阿里11.11用stream很好的完成数据处理案例,为此迎着互联网技术风口细细地学习一下stream,说不定能让代码飘起来 三.常用的例子 以下内容均使用该代码作为前提: 1 public class AppTest { 2 3 public static void main(String[] args){ 4 List<StreamDemo> list = new ArrayList

python——lambda,filter,map,reduce

lambda函数 使用方法:lambda [arg1[,arg2,arg3,...,argn]] : expression 如: add = lambda(x,y:x+y) add(1,2) 结果为1+2=3 filter函数 filter(bool_func,seq) 此函数的功能相当于过滤器,通过返回值为bool的函数bool_func来迭代遍历seq中的每个元素: 结果返回一个seq中使bool_func返回值为true的元素的序列. filter(lambda x : x%2 == 0,