StaffOperInfo before = null;
for (StaffOperInfo so : staffMap.get(staffId)) {
if (before == null) {
before = so;
} else {
if (so.getTermIp()!=null&&!so.getTermIp().equals(before.getTermIp())
&& so.getAcceptDate().getTime()-
before.getAcceptDate().getTime()<step*1000) {
if (filterMap.containsKey(staffId)) {
filterMap.get(staffId).add(before);
filterMap.get(staffId).add(so);
} else {
List<StaffOperInfo> ll = new ArrayList<>();
ll.add(before);
ll.add(so);
filterMap.put(staffId, ll);
}
}
before = so;
}
}
答案 0 :(得分:1)
Streams不会让你的代码变得更好。如果您的集合是List或Array(如果没有,这不会起作用),下面说的是这样做的方式之一:
List<StaffOperInfo> list = staffMap.get(staffId);
IntStream.range(1, list.size() - 1) // Stream on Indices of the List
.mapToObj(i -> new StaffOperInfo[]{list.get(i - 1), list.get(i)}) // Get pair of StaffOperInfo
.filter(arr -> !Objects.equals(arr[0].getTermIp(), arr[1].getTermIp()) // Filter pairs based on your if condition
&& (arr[1].getAcceptDate().getTime() - arr[0].getAcceptDate().getTime() < step * 1000))
.forEach(arr -> {
if (filterMap.containsKey(staffId))
filterMap.get(staffId).addAll(Arrays.asList(arr));
else
filterMap.put(staffId, Arrays.asList(arr));
});
答案 1 :(得分:1)
正如@Dark发布的那样,通过原生Stream API进行操作并不容易/简单。但你可以尝试StreamEx
StreamEx.of(staffMap.get(staffId)).forPairs((before, so) -> {
if (so.getTermIp()!=null&&!so.getTermIp().equals(before.getTermIp())
&& so.getAcceptDate().getTime()-
before.getAcceptDate().getTime()<step*1000) {
List<StaffOperInfo> ll = filterMap.computeIfAbsent(staffId, k -> new ArrayList<>());
ll.add(before);
ll.add(so);
}
});