【发布时间】:2021-03-22 02:33:57
【问题描述】:
我正在尝试弄清楚如何获取匹配值并将其存储在字符串变量中,这就是我所做的:
出于示例目的,我创建了以下内容:
Map<Attachment, String> mapattach = new HashMap<Attachment, String>();
Attachment a1 = new Attachment();
a1.setId("one1");
a1.setName("one");
a1.setUrl("http://1.com");
Attachment a2 = new Attachment();
a2.setId("two2");
a2.setName("two");
a2.setUrl("http://2.com");
Attachment a3 = new Attachment();
a3.setId("three3");
a3.setName("three");
a3.setUrl("http://3.com");
mapattach.put(a1, "one1");
mapattach.put(a2, "two22");
mapattach.put(a3, "three33");
//java stream
//it will match only one item and it returns
String matchFound = mapattach.entrySet().stream()
.filter( f -> recordIds.contains(f.getKey().getId()))
.findFirst().toString();
以上代码返回一条记录的字符串:
结果:
Optional[class Attachment {
name: one
id: one1
mimeType: null
url: http://1.com
referenceId: null }=one1]
但我想要的只是url 我该怎么办?
【问题讨论】:
标签: stream java-stream