链接消费者Java 8 [英] Chaining Consumers Java 8

查看:76
本文介绍了链接消费者Java 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我遇到以下问题.可以说,我们有对象"Account".该对象Account是不可变的,因此加班我们对它执行操作,实际上是将其转换为另一种状态.例如,帐户可以成为ClosedAccount或NewAccount等.现在,在某些情况下,我的转换是反射性的,帐户"已转换为帐户",但有些很小的更改,例如"modifiedDate".因为我的帐户本质上是一成不变的,所以即使反射操作仍在创建具有更新值的新帐户.如果我的对象不是一成不变的,我会使用类似的东西:

Hello I am having the following problem. Lets say that We have object "Account" This object Account is immutable, so overtime we execute an action upon it we are actually transforming it to another state. For example Account can become ClosedAccount or NewAccount and so on. Now in some occasions my transitions are reflective Account is transformed to Account but something very small is changed like for example "modifiedDate". Because my Account is its essence immutable even the reflective operations are still creating new Accounts with updated values. If my object is not immutable I would have used something like :

List<String> accountsIds;
accountIds.parrallelStream.map(t->NewAccount)
          .map(t->Account)
          .peek(t->changetheDate())
          .map(t->closeAccount())
          .forEach(bla bla bla)

现在的问题是peek()只会更改可变对象的状态.但就我而言,该帐户是不可变的,因此即使我创建了一个新对象,该对象也不会进一步流式传输.

Now the problem is that the peek() will only change the state for mutable object. But in my case the Account is immutable so even if I create a new object this object will not be streamed further.

问题是,是否可以像映射"方法那样将消费者"链接起来,使消费者"的输出成为下一个消费者"的输入.换句话说,我正在寻找对不可变对象有效的窥视"操作.

The question is is it possible to chain "Consumers" in such way that they work like "map" methods the output of the "Consumer" to become the input of the next "Consumer". In other words I am looking for "peek" operation that works on Immutable objects.

我问这个问题的原因是因为我的理解是"map"是一种转换操作,而我想执行更像是Action的操作.仅当我使用不可变对象时,它才用作转换操作,但是如果我使用可变对象,那将只是一个动作.

The reason I am asking this question is because my understanding is that "map" is a transformation operation, while I want to execute something that is more like an Action. It works as a transformation operation only because I am working with immutable objects but if I was using mutable objects it would have been just an action.

推荐答案

您可能正在尝试解决一个不存在的问题.我认为,无论更改有多小,用map()对对象进行任何更改都是完全正常的.

You are probably trying to solve a problem that does not exist. I think it is perfectly normal to do any change to an object with map() no matter how small the change is.

我的理解是,像map()peek()这样的操作背后没有语义上的含义,您可以在适合的任何任务中使用它们中的任何一个.

My understanding is that there is no semantic meaning behind operations like map() or peek(), and you can use either of them for any task where it suits.

peek()通常用于调试:它允许您记录流中的每个项目,而无需使用终端操作并销毁流. map()用于需要处理对象并将其向前传递的情况.

peek() is often used for debugging: it allows you to log every item in the stream without using a terminal operation and destroying the stream. map() is used when you need to do something with objects and pass them forward.

也请考虑一下:Consumer接口中的方法有一个输入参数,并且不返回任何值.如果您的输入参数是不可变的,那么就不可能从此方法返回任何值而不会产生任何肮脏的花招.

Also, think about it: the method in the Consumer interface has one input parameter and it does not return any value. If your input parameter is immutable, it makes it impossible to return any value from this method without any dirty tricks.

这篇关于链接消费者Java 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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