如何将位组合成long以创建唯一ID?

时间:2022-11-25 14:08:41

I would like to write a utility that will provide me with a relatively unique ID in Java. Something pretty simple, like x bits from timestamp + y bits from random number.

我想编写一个实用程序,它将在Java中为我提供一个相对独特的ID。非常简单的东西,比如来自时间戳的x位+来自随机数的y位。

So, how would I implement the following method:

那么,我将如何实现以下方法:

long getUniqueID()
{
    long timestamp = System.currentTimeMillis();
    long random = some random long

    ...

    return id;
}

BONUS

Any suggestions for other easily obtainable information I could use to form my ID?

我可以用来形成我的身份证的其他容易获得的信息的建议吗?

note: I am aware of GUIDs and I know Java has a UUID class, but I don't want something that is 128 bits long.

注意:我知道GUID,我知道Java有一个UUID类,但我不想要128位长的东西。

3 个解决方案

#1


3  

Just clip the bits you don't need:

只需剪掉你不需要的部分:

return java.util.UUID.randomUUID().getLeastSignificantBits();

#2


1  

What you are trying to do is create a hash function that combines two long values into a single long value. In this case, the uniformity of the hash function will be of utmost importance since collisions in created unique ID values are unacceptable. However, if you can compare hash values to previously created identifiers, then collisions can be resolved by modifying the hash until no collision occurs.

您要做的是创建一个哈希函数,将两个长值组合成一个长值。在这种情况下,散列函数的一致性将是最重要的,因为创建的唯一ID值中的冲突是不可接受的。但是,如果您可以将哈希值与先前创建的标识符进行比较,则可以通过修改哈希值来解决冲突,直到不发生冲突。

For example, you could take the time stamp and perform an exclusive-or (using the caret ^ operator in Java) with the random value. If a collision is detected, then add one to the result.

例如,您可以使用随机值获取时间戳并执行exclusive-or(使用Java中的插入符号^运算符)。如果检测到碰撞,则在结果中添加一个。

#3


1  

If unique in the same JVM is enough then something like this should do the job.

如果同一个JVM中的唯一性足够,那么这样的事情应该可以胜任。

public class UniqueID {
  static long current= System.currentTimeMillis();
  static public synchronized long get(){
    return current++;
    }
}

#1


3  

Just clip the bits you don't need:

只需剪掉你不需要的部分:

return java.util.UUID.randomUUID().getLeastSignificantBits();

#2


1  

What you are trying to do is create a hash function that combines two long values into a single long value. In this case, the uniformity of the hash function will be of utmost importance since collisions in created unique ID values are unacceptable. However, if you can compare hash values to previously created identifiers, then collisions can be resolved by modifying the hash until no collision occurs.

您要做的是创建一个哈希函数,将两个长值组合成一个长值。在这种情况下,散列函数的一致性将是最重要的,因为创建的唯一ID值中的冲突是不可接受的。但是,如果您可以将哈希值与先前创建的标识符进行比较,则可以通过修改哈希值来解决冲突,直到不发生冲突。

For example, you could take the time stamp and perform an exclusive-or (using the caret ^ operator in Java) with the random value. If a collision is detected, then add one to the result.

例如,您可以使用随机值获取时间戳并执行exclusive-or(使用Java中的插入符号^运算符)。如果检测到碰撞,则在结果中添加一个。

#3


1  

If unique in the same JVM is enough then something like this should do the job.

如果同一个JVM中的唯一性足够,那么这样的事情应该可以胜任。

public class UniqueID {
  static long current= System.currentTimeMillis();
  static public synchronized long get(){
    return current++;
    }
}