任何人都可以帮我转换java流吗?
private List<String> takeAllPages(Long iId, String clientId, List<PageSummaryList> pages) {
IssueHeader issuetHeader = issueApi.get(iId);
String issueSub = issueHeader.getSubject();
Set<String> samplePageNames = new HashSet<>();
for (PageSummaryList samplePage : pages) {
String entityId = "issue-" + clientId + "-doc-" + samplePage.getName().replace(" ", "-");
List<TickType> tickList = tickTypeApi.getTickForEntity(EntityType.DELL_SOP, entityId);
for (TickType tickType : tickList) {
String tickName = tickType.getName();
if (issueSub.contains(tickName)) {
samplePageNames.add(samplePage.getName());
}
}
}
return Lists.newArrayList(samplePageNames);
}
答案 0 :(得分:0)
因为你没有提供你的代码使用的每个类我都无法测试它,但这是一个应该工作的实现(或者一些小的修复可以工作)。
private List<String> takeAllPages(Long iId, String clientId, List<PageSummaryList> pages){
IssueHeader issuetHeader = issueApi.get(iId);
String issueSub = issueHeader.getSubject();
return pages.stream()
.flatMap(sp -> {
String entityId = "issue-" + clientId + "-doc-" + sp.getName().replace(" ", "-");
return tickTypeApi.getTickForEntity(EntityType.DELL_SOP, entityId).stream();
})
.filter(tickType -> !issueSub.contains(tickType.getName()))
.collect(Collectors.toList());
}
编辑在基本代码中有一些概念错误(我没有看到你的要求中的一些细节,对不起)但我已经制作了另一个版本,我将保留第一个实现仅供参考
这是正确的代码我认为xD(测试它,我们会弄清楚)。
private List<String> takeAllPages(Long iId, String clientId, List<PageSummaryList> pages){
IssueHeader issuetHeader = issueApi.get(iId);
String issueSub = issueHeader.getSubject();
// Until here it is the same as the code you provided
// This code should do the exact same as yours but using Streams API.
// Using the @stream() method i am converting your @pages list (List<PageSummaryList>) to
// a Stream<PageSummaryList>, so that i can perform various operations upon every item in that list (@pages).
return pages.stream()
// Firstly i am converting every PageSummaryList into a Pair (javafx.util.Pair but if you want you can create your own).
// This pair contains a PageSummaryList and its corresponding Stream<TickType>, basically your first for loop.
// After this map operation i will have a Stream<Pair<PageSummaryList, List<TickType>>>.
.map(sp -> {
String entityId = "issue-" + clientId + "-doc-" + sp.getName().replace(" ", "-");
List<TickType> stream = tickTypeApi.getTickForEntity(EntityType.DELL_SOP, entityId);
return new Pair<PageSummaryList, List<TickType>>(sp, stream);
})
// Here i have Stream<Pair<PageSummaryList, List<TickType>>>
// and i want only to keep those where their List<TickType> contains an element whose name is in @issueSub
.filter(pair -> {
return pair.getValue().stream()
.filter( tp -> !issueSub.contains(tp.getName())
.count() > 0;
})
// Now i want only the names (its basically i want the Stream<String> (it is in someway your Set<String>
.map(pair -> pair.getKey().getName())
.collect(Collectors.toList()); // this is what you want to return, a List<String> (equal to your return statement.
}