New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lists.partition() gives incorrect result when partition size is Integer.MAX_VALUE #616
Comments
Original comment posted by boppenheim@google.com on 2011-05-04 at 07:00 AM The behavior is actually whenever you give a partition size >= Integer.MAX_VALUE - (list.size() - 2). On first thought, it seems odd to pass in values like Integer.MAX_VALUE. However, I guess you might be using MAX_VALUE to mean "give me a single partition". What is the use case where you want that though? I suppose we could special case when the partition size is >= to the size of the list. I'm not sure if its worth the extra logic or not. |
Original comment posted by qforce.bak on 2011-05-04 at 08:11 AM I came across this oddity while I was working on an SWT-based table viewer that supports pagination. Internally, the table viewer uses Lists.partition() to split its input elements into pages of a fixed size. By default, the page size is set to Integer.MAX_VALUE so that the table viewer behaves like one without pagination support, i.e. by default it always displays a single page, just like an ordinary table viewer. |
Original comment posted by finnw1 on 2011-05-04 at 09:12 AM @boppenheim, the special-casing you describe will not solve the problem for all cases. Consider:
Here the partition size is smaller than the list, but the overflow still occurs. |
Original comment posted by finnw1 on 2011-05-04 at 09:24 AM See this StackOverflow question for alternative ways to calculate the size that do not suffer from this bug: http://stackoverflow.com/questions/17944/how-to-round-up-the-result-of-integer-division This example is in C#, but everything here is also applicable to Java. |
Original comment posted by kevinb@google.com on 2011-05-05 at 02:09 AM (No comment entered for this change.) Status: |
Original comment posted by kevinb@google.com on 2011-07-13 at 06:18 PM (No comment entered for this change.) Status: |
Original comment posted by kevinb@google.com on 2011-07-13 at 07:25 PM (No comment entered for this change.) Status: |
Original comment posted by wasserman.louis on 2011-08-22 at 03:52 PM (No comment entered for this change.) Status: |
Original issue created by qforce.bak on 2011-05-02 at 07:06 PM
Hello,
It seems the method Lists.partition() behaves incorrectly when given a partition size of Integer.MAX_VALUE or Integer.MAX_VALUE - 1. The following snippet (based on Guava r08) demonstrates this:
List<String> list = Arrays.asList("a", "b", "c");
for (int i = 0; i < 5; i++) {
List<List<String>> partition = Lists.partition(list, Integer.MAX_VALUE - i);
System.out.println("size=" + partition.size() + " for i=" + i);
}
The console output is:
size=-1 for i=0
size=-1 for i=1
size=1 for i=2
size=1 for i=3
size=1 for i=4
I would expect size=1 if i=0 or i=1.
Best regards
Tran Nam Quang
The text was updated successfully, but these errors were encountered: