- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,对这个抽象的标题感到抱歉。这个想法通过一个例子更简单。假设我在列表 L 中有一些值。我想为服务构建参数请求,然后调用该服务并收集所有响应。
目前,我正在使用这种代码结构:
private final List<String> bodies = ImmutableList.of(
"1", "2", "3", "4"
);
@Test
public void BasicRequestResponseStreamToList() {
final List<Request> requests = bodies.stream()
.map(Request::new)
.collect(Collectors.toList());
final List<Response> responses = requests.stream()
.map(service::send)
.collect(Collectors.toList());
commonAssertions(requests, responses);
}
但是,考虑到必须在发送第一个请求之前构建最后一个请求,我发现两个流的需求效率不高。我想做类似的事情:
@Test
public void StatefulMapperStreamRequestResponseToList() {
final List<Request> requests = new ArrayList<>();
final List<Response> responses = bodies.stream()
.map(Request::new)
.map(x -> {
requests.add(x);
return service.send(x);
})
.collect(Collectors.toList());
commonAssertions(requests, responses);
}
但是,我对使用这样的“Hack”来映射语义感到内疚。然而,这是我发现用延迟加载构建 2 个相关列表的唯一方法。我对第一个解决方案不感兴趣,因为它必须在发送请求之前等待构建所有请求。我很想在 EIP 中实现类似窃听的功能。 http://camel.apache.org/wire-tap.html
我很乐意让您想到一种比修改 map 方法的语义更优雅的方式来实现这一点。
如果有帮助,您可以在这里找到源代码:http://tinyurl.com/hojkdzu
最佳答案
使用 .peek()
虽然需要对代码进行较少的更改,但实际上是一种非常肮脏的解决方案。您需要它,因为您的原始代码存在设计缺陷。你有“并行数据结构”(可能这个术语不太好):requests
列表中的第一个元素对应于 responses
列表中的第一个元素,等等在。当您遇到这种情况时,请考虑创建一个新的 PoJo 类。像这样:
public class RequestAndResponse { // You may think up a better name
public final Request req; // use getters if you don't like public final fields
public final Response resp;
public RequestAndResponse(Request req, Response resp) {
this.req = req;
this.resp = resp;
}
}
现在您的问题神奇地消失了。你可以这样写:
List<RequestAndResponse> reqresp = bodies.stream()
.map(Request::new)
.map(req -> new RequestAndResponse(req, service.send(req)))
.collect(Collectors.toList());
commonAssertions(reqresp);
之后您将需要更改 commonAssertions
方法,但我很确定它会变得更简单。此外,您可能会发现代码中的某些方法同时使用请求和响应,因此将它们作为 RequestAndResponse
类中的方法是很自然的。
关于Java 8 : Stream : Consuming steps to save Intermediate States of streamed elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950462/
在我用 OpenCascade 生成的 STEP 文件中,我可以看到: #58 = VERTEX_POINT('',#59); #59 = CARTESIAN_POINT('',(-0.5,-0.5,
这个问题在这里已经有了答案: How to use Firebug's debugger functions? (1 个回答) 关闭 8 年前。 我是 FireBug 调试器的新手,谁能告诉我什么是
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我想知道 Eclipse IDE 中是否存在任何功能,可以在执行 selenium 脚本 时“从步骤进行调试” “单步执行”、“单步执行”。 “QTP” 和大多数专有功能自动化工具中都提供此功能。我假
我正在寻找 .stp/.step 文件的某种规范、文档、解释等。 更多的是关于每行包含哪些信息而不是一般信息。 我似乎无法自己弄清楚每个值的含义。 有谁知道一些关于 STEP 文件的好读物? 我已经搜
我写了下面的代码: Mockup TOPdesk
我正在尝试研究 java String 类中的方法如何工作,因此我创建了一些调用该 String 类方法的自定义代码。 如您所见,我在自己的代码中设置了一个断点,并且在 java String 类源代
我有某些功能,需要我进行一个 api 调用来检索第二个 api 调用中使用的某个 id,逻辑上它看起来像这样: componentWillMount () { this.props.functio
在本地使用 AppEngine(即使用 dev_appserver.py)时,是否可以进行逐步调试?使用 logging.info() 或类似的函数来显示代码中所有变量的值并确定错误的位置是一种过时的
我正在使用 JQuery Steps,它非常好。但事实证明有些事情很棘手。 假设我在第 2 步。根据我在表单中选择的内容,我可能想在单击“下一步”时直接跳到第 4 步,并且进一步禁用第 3 步 anc
我已经包含了 jquery-steps 插件。 如何更改按钮文本? 现在它说“完成”我想把它改成“去” 谢谢 最佳答案 查看以下内容 link .您可以在初始化时更改所有标签。 var setting
在(现已弃用的) Angular 场景测试运行器中,有一个选项可以创建一个 runner.html 页面,该页面将在 iFrame 中运行测试,同时在主页中逐步报告进度。 有没有办法获得类似的 Pro
我有使用标签 的 primefaces 步骤像下面这样: 结果是这样的: 我可以点击步骤 1,但不能点击步骤 3 和 4。如何为所有步骤启用点击? 最佳答案 哇,
给定一个使用分区的 Spring Batch 作业,是否可以有多个分区步骤? 例如: 在上面的示例中,是否可以将另一个分区
假设您要从数据库中获取一条记录,该记录返回大量数据并且需要多次连接。 所以我的问题是,使用单个查询来检查数据是否存在并在存在时获取结果是否更好?或者做一个更简单的查询,检查数据是否存在然后id记录是否
我如何创建一个具有整数输入类型而不是字符串的任务,例如我想启动一个状态机: "SomeStage": { "Comment": "Getting busy
我还没有看到有人问过这个问题——尽管我确实阅读了大约 100 个关于类似主题的 jQuery 步骤——似乎没有一个能解决我的问题。 我正在使用 jQuery-steps 并希望在第一步完成后添加一个“
我想知道是否可以在 Laravel 5.3 中逐个迁移。 我无法使 --step 选项正常工作,文档仅提及此选项与 rollback 或 refresh 一起使用命令。 // this works p
我已按照 AWS 文档中的步骤在本地设置和运行 AWS Step Functions:https://docs.aws.amazon.com/step-functions/latest/dg/sfn-
我已按照 AWS 文档中的步骤在本地设置和运行 AWS Step Functions:https://docs.aws.amazon.com/step-functions/latest/dg/sfn-
我是一名优秀的程序员,十分优秀!