简体   繁体   English

Java8 Stream上的Call Filter方法

[英]Call Filter method on Java8 Stream

Is it possible to update Stream data and return it directly? 是否可以更新Stream数据并直接将其返回? I would like to update stream data on request params send by the consumer, kindly see the code below. 我想根据消费者发送的请求参数更新流数据,请参见下面的代码。

Supplier<Stream<User>> userStream = users::stream;

userStream.get().filter(user -> user.getRole().getType().getName().equalsIgnoreCase(value));
userStream.get().filter(user -> user.getDownloadCertificate() == check );

You can't update Stream because it is not a data structure, but a sequence of elements supporting sequential and parallel aggregate operations (source: https://docs.oracle.com/javase/8/docs/api/?java/util/stream/Stream.html ). 您不能更新Stream,因为它不是数据结构,而是支持顺序和并行聚合操作的一系列元素 (来源: https//docs.oracle.com/javase/8/docs/api/?java / util /stream/Stream.html )。

Java 8 documentation explains the main difference between Streams and Collections: Java 8文档解释了Streams和Collections之间的主要区别:

Collections and streams , while bearing some superficial similarities, have different goals. 集合虽然具有一些表面上的相似性,但具有不同的目标。 Collections are primarily concerned with the efficient management of, and access to, their elements. 馆藏主要涉及对其元素的有效管理和访问。 By contrast, streams do not provide a means to directly access or manipulate their elements, and are instead concerned with declaratively describing their source and the computational operations which will be performed in aggregate on that source. 相比之下,流不提供直接访问或操纵其元素的方法,而与声明性地描述其源以及将在该源上聚合执行的计算操作有关。 However, if the provided stream operations do not offer the desired functionality, the BaseStream.iterator() and BaseStream.spliterator() operations can be used to perform a controlled traversal. 但是,如果提供的流操作未提供所需的功能,则可以使用BaseStream.iterator()BaseStream.spliterator()操作执行受控的遍历。

A stream pipeline, like the "widgets" example above, can be viewed as a query on the stream source. 像上面的“窗口小部件”示例一样,流管道也可以视为对流源的查询。 Unless the source was explicitly designed for concurrent modification (such as a ConcurrentHashMap), unpredictable or erroneous behavior may result from modifying the stream source while it is being queried. 除非明确为并行修改而设计了源(例如ConcurrentHashMap),否则在查询流源时修改流源可能会导致不可预测或错误的行为。

Source: https://docs.oracle.com/javase/8/docs/api/?java/util/stream/Stream.html 来源: https : //docs.oracle.com/javase/8/docs/api/? java/ util/ stream/ Stream.html

The desired usage of a Stream in your case may look like this: 在您的情况下,Stream的期望用法可能如下所示:

List<User> users = getUsers(); // let's assume this method returns a list of users
String name = "some name";
boolean check = true;

//Now let's create a new list of filtered users
List<User> filteredUsers = users.stream()
        .filter(user -> user.getRole().getType().getName().equalsIgnoreCase(name))
        .filter(user -> user.getDownloadCertificate() == check)
        .collect(Collectors.toList());

Keep in mind that initial list of users was not modified. 请记住,初始用户列表未修改。 Stream iterated once when the terminal operation ( .collect ) was called. 调用终端操作( .collect )时,流将迭代一次。

A Stream will iterate over a data source to perform the operation it represents, but never modify the source. Stream将在数据源上进行迭代以执行其表示的操作 ,但永远不要修改该源。

If you want to modify the source collection in-place, just do it directly without any stream: 如果要就地修改源集合,则无需任何流即可直接进行:

users.removeIf(user -> !user.getRole().getType().getName().equalsIgnoreCase(value));
users.removeIf(user -> user.getDownloadCertificate() != check );

Of course, this requires that users is a mutable collection. 当然,这要求users是可变的集合。 If that's not the case, you have to create a new collection anyway and in that case, you may use the Stream API to create a new collection, as shown in this answer . 如果不是这种情况,则无论如何都必须创建一个新集合,在这种情况下,您可以使用Stream API创建一个新集合, 如此答案所示。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM