簡體   English   中英

我想創建唯一的用戶ID

[英]I want to create unique user id

我只是創建唯一的ID,但使用隨機數。 但我想創建該ID,例如jason-001,smith-002等。

Integer user_id= new Random().nextInt();

//implementation
reference_uploaded_by.getRef().setValue(name.getText().toString() + "-" + user_id);

您可以將AtomicLong#incrementAndGet用作計數器。 “原子”表示該類是線程安全的

AtomicLong userIdIncrementor = new AtomicLong(previouslyUsedNumber);

使用該對象獲取每個新用戶的遞增編號。

long number = userIdIncrementor.incrementAndGet();
String userId = userName + String.format("%03d", number);

此代碼假定您永遠不會有超過一千個用戶,並且您在“問題”中指定的號碼只有三位數字。

有關格式化整數的詳細信息,請參見此答案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM