Skip to content

Instantly share code, notes, and snippets.

@apangin
Last active February 14, 2025 21:29
Show Gist options
  • Save apangin/5f9de9c03d4b785b7ff082ecac6e792a to your computer and use it in GitHub Desktop.
Save apangin/5f9de9c03d4b785b7ff082ecac6e792a to your computer and use it in GitHub Desktop.
Objects.hash alternative capable of allocation elimination
public static int hash(String... args) {
if (args != null) {
switch (args.length) {
case 1:
return 31 + Objects.hashCode(args[0]);
case 2:
return 961 + Objects.hashCode(args[0]) * 31
+ Objects.hashCode(args[1]);
case 3:
return 29791 + Objects.hashCode(args[0]) * 961
+ Objects.hashCode(args[1]) * 31
+ Objects.hashCode(args[2]);
}
}
return Arrays.hashCode(args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment