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

Maps and sets of given size after Java 19 #258

Closed
snago opened this issue May 15, 2024 · 3 comments
Closed

Maps and sets of given size after Java 19 #258

snago opened this issue May 15, 2024 · 3 comments

Comments

@snago
Copy link
Contributor

snago commented May 15, 2024

Java 19 introduced new static methods for creating maps and sets so that this (using Maps from Guava)

Map<String, String> map = Maps.newLinkedHashMapWithExpectedSize(numMappings);

can be replaced with

Map<String, String> map = LinkedHashMap.newLinkedHashMap(numMappings);

and similar for HashMap, HashSet, etc.

There's probably some similar Apache Commons methods that can be replaced as well.

@snago
Copy link
Contributor Author

snago commented Jan 7, 2025

Hi! Thanks for adding this @gaul!

The suggested replacements are slightly wrong though for HashMap and HashSet.
The current suggested replacement, java.util.HashMap<>(int), will create a HashMap that can fit 75% of the given size before it is resized. The correct replacement is java.util.HashMap.<>newHashMap(int).
Similar for HashSet, it should be java.util.HashSet.<>newHashSet(int), not java.util.HashSet<>(int).

I could create a PR if you want me to.

@gaul
Copy link
Owner

gaul commented Jan 7, 2025

Please submit a PR to fix this!

@snago
Copy link
Contributor Author

snago commented Jan 8, 2025

#315

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants