Java最佳实践,在对象修改之前还是之后添加到集合?
假设您要将 x 个对象添加到集合中,并且在将它们添加到集合之后或之前,您要修改对象属性。在对象修改之前或之后何时将元素添加到集合中。
选项 A)
public static void addToCollection(List<MyObject> objects) {
MyObject newObject = new MyObject();
objects.add(newObject);
newObject.setMyAttr("ok");
}
选项 B)
public static void addToCollection(List<MyObject> objects) {
MyObject newObject = new MyObject();
newObject.setMyAttr("ok");
objects.add(newObject);
}
Say you are adding x number of objects to a collection, and after or before adding them to a collection you are modifying the objects attributes. When would you add the element to the collection before or after the object has been modified.
Option A)
public static void addToCollection(List<MyObject> objects) {
MyObject newObject = new MyObject();
objects.add(newObject);
newObject.setMyAttr("ok");
}
Option B)
public static void addToCollection(List<MyObject> objects) {
MyObject newObject = new MyObject();
newObject.setMyAttr("ok");
objects.add(newObject);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
为了安全起见,您应该在添加之前进行修改,除非有特定原因您不能这样做,并且您知道集合可以处理修改。可以合理地假设该示例是安全的,因为一般
List
契约不依赖于对象属性 - 但这没有说明具体实现,具体实现可能具有取决于对象值的附加行为。TreeSet 和 Map 通常不允许在插入对象后对其进行修改,因为集合的结构取决于对象的属性。对于树来说,一旦添加了项目,比较器使用的任何属性就无法更改。对于映射,hashCode 必须保持不变。
所以,一般情况下,先修改,再添加。这对于并发集合变得更加重要,因为首先添加可能会导致其他集合用户在分配其最终状态之前看到对象。
To be on the safe side, you should modify before adding, unless there is a specific reason you cannot do this, and you know the collection can handle the modification. The example can reasonably be assumed to be safe, since the general
List
contract does not depend upon object attributes - but that says nothing about specific implementations, which may have additional behavior that depends upon the object's value.TreeSet, and Maps in general do no tolerate modifying objects after they have been inserted, because the structure of the collection is dependent upon the attributes of the object. For trees, any attributes used by the comparator cannot be changed once the item has been added. For maps, it's the hashCode that must remain constant.
So, in general, modify first, and then add. This becomes even more important with concurrent collections, since adding first can lead to other collection users seeing an object before it been assigned it's final state.
您提供的示例不会有任何问题,因为您使用的是不关心对象内容的列表集合。
如果您使用像 TreeMap 这样的东西,它在内部对其存储的对象键的内容进行排序,则可能会导致 Collection 进入意外状态。同样,这取决于 equals 方法是否使用您要更改的属性进行比较。
最安全的方法是在将对象放入集合之前对其进行修改。
The example you provided won't have any issues because you're using a List collection which doesn't care about the Object contents.
If you were using something like TreeMap which internally sorts the contents of the Object keys it stores it could cause the Collection to get into an unexpected state. Again this depends on if the equals method uses the attribute you're changing to compare.
The safest way is to modify the object before placing it into the collection.
要遵循的良好设计规则之一是不要将半构造对象暴露给第 3 方子系统。
因此,根据此规则,尽最大努力初始化您的对象,然后将其添加到列表中。
如果
objects
是ArrayList
那么最终结果可能是相同的,但是如果objects
是List
的特殊风格,那么最终结果可能是相同的code> 每次添加新对象时都会触发某种通知事件,那么顺序将非常重要。One of the good design rules to follow, is not to expose half-constructed object to a 3rd party subsystem.
So, according to this rule, initialize your object to the best of your abilities and then add it to the list.
If
objects
is anArrayList
then the net result is probably the same, however imaging ifobjects
is a special flavor ofList
that fires some kind of notification event every time a new object is added to it, then the order will matter greatly.在我看来,它取决于集合的设置属性和类型,如果集合是一个 Set 并且该属性对 equal 或 hascode 方法有影响,那么我肯定会在此之前设置此属性,在其他情况下也请参阅 sorterd 列表等这是无关紧要的。但对于创建对象的示例,我将首先设置属性而不是添加到集合,因为代码组织得更好。
In my opinion its depend of the settted attribure and tyle of collection, if the collection is a Set and the attribute have infulance on the method equal or hascode then definitely i will set this property before this refer also to sorterd list etc. in other cases this is irrelevant. But for this exapmle where object is created i will first set the atributes than add to collection because the code is better organized.
我认为无论哪种方式都是一样的,我个人喜欢B,:)
I think either way it's the same, personally I like B, :)
这确实归结为情况的需要。功能上没有区别。
您应该小心的一件事是确保您拥有要修改的对象的正确句柄。
It really does boil down to what the situation requires. Functionally there's no difference.
One thing you should be careful with, is being sure you have the correct handle to the object you want to modify.
当然,在这种情况下,修改对象是“创建对象”思想的一部分,因此应该与构造函数分组。 “创建对象”后,您“将其添加到集合中”。因此,我会做B,甚至可能在修改后添加一个空行,以更加强调这两个独立的想法。
Certainly in this instance, modifying the object is part of the "create the object" thought, and so should be grouped with the constructor as such. After you "create the object" you "add it to the collection". Thus, I would do B, and maybe even add a blank line after the modification to give more emphasis on the two separate thoughts.