Reputation: 2120
I need to generate a unique Long
of length of 10 or more digits from String seed that can be of any length. In normal circumstances it would be not possible to do since there are more String
permutations than Long
can store, however i know that system will not generate more Strings
than i can store in Long
, how can i generate unique Long
for each String
in such circumstances?
I can not use dynamic perfect hashing since is too time consuming and i can not use minimal perfect hash function since i dont want generated numbers to seqentional.
EDIT: i can not store any information about already processed Strings, including amount of them
Upvotes: 2
Views: 1377
Reputation: 533492
You can use a counter like this
final AtomicLong counter = new AtomicLong();
final Map<String, Long> idMap = new LinkedHashMap<>();
public long idFor(String s) {
return idMap.computeIfAbsent(s, isMap::incrementAndGet);
}
This will return a unique id for each String.
Upvotes: 6