Java 使用字段的“生成新id”添加新对象

Java 使用字段的“生成新id”添加新对象,java,Java,我有一份清单: private List<Day> days = new ArrayList<>(); 我添加了一个新的Day对象,如: Day day = new Day(); day.setHoliday(1); days.add(day); 如何使添加新元素时,字段id自动设置,该字段等于上一个元素+1? 也许我可以使用Java 8 Streams?使用一个静态变量,比如oldId来存储以前的id。将您的日课程更改为: public class Day impl

我有一份清单:

private List<Day> days = new ArrayList<>();
我添加了一个新的
Day
对象,如:

Day day = new Day();
day.setHoliday(1);
days.add(day);
如何使添加新元素时,字段
id
自动设置,该字段等于上一个元素+1?
也许我可以使用Java 8 Streams?

使用一个静态变量,比如
oldId
来存储以前的
id
。将您的
课程更改为:

public class Day implements Identifiable {
    private static Integer oldId = 0;
    private Integer id;
    private byte isHoliday;

    public Day() {
        this.id = oldId + 1;
        oldId++;
    }
    //getters and setters
}

使用静态变量say
oldId
存储以前的
id
。将您的
课程更改为:

public class Day implements Identifiable {
    private static Integer oldId = 0;
    private Integer id;
    private byte isHoliday;

    public Day() {
        this.id = oldId + 1;
        oldId++;
    }
    //getters and setters
}

您可以使用静态成员和构造函数:

public class Day implements Identifiable {
  static private int maxId = 0;
  final private Integer id;
  private byte isHoliday;

  public Day() {
    this.id = maxId;
    maxId++;
  }
}
每次创建当天的新实例时,其id成员都设置为maxId的值,然后maxId递增


将“id”设为final是个好主意,因为它用于标识对象。

您可以使用静态成员和构造函数:

public class Day implements Identifiable {
  static private int maxId = 0;
  final private Integer id;
  private byte isHoliday;

  public Day() {
    this.id = maxId;
    maxId++;
  }
}
每次创建当天的新实例时,其id成员都设置为maxId的值,然后maxId递增


将“id”设为final是一个好主意,因为它用于标识对象。

如果您可以尝试使用简单类型,而不是面向对象的类型,即,而不是整数到整数

public class Day {
    private static int serial = 0; //static means that this is a common field (the same place in memory) for all created objects.
    private final int id; //final means that another value / object can not be assigned to this reference after initializing in the constructor.

    public Day() {
        id = serial++;
    }
    //getters and setters
}

如果您可以尝试使用简单类型,而不是面向对象的类型,即不使用整数到整数

public class Day {
    private static int serial = 0; //static means that this is a common field (the same place in memory) for all created objects.
    private final int id; //final means that another value / object can not be assigned to this reference after initializing in the constructor.

    public Day() {
        id = serial++;
    }
    //getters and setters
}
您可以使用AtomicInteger()-它是线程安全的

 public class Day implements Identifiable {
  private static AtomicInteger count = new AtomicInteger(0);
  private int id;
  private byte isHoliday;

  public Day() {
    this.id = count.incrementAndGet(); 
  }
}
您可以使用AtomicInteger()-它是线程安全的

 public class Day implements Identifiable {
  private static AtomicInteger count = new AtomicInteger(0);
  private int id;
  private byte isHoliday;

  public Day() {
    this.id = count.incrementAndGet(); 
  }
}

列表
周围做一个包装,并有一个方法
addDay
来满足您的需要可能是最简单的。您使用
id
字段做什么?实际的要求不清楚(听起来很傻)。是否希望id包含列表中的位置?那么如果它被添加到另一个列表中呢?或者所有的
都应该有唯一的id吗?为什么?在
列表
周围做一个包装,并有一个方法
addDay
来满足您的需要可能是最简单的。您使用
id
字段做什么?实际的要求不清楚(听起来很愚蠢)。是否希望id包含列表中的位置?那么如果它被添加到另一个列表中呢?或者所有的
都应该有唯一的id吗?为什么?OP想要
在添加新元素时,而不是在构造对象时。我认为这可以简化为
这个。id=oldId++
,构造函数可能应该是
public
@AyushGupta,我不能使用构造函数,它的对象是POJO,没有constructor@All_Safe这是默认的构造函数,在添加新元素时调用它,而不是在构造对象时调用它。我认为这可以简化为
this.id=oldId++
,构造函数可能应该是
public
@AyushGupta,我不能使用构造函数,它的对象是POJO,没有constructor@All_Safe这是默认的构造函数,它无论如何都会被调用,即new AtomicInteger();如果我们想从0开始,那么getAndIncrement(),只需要新的AtomicInteger();如果我们想从0开始,那么getAndIncrement()。