简体   繁体   English

在Java中分配对象ID的优雅方式

[英]Elegant way to assign object id in Java

I have a class for objects ... lat's say apples. 我有一个对象类...拉特说苹果。

Each apple object mush have a unique identifier (id)... how do I ensure (elegantly and efficiently) that newly created has unique id. 每个苹果对象都有一个唯一的标识符(id)...我如何确保(优雅和有效)新创建的具有唯一ID。

Thanks 谢谢

have a static int nextId in your Apple class and increment it in your constructor. 在Apple类中有一个static int nextId并在构造函数中递增它。

It would probably be prudent to ensure that your incrementing code is atomic, so you can do something like this (using AtomicInteger). 确保增量代码是原子代码可能是谨慎的,所以你可以做这样的事情(使用AtomicInteger)。 This will guarantee that if two objects are created at exactly the same time, they do not share the same Id. 这将保证如果两个对象完全同时创建,则它们不共享相同的Id。

public class Apple {
    static AtomicInteger nextId = new AtomicInteger();
    private int id;

    public Apple() {
        id = nextId.incrementAndGet();
   }
}

Use java.util.UUID.randomUUID() 使用java.util.UUID.randomUUID()

It is not int , but it is guaranteed to be unique: 它不是int ,但保证是唯一的:

A class that represents an immutable universally unique identifier (UUID). 表示不可变通用唯一标识符(UUID)的类。


If your objects are somehow managed (for example by some persistence mechanism), it is often the case that the manager generates the IDs - taking the next id from the database, for example. 如果您的对象以某种方式进行管理(例如通过某种持久性机制),则通常会出现管理器生成ID的情况 - 例如,从数据库中获取下一个ID。

Related: Jeff Atwood's article on GUIDs (UUIDs). 相关: Jeff Atwood关于GUID (UUID) 的文章 It is database-related, though, but it's not clear from your question whether you want your objects to be persisted or not. 但是,它与数据库相关,但是从您的问题中不清楚您是否希望保留对象。

Have you thought about using UUID class. 你有没有想过使用UUID类。 You can call the randomUUID() function to create a new id everytime. 您可以调用randomUUID()函数每次创建一个新的id。

There is another way to get unique ID's. 还有另一种获取唯一ID的方法。 Instead of using an int or other data type, just make a class: 不要使用int或其他数据类型,只需创建一个类:

final class ID
{
  @Override
  public boolean equals(Object o)
  {
     return this==o;
  }
}

public Apple
{
  final private ID id=new ID();
}

Thread safe without synchronizing! 线程安全无需同步!

如何以优雅的方式使用 Java8 返回 map<object, long> 来自列表<object>计数发生?<div id="text_translate"><p> 我知道标题很拗口,所以我将尝试用一个例子来解释。</p><p> 我有一个清单。 Chores 有一个 id 字段作为标识符,但列表将包含许多家务,其中一些将具有相同的 ID。 我的目标是返回一个 Map&lt;Chore, Long&gt; ,其中 long 将计算杂务的出现次数。 例如:</p><pre> List&lt;Chore&gt; choreList = new ArrayList&lt;Chore&gt;(); choreList.add(new Chore("1", "vacuum", "7-6-22"); //Numbers 1,2,3 are the ID's for each chore choreList.add(new Chore("2", "dusting", "7-1-22"); choreList.add(new Chore("3", "mowing", "7-15-22"); choreList.add(new Chore("1", "vacuum", "7-14-22"); choreList.add(new Chore("1", "vacuum", "7-18-22"); choreList.add(new Chore("2", "dusting", "7-24-22");</pre><p> 使用此列表和 Java8 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ function,我想返回一个 Map&lt;Chore, Long&gt;,其中包含家务 object 和事件。 因此,对于 ID 为“1”的杂项,长为 3。对于 ID 为“2”的杂项,长为 2……等等。</p><p> 我试图为此找到一个优雅的解决方案,但看起来每个人都可以得到如下内容:</p><pre> Map&lt;String, Long&gt; choresById = chores.stream().collect(Collectors.groupingBy(Chore::getId, Collectors.counting()));</pre><p> 我怎样才能想出 Map&lt;Chore, Long&gt; 而不是 Map&lt;String, Long&gt; (在这种情况下,字符串是 ID?)</p></div></object></object,> - How to use Java8 in an elegant way to return a map of <Object, Long> from a List<Object> counting occurrence?

暂无
暂无

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

相关问题 Java 如果 Number 是 null,是否有任何优雅的方法可以为 Number 分配默认值 - Java Is there any elegant way to assign a default value to Number if that Number is null 在Java中进行对象到对象转换的简单/优雅方法? - Simple/elegant way to do object to object transformation in Java? 在Java中从向量添加不同对象的最优雅方法 - Most elegant way to add a different object from vector in Java 更好地为Java中的对象数组赋值 - Better way to assign value to array of object in Java 在Java中转义$的最优雅方法 - most elegant way of escaping $ in java 如何以优雅的方式使用 Java8 返回 map<object, long> 来自列表<object>计数发生?<div id="text_translate"><p> 我知道标题很拗口,所以我将尝试用一个例子来解释。</p><p> 我有一个清单。 Chores 有一个 id 字段作为标识符,但列表将包含许多家务,其中一些将具有相同的 ID。 我的目标是返回一个 Map&lt;Chore, Long&gt; ,其中 long 将计算杂务的出现次数。 例如:</p><pre> List&lt;Chore&gt; choreList = new ArrayList&lt;Chore&gt;(); choreList.add(new Chore("1", "vacuum", "7-6-22"); //Numbers 1,2,3 are the ID's for each chore choreList.add(new Chore("2", "dusting", "7-1-22"); choreList.add(new Chore("3", "mowing", "7-15-22"); choreList.add(new Chore("1", "vacuum", "7-14-22"); choreList.add(new Chore("1", "vacuum", "7-18-22"); choreList.add(new Chore("2", "dusting", "7-24-22");</pre><p> 使用此列表和 Java8 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ function,我想返回一个 Map&lt;Chore, Long&gt;,其中包含家务 object 和事件。 因此,对于 ID 为“1”的杂项,长为 3。对于 ID 为“2”的杂项,长为 2……等等。</p><p> 我试图为此找到一个优雅的解决方案,但看起来每个人都可以得到如下内容:</p><pre> Map&lt;String, Long&gt; choresById = chores.stream().collect(Collectors.groupingBy(Chore::getId, Collectors.counting()));</pre><p> 我怎样才能想出 Map&lt;Chore, Long&gt; 而不是 Map&lt;String, Long&gt; (在这种情况下,字符串是 ID?)</p></div></object></object,> - How to use Java8 in an elegant way to return a map of <Object, Long> from a List<Object> counting occurrence? 在抽象类上实现ID的一种优雅方法 - A elegant way to implement ID on a abstract class Hamcrest - 测试具有相同属性值的复杂对象的优雅方式 - Hamcrest - Elegant way to test complex object with samepropertyvaluesas 是否有一种优雅的方式来展开包装在2个嵌套的Optionals中的对象? - Is there an elegant way to unwrap an object wrapped in 2 nested Optionals? 用FilterInputStream对象读取大数据的一种优雅方法 - elegant way to read large data with FilterInputStream object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM