Java中作为参数传递列表
我今天有一张单子Java中作为参数传递列表,java,arrays,arraylist,Java,Arrays,Arraylist,我今天有一张单子 [Attachments(filename=a.json, id=ATT-mXRJB-BmVzs, contentType=application/json), Attachments(filename=b.pdf, id=ATT-y7Qr2-8RqkW, contentType=application/pdf ), Attachments(filename=c.docx, id=ATT-mYh3z-3YJ37, contentType=application/vnd
[Attachments(filename=a.json, id=ATT-mXRJB-BmVzs, contentType=application/json),
Attachments(filename=b.pdf, id=ATT-y7Qr2-8RqkW, contentType=application/pdf ),
Attachments(filename=c.docx, id=ATT-mYh3z-3YJ37, contentType=application/vnd.openxmlformats-officedocument.wordprocessingml.document)]
我需要迭代并从此列表中获取id,并将每个id作为API调用的参数传递-attachments/{{attachmentid}}/retrieve
我能够检索ID并将其存储在地图中。(不确定此处使用地图是否正确?)
这是我编写的代码片段,但之后我无法继续
Map<String, String> attachmentMap = new HashMap<String, String>();
Optional.ofNullable(attList)
.orElse(Collections.emptyList())
.forEach(aList -> {
attachmentMap.put("id", aList.getId());
LOGGER.debug("Attachment ids : {}",attachmentMap);
});
我能够打印附件ID—但是如何将它们作为参数传递给这样的api调用—attachments/{{attachmentid}}/retrieve请使用foreach在映射中迭代并调用http调用方法
Map<String, String> attachmentMap = new HashMap<String, String>();
Optional.ofNullable(attList)
.orElse(Collections.emptyList())
.forEach(aList -> {
attachmentMap.put("id", aList.getId());
httpCall(aList.getId());
LOGGER.debug("Attachment ids : {}",attachmentMap);
});
请使用foreach遍历map并调用http调用方法
Map<String, String> attachmentMap = new HashMap<String, String>();
Optional.ofNullable(attList)
.orElse(Collections.emptyList())
.forEach(aList -> {
attachmentMap.put("id", aList.getId());
httpCall(aList.getId());
LOGGER.debug("Attachment ids : {}",attachmentMap);
});
为什么不直接循环
附件
列表,从每个附件
中检索id,并将其传递给UriComponentsBuilder以构建URI呢。您不需要额外的数据结构来保存ID
List<URI> uriList = new ArrayList<>();
for(Attachment attachment: Attachments){
String id = attachment.getId();
if (id != null) {
URI uri = UriComponentsBuilder.
.pathSegment(ATTACHMENTS)
.pathSegment(id)
.pathSegment(RETRIEVE)
.build().toUri();
uriList.add(uri); // or you can call the uri directly from here using your http client.
}
}
List uriList=new ArrayList();
用于(附件:附件){
String id=attachment.getId();
如果(id!=null){
URI=UriComponentsBuilder。
.pathSegment(附件)
.pathSegment(id)
.pathSegment(检索)
.build().toUri();
添加(uri);//或者您可以从这里使用http客户端直接调用uri。
}
}
为什么不直接循环附件
列表,从每个附件
中检索id,并将其传递给UriComponentsBuilder以构建URI。您不需要额外的数据结构来保存ID
List<URI> uriList = new ArrayList<>();
for(Attachment attachment: Attachments){
String id = attachment.getId();
if (id != null) {
URI uri = UriComponentsBuilder.
.pathSegment(ATTACHMENTS)
.pathSegment(id)
.pathSegment(RETRIEVE)
.build().toUri();
uriList.add(uri); // or you can call the uri directly from here using your http client.
}
}
List uriList=new ArrayList();
用于(附件:附件){
String id=attachment.getId();
如果(id!=null){
URI=UriComponentsBuilder。
.pathSegment(附件)
.pathSegment(id)
.pathSegment(检索)
.build().toUri();
添加(uri);//或者您可以从这里使用http客户端直接调用uri。
}
}