Add Lists.partitionBy to split a list into a list of lists
See original GitHub issueSometimes 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 sizejava.util.stream.Collectors.groupingBy
(and very similarCollectors.partitioningBy
) only expect a predicate (not aBiPredicate
), so the caller cannot compare two items directly. They also return aMap
, 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:
- Created 5 years ago
- Reactions:1
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the pointer, I have not considered that! However,
Multimaps.index
returns a map, so it’s very similar toCollectors.groupingBy
: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:
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!