简体   繁体   English

从字符串生成唯一的 integer ID

[英]Generating a unique integer ID from a String



I need to generate a unique integer id for a string.我需要为字符串生成一个唯一的 integer id。

Reason:原因:
I have a database application that can run on different databases.我有一个可以在不同数据库上运行的数据库应用程序。 This databases contains parameters with parameter types that are generated from external xml data.该数据库包含从外部xml 数据生成的参数类型的参数。 the current situation is that i use the ordinal number of the Enum.目前的情况是我使用枚举的序数。 But when a parameter is inserted or removed, the ordinals get mixed up:但是当插入或删除参数时,序数会混淆:
(FOOD = 0, TOYS = 1) <--> (FOOD = 0, NONFOOD = 1, TOYS = 2) (食品 = 0,玩具 = 1)<-->(食品 = 0,非食品 = 1,玩具 = 2)

The ammount of Parameter types is between 200 and 2000, so i am scared a bit using hashCode() for a string.参数类型的数量在 200 到 2000 之间,所以我有点害怕使用 hashCode() 作为字符串。

PS: I am using Java. PS:我使用的是Java。

Thanks a lot非常感谢

I would use a mapping table in the database to map these Strings to an auto increment value.我会使用数据库中的映射表到 map 这些字符串到自动增量值。 These mapping should then be cached in the application.然后应将这些映射缓存在应用程序中。

Use a cryptographic hash.使用加密 hash。 MD5 would probably be sufficient and relatively fast. MD5 可能就足够了,而且速度相对较快。 It will be unique enough for your set of input.对于您的输入集,它将是独一无二的。

How can I generate an MD5 hash?如何生成 MD5 hash?

The only problem is that the hash is 128 bits, so a standard 64-bit integer won't hold it.唯一的问题是 hash 是 128 位的,因此标准的 64 位 integer 将无法容纳它。

If you need to be absolute certain that the id are unique (no collissions) and your strings are up to 32 chars, and your number must be of no more than 10 digits (approx 32 bits), you obviously cannot do it by a one way function id=F(string) .如果您需要绝对确定 id 是唯一的(没有冲突)并且您的字符串最多为 32 个字符,并且您的号码必须不超过 10 位(大约 32 位),那么您显然不能做到这一点方式 function id=F(string)

The natural way is to keep some mapping of the string to unique numbers (typically a sequence), either in the DB or in the application.自然的方法是在数据库或应用程序中保留字符串到唯一数字(通常是序列)的一些映射。

I came across this post that's sensible: How to convert string to unique identifier in Java我遇到了这个明智的帖子: How to convert string to unique identifier in Java

In it the author describes his implementation:作者在其中描述了他的实现:

public static long longHash(String string) {
  long h = 98764321261L; 
  int l = string.length();
  char[] chars = string.toCharArray();

  for (int i = 0; i < l; i++) {
    h = 31*h + chars[i];
  }
  return h;
}

If you know the type of string values (length, letter patterns), you can count the total number of strings in this set and if it fits within 32 bits, the count function is your integer value.如果您知道字符串值的类型(长度、字母模式),则可以计算该集合中的字符串总数,如果它适合 32 位,则计数 function 就是您的 integer 值。

Otherwise, the string itself is your integer value (integer in math terms, not Java).否则,字符串本身就是您的 integer 值(数学术语中的整数,而不是 Java)。

By Enum you mean a Java Enum?枚举是指 Java 枚举? Then you could give each enum value a unique int by your self instead of using its ordinal number:然后你可以自己给每个枚举值一个唯一的 int 而不是使用它的序数:

public enum MyEnum {

    FOOD(0),
    TOYS(1),

    private final int id;

    private MyEnum(int id)
    {
        this.id = id;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM