I need to use and recycle ids(ints) within a range say from 1 to 20million.
what is the most efficient way to do this?
Somethings i tried.
Generate a running sequence of numbers from 1 to k and store in a map. after k numbers if lets say if id 2 becomes free we delete it from the map. And continue our next id from k+1 (it will be good if i can choose the id that was freed from the beginning(2) instead of k+1. how can i do this ? )
Generate random numbers in between range 1 to 20 million and check if its already used with a map lookup, if yes, choose another random number or do number+1 until map lookup fails.
Storing all numbers from 1 to 20million in a set and taking one by one for use and add back when it's freed( this will have bigger memory footprint and don't want to do this)
What is the most efficent way to solve this problem, if lets say around 50% of ids are used at any point of time