无法生成唯一的用户编号

时间:2022-11-25 13:02:51

I have a problem when I'm trying to generate a unique customer-id in my application. I want the numbers to start from 1 and go up. I have a register-class using tree-map that generates the next customer-number using this code:

当我尝试在我的应用程序中生成唯一的customer-id时,我遇到了问题。我希望数字从1开始然后上升。我有一个使用树形图的寄存器类,它使用以下代码生成下一个客户编号:

public String generateNumber()
{
    int number = 1;

    for(Map.Entry<String, Forsikringkunde> entry : this.entrySet())
    {
        if(entry.getValue().getNumber().equals(String.valueOf(number)))
        {
            number++;
        } 
    }return String.valueOf(number);
}

When I generate customers in my application I get duplicates of the numbers even though I iterate through the map. When creating a customer I create the object, run this method, use a set-method for the ID and adds it to the register, but it doesn't work. Anyone have a solution?

当我在我的应用程序中生成客户时,即使我遍历地图,我也会得到重复的数字。创建客户时,我创建了对象,运行此方法,使用ID的set方法并将其添加到寄存器,但它不起作用。有人有解决方案吗?

2 个解决方案

#1


Modify the code to instead find the maximum number in your map, and then use that+1:

修改代码,找到地图中的最大数字,然后使用+ 1:

public String generateNumber()
{
    int max = -1;

    for(Map.Entry<String, Forsikringkunde> entry : this.entrySet())
    {
        int entry = Integer.parseInt(entry.getValue().getNumber());
        if(entry > max)
        {
            max = entry;
        } 
    }
    return String.valueOf(max + 1);
}

(This mimics your coding style. aioobe's answer shows how to do the same thing more elegantly.)

(这模仿了你的编码风格.aioobe的答案显示了如何更优雅地做同样的事情。)

Your method doesn't work because the map is not iterated in order. For example, here's what happens if you iterate through two users with number 2 and 1 respectively:

您的方法不起作用,因为地图没有按顺序迭代。例如,如果您分别遍历编号为2和1的两个用户,会发生什么:

  1. Start with "number = 1"
  2. 以“number = 1”开头

  3. Check if number == 2: it's not, so continue
  4. 检查数字== 2:不是,所以继续

  5. Check if number == 1: it is, so set number = 2
  6. 检查数字== 1:是,所以设置数字= 2

Now the loop is done and number is 2, even though a user with id 2 already exists. If it had been iterated in order, it would have worked.

现在循环完成,数字为2,即使已存在id为2的用户。如果它按顺序迭代,它就会起作用。

#2


If you're on Java 8, I suggest you try this:

如果你使用的是Java 8,我建议你试试这个:

int max = this.values()
              .stream()
              .map(Forsikringkunde::getNumber)
              .mapToInt(Integer::parseInt)
              .max()
              .orElse(0);

return String.valueOf(max + 1);

#1


Modify the code to instead find the maximum number in your map, and then use that+1:

修改代码,找到地图中的最大数字,然后使用+ 1:

public String generateNumber()
{
    int max = -1;

    for(Map.Entry<String, Forsikringkunde> entry : this.entrySet())
    {
        int entry = Integer.parseInt(entry.getValue().getNumber());
        if(entry > max)
        {
            max = entry;
        } 
    }
    return String.valueOf(max + 1);
}

(This mimics your coding style. aioobe's answer shows how to do the same thing more elegantly.)

(这模仿了你的编码风格.aioobe的答案显示了如何更优雅地做同样的事情。)

Your method doesn't work because the map is not iterated in order. For example, here's what happens if you iterate through two users with number 2 and 1 respectively:

您的方法不起作用,因为地图没有按顺序迭代。例如,如果您分别遍历编号为2和1的两个用户,会发生什么:

  1. Start with "number = 1"
  2. 以“number = 1”开头

  3. Check if number == 2: it's not, so continue
  4. 检查数字== 2:不是,所以继续

  5. Check if number == 1: it is, so set number = 2
  6. 检查数字== 1:是,所以设置数字= 2

Now the loop is done and number is 2, even though a user with id 2 already exists. If it had been iterated in order, it would have worked.

现在循环完成,数字为2,即使已存在id为2的用户。如果它按顺序迭代,它就会起作用。

#2


If you're on Java 8, I suggest you try this:

如果你使用的是Java 8,我建议你试试这个:

int max = this.values()
              .stream()
              .map(Forsikringkunde::getNumber)
              .mapToInt(Integer::parseInt)
              .max()
              .orElse(0);

return String.valueOf(max + 1);