question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Lists.partitionBy to split a list into a list of lists

See original GitHub issue

Sometimes you wish to iterate over a list of items in a grouped fashion:

List<Project> trending = Arrays.asList(new Project("guava", "java"),
        new Project("spring", "java"), new Project("node", "js"));

Project current = null;
for (Project p : trending) {
    if (current == null) {
        current = p;
    } else if (!current.getLang().equals(p.getLang())) {
        System.out.println("---");
        current = p;
    }
    System.out.println(p.getName());
}

There already is Lists.partition, but in this case the sub lists might have different sizes (and we don’t know these sizes).

It would be nice to have a standard way to split the list, with only needing to specify the difference between the items:

List<List<Project>> grouped = Lists.partitionBy(trending,
        (p1, p2) -> p1.getLang().equals(p2.getLang()));
for (int i = 0; i < grouped.size(); i++) {
    if (i > 0) {
        System.out.println("---");
    }
    for (Project p : grouped.get(i)) {
        System.out.println(p.getName());
    }
}

Naming

There already is Lists.partition and as that also returns List<List<T>>, I would suggest a similar name. Possible alternatives would be splitBy or groupBy. My suggestion would be partitionBy.

Related

  • Lists.partition, but assumes that each sub list has the same size
  • java.util.stream.Collectors.groupingBy (and very similar Collectors.partitioningBy) only expect a predicate (not a BiPredicate), so the caller cannot compare two items directly. They also return a Map, which is not desired in some cases.

Summary

Add new method List<List<T>> Lists.partitionBy(List<T>, BiPredicate<T, T>).

I would be happy to implement it and if there is agreement on this, I can create a PR. Thanks for reading, and if you have any questions, please ask!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pascalgncommented, May 5, 2018

Thanks for the pointer, I have not considered that! However, Multimaps.index returns a map, so it’s very similar to Collectors.groupingBy:

var result = Multimaps.index(Arrays.asList("Inky", "Blinky", "Pinky", "Pinky", "Clyde"),
        String::length);
System.out.println(result);  // {4=[Inky], 6=[Blinky], 5=[Pinky, Pinky, Clyde]}

Sometimes it might be easier to determine where a list should be split (“no, these two items are different!”) than to assign a suitable key to each item. Additionally, the proposed solution would support cases where you would not want items matched to an “old” group when there are groups in between:

List<List<String>> grouped = Lists.partitionBy(
        Arrays.asList("Pinky", "Blinky", "Pinky", "Pinky"),
        (a, b) -> a.length() == b.length());
System.out.println(grouped);  // [[Pinky], [Blinky], [Pinky, Clyde]]
0reactions
pascalgncommented, Oct 10, 2019

I received some cautious 👍and no 👎, so I created a PR for it. If you have some time, please have a look (it’s small!) and tell me what you think!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Partition a List in Java - Baeldung
In this article, we'll illustrate how to split a List into several sublists of a given size. For a relatively simple operation, ...
Read more >
Efficient way to divide a list into lists of n size - java
I have a quick question about lists.partition what happens if the input list size is less than the expected size of sublist. Does...
Read more >
Partition a List in Python | Split Python List - FavTutor
This method takes the list to be partitioned as its first argument and the size of the chunks as its second argument. The...
Read more >
Partition a list into multiple sublists in Java - Techie Delight
A naive solution is to create m empty lists and process each element of the original list, and add it to the corresponding...
Read more >
Divide a list to lists of n size in Java 8 - Szymon Stepniak
Partitioning (also known as chunking) is an operation that transforms a collection of elements into a collection of chunks of a given size....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found