Skip to content

Instantly share code, notes, and snippets.

@Sythelux
Created May 13, 2016 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sythelux/846f4474e3d0aa170e3c54401061eff6 to your computer and use it in GitHub Desktop.
Save Sythelux/846f4474e3d0aa170e3c54401061eff6 to your computer and use it in GitHub Desktop.
Little ID Generator, that makes ids reusable after freeing
public class ReusableIDGenerator {
private static Map<Integer, Boolean> map = Collections.synchronizedMap(new HashMap<Integer, Boolean>());
private ReusableIDGenerator() {}
public static int get() {
for (int i = 0; i < Integer.MAX_VALUE; i++) {
if (map.containsKey(i)) {
if (!map.get(i)) {
map.put(i, true);
return i;
}
} else {
map.put(i, true);
return i;
}
}
return -1;
}
public static void free(int i) {
map.put(i, false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment