[英]Grouping only consecutive elements based on property using Java Streams
I have list of objects, each object having an id.我有对象列表,每个 object 都有一个 ID。 I want to group elements by their id given they are consecutive.
考虑到它们是连续的,我想按它们的 id 对元素进行分组。 Like, if objects are:
就像,如果对象是:
(id1, id1, id1, id2, id2, id3, id3, id2, id2, id4)
then groups must be:那么组必须是:
(id1, id1, id1), (id2, id2), (id3, id3), (id2, id2), (id4)
Can this be achieved by Java Streams API?这可以通过 Java Streams API 来实现吗?
This is very possible using mutable reduction resulting in the List<List<String>>
.这很可能使用可变归约导致
List<List<String>>
。 For better readability, I recommend to split into methods:为了更好的可读性,我建议拆分为方法:
List<List<String>> newList = list.stream().collect(
ArrayList::new,
(lists, string) -> {
if (lists.isEmpty()) {
withNewList(lists, string);
} else {
withNewString(lists, string);
}
},
ArrayList::addAll
);
// adds a new inner list with a single item (string)
static void withNewList(ArrayList<List<String>> lists, String string) {
List<String> newList1 = new ArrayList<>();
newList1.add(string);
lists.add(newList1);
}
static void withNewString(ArrayList<List<String>> lists, String string) {
// if the last inserted list has a same item
List<String> lastList = lists.get(lists.size() - 1);
if (lastList.contains(string)) {
// append it to the last inner list
lastList.add(string);
} else {
// or else create a new list with a single item (string)
withNewList(lists, string);
}
}
Considering the following list
input:考虑以下
list
输入:
List<String> list = List.of(
"id1", "id1", "id1", "id2", "id2", "id3", "id3", "id2", "id2", "id4");
... when you print the result out, the output looks like: ...当您打印出结果时,output 看起来像:
[[id1, id1, id1], [id2, id2], [id3, id3], [id2, id2], [id4]]
For easy navigation to the last (previous) item, I'd use a LinkedList of LinkedLists:为了轻松导航到最后一个(上一个)项目,我将使用 LinkedList 的 LinkedList:
public static void main(String[] args) {
...
Stream.of(id1, id1, id1, id2, id2, id3, id3, id2, id2, id4)
.sequential() // order is essential
.collect(LinkedList::new, (listOfLists, object) -> {
if (listOfLists.isEmpty() || listOfLists.getLast().getLast() != object) {
listOfLists.add(new LinkedList<>(List.of(object)));
} else {
listOfLists.getLast().add(object);
}
}, List::addAll);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.