使用java流仅基于属性对连续元素进行分组

trnvg8h3  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(300)

我有一个对象列表,每个对象都有一个id。如果元素是连续的,我想按id对它们进行分组。例如,如果对象是:

(id1, id1, id1, id2, id2, id3, id3, id2, id2, id4)

则组必须是:

(id1, id1, id1), (id2, id2), (id3, id3), (id2, id2), (id4)

这可以通过javastreamsapi实现吗?

hyrbngr7

hyrbngr71#

为了方便导航到最后一个(上一个)项目,我将使用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);
    ...
}
ajsxfq5m

ajsxfq5m2#

这是非常有可能使用可变的减少导致 List<List<String>> . 为了更好的可读性,我建议分为以下几个方法:

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);
    }
}

考虑到以下几点 list 输入:

List<String> list = List.of(
    "id1", "id1", "id1", "id2", "id2", "id3", "id3", "id2", "id2", "id4");

... 打印结果时,输出如下所示: [[id1, id1, id1], [id2, id2], [id3, id3], [id2, id2], [id4]]

相关问题