Skip to content
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

Sets.cartesianProduct gives undocumented IllegalArgumentException due to its size #3742

Open
Meijuh opened this issue Dec 19, 2019 · 1 comment
Labels
P3 package=collect type=api-docs Change/add API documentation type=defect Bug, not working as expected

Comments

@Meijuh
Copy link

Meijuh commented Dec 19, 2019

The following piece of code throws an undocumented IllegalArgumentException:

final Set<String> products = Sets.newHashSet("coffee", "tea");
final List<Set<String>> productOf = Collections.nCopies(31, products);    
assert Sets.cartesianProduct(productOf).size() == Integer.MAX_VALUE;

The fact that Sets::cartesianProduct will throw an IllegalArgumentException when there would be more than 2^31-1 elements in the resulting set is not documented as far as I can see.

I would like to see one of two solutions:

  1. Document why this Exception is thrown (there may be an implementation detail that prevents even defining such large sets).
  2. Fix it, because java.util.Set::size() specifies that if the size would be more than Integer.MAX_VALUE it would simply be Integer.MAX_VALUE.

I like to see this fixed (solution 2), because now I can't even define such a Cartesian product. Iterating over so many elements is of course a different issue, but I have a piece of code that may very quickly find an element in the product, because I know it is in there and due to some ordering on element.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 package=collect type=api-docs Change/add API documentation type=defect Bug, not working as expected
Projects
None yet
Development

No branches or pull requests

3 participants