Skip to content

Instantly share code, notes, and snippets.

@apangin
Created May 23, 2024 23:22
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 + args[1].hashCode();
case 3:
return 29791 + Objects.hashCode(args[0]) * 961 + args[1].hashCode() * 31 + args[2].hashCode();
}
}
return Arrays.hashCode(args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment