如何生成长度小于 8 个字符的唯一数字

发布于 2024-08-26 11:25:09 字数 1057 浏览 10 评论 0原文

我想每次调用方法generateCustumerId()时生成唯一的id。生成的 id 长度必须为 8 个字符或少于 8 个字符。 这个要求是必要的,因为我需要将其存储在数据文件中,并且该 id 的模式被确定为 8 个字符长。

选项 1 效果很好。我想使用 UUID,而不是选项 1。问题是 UUID 生成的 id 有很多字符。有人知道如何生成小于 99999999 的唯一 ID 吗?

选项 1

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class CustomerIdGenerator {

private static Set<String> customerIds = new HashSet<String>();
private static Random random = new Random();

// XXX: replace with java.util.UUID
public static String generateCustumerId() {

    String customerId = null;
    while (customerId == null || customerIds.contains(customerId)) {
        customerId = String.valueOf(random.nextInt(89999999) + 10000000);
    }
    customerIds.add(customerId);
    return customerId;
}

}

选项2生成的唯一 ID 太长

 public static String generateCustumerId() {
    String ownerId = UUID.randomUUID().toString();
    System.out.println("ownerId " + ownerId);
    return ownerId
}

I want to generate unique id's everytime i call methode generateCustumerId(). The generated id must be 8 characters long or less than 8 characters.
This requirement is necessary because I need to store it in a data file and schema is determined to be 8 characters long for this id.

Option 1 works fine. Instead of option 1, I want to use UUID. The problem is that UUID generates an id which has to many characters. Does someone know how to generate a unique id which is less then 99999999?

option 1

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class CustomerIdGenerator {

private static Set<String> customerIds = new HashSet<String>();
private static Random random = new Random();

// XXX: replace with java.util.UUID
public static String generateCustumerId() {

    String customerId = null;
    while (customerId == null || customerIds.contains(customerId)) {
        customerId = String.valueOf(random.nextInt(89999999) + 10000000);
    }
    customerIds.add(customerId);
    return customerId;
}

}

option2 generates an unique id which is too long

 public static String generateCustumerId() {
    String ownerId = UUID.randomUUID().toString();
    System.out.println("ownerId " + ownerId);
    return ownerId
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

变身佩奇 2024-09-02 11:25:09

也许 Commons Id 项目 有一定用处。 字母数字生成器 (以及它们的其他生成器)采用长度参数。当然,这实际上只支持一个序列(这意味着您必须知道/确定应用程序重新启动时的最后一个最高标识符)。

或者,您可以尝试类似本地时间的 CRC-32 校验和或随机生成的字节数组(CRC32 是 内置于 Java 标准库)。

Maybe the Commons Id project is of some use. The alphanumeric generator (and their other generators as well) takes a length argument. Of course, that really just supports a sequence (which implies that you'll have to know /determine the last highest identifier on application restarts).

Alternatively, you might try something like the CRC-32 checksum of the localtime or a randomly generated array of bytes (CRC32 is built into the Java Standard Library).

冰葑 2024-09-02 11:25:09

它需要是唯一的还是随机的?如果它只需要唯一,您可以在应用程序启动时从数据存储中加载最高值(假设只有一个应用程序正在写入数据文件)。您拥有最高价值:

public class IdGenerator{
  private String value;

  public IdGenerator( String initial value ){
    this.value = value;
  }

  public synchronized String nextValue(){
    value = incrementValue( value );
    return value;
  }

  private static String nextValue( String currentValue ){
    // Somehow increment the value.
    return incrementedValue;
  }

}

根据您在 uuid 中允许的字符,可以通过多种方式完成此操作。我会读取当前字符串的最后一个字符,检查它是否是最后一个允许的字符。是这样,增加第7个字符,依此类推。否则,增加最后一个字符。

Does it need to be unique or random? If it only needs to be unique, you could load the highest value from the datastore when the application is launched (assuming only one app is writing to the data file). One you have your highest value:

public class IdGenerator{
  private String value;

  public IdGenerator( String initial value ){
    this.value = value;
  }

  public synchronized String nextValue(){
    value = incrementValue( value );
    return value;
  }

  private static String nextValue( String currentValue ){
    // Somehow increment the value.
    return incrementedValue;
  }

}

Depending on what characters you allow in you uuid this can be done various ways. I would read the last character of the current String, check if it's the last allowed character. Is so, increment the 7th character and so on. Else, increment the last character.

自控 2024-09-02 11:25:09

这不是世界上最好的解决方案,但是将 uuid 切成适当的长度怎么样?它不会是全局唯一的 - 只是要注意,您不会获得全局唯一的标识符 - 但它可能适用于本地唯一。

Not the best solution in the world, but how about chopping the uuid to the length? It will not be globally unique - just be aware that you will not get a globally unique identifier - but it might work for locally unique.

北音执念 2024-09-02 11:25:09

保留一个持久计数器,每次请求新 ID 时该计数器都会加一。

如果数据文件是一个 SQL 表,您始终可以选择 MAX(id) + 1 并使用它,但是您需要绝对确定当同时需要两个 id 时(并且它们两者具有相同的 MAX(id) 值。请注意,大多数数据库都有为此目的而设计的本机数据类型,对于任何重要的程序,您都应该使用该设施。

Keep a persistent counter which increments by one every time a new ID is requested.

If the data file is an SQL table you can always select MAX(id) + 1, and use that, but you need to be absolutely certain that you do not run into multitasking issues when two id's are needed at the same time (and they both have the same value from MAX(id)). Note that most databases have a native datatype designed for this purpose and for any non-trivial program you should use that facility.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文