Java <bag> 和 <list> 之间的基本区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22808566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:03:50  来源:igfitidea点击:

basic difference between <bag> and <list>

javahibernate

提问by coreJavare

I was learning Hibernate, where collections are used in hibernate. I know that bag in collection is used for mapping property of type Collection or list. And also difference betweeen bag and list is bag is unordered with duplicate allowed collection type, and in list we maintain the insertion order in collection.
1> But apart from this is there any other difference between this two?
2> I read in one book that,

我正在学习 Hibernate,在 Hibernate 中使用集合。我知道集合中的包用于映射集合或列表类型的属性。bag 和 list 之间的区别还在于 bag 是无序的,并且允许重复的集合类型,并且在列表中我们维护集合中的插入顺序。
1> 但除此之外,这两者之间还有其他区别吗?
2> 我在一本书中读到,

bag is the lack of objects to be used as keys for the elements in the bag, which decreases performance when updating or deleting elements. When an element of the bag changes, Hibernate must update all of the elements since there is no way for Hibernate to find out which element has changed

bag 是包中缺少用作元素键的对象,这会降低更新或删除元素时的性能。当包中的元素发生变化时,Hibernate 必须更新所有元素,因为 Hibernate 无法找出哪个元素发生了变化

do any one have any idea about this?

有没有人对此有任何想法?

回答by Jakub H

Your definition is correct. Bag works like a list without index (you don't know what is the order of elements), so it's similar to Set with duplicates.

你的定义是正确的。Bag 就像一个没有索引的列表(你不知道元素的顺序是什么),所以它类似于带有重复项的 Set。

The most important thing is to know that Hibernate can map your collections as a bag implicitly if you don't use index column in one-to-many relation. This may decrease the performance of delete/update statements and it's good to be aware of this.

最重要的是要知道,如果您不在一对多关系中使用索引列,Hibernate 可以将您的集合隐式映射为包。这可能会降低删除/更新语句的性能,最好注意这一点。

Here you can find how it works internally: http://assarconsulting.blogspot.co.uk/2009/08/why-hibernate-does-delete-all-then-re.html

在这里你可以找到它的内部工作原理:http: //assarconsulting.blogspot.co.uk/2009/08/why-hibernate-does-delete-all-then-re.html

回答by myinterviewexp.com

When you don't want insertion order capability of list but want to allow duplicate values then you can go for bag. Here you can't go for set because it doesn't allow duplicate values.

当您不想要列表的插入顺序功能但又希望允许重复值时,您可以选择 bag。在这里你不能去 set 因为它不允许重复的值。

回答by arun rajput

a) BAG

一个包

Just want to add One more point. There are two types of bags 1)Bag without id's and 2) Bag with Id's.

只想补充一点。有两种类型的包 1) 没有 ID 的包和 2) 带 ID 的包。

In Bag without Id's when you remove any element, entire bag got cleared and the elements are inserted again.

在没有 Id 的包中,当您删除任何元素时,整个包被清除并再次插入元素。

But in bag with Id's, the element which has been removed only gets removed, rest of the elements are not impacted.

但是在带有 Id 的包中,已删除的元素只会被删除,其余元素不会受到影响。

@ElementCollection
    @CollectionTable(name = "account_user",joinColumns=@JoinColumn(name="user_id"))
    @CollectionId(columns = { @Column(name = "account_user_id") }, generator = "sequence", type = @Type(type = "long"))
    @Column(name = "account_provider")
    private Collection<String> accountSet = new ArrayList<String>();

So if you are using bag, always try to use id bags unless you have good reason to use another.

因此,如果您使用 bag,请务必尝试使用 id bag,除非您有充分的理由使用另一个。

b) ListList are also of two types, lists with order and without order.

b) ListList 也有两种类型,有顺序的和无序的。

Lists without order is similar to bag without ids.

没有顺序的列表类似于没有 id 的包。

@ElementCollection
        @CollectionTable(name = "account_user",joinColumns=@JoinColumn(name="user_id"))
        @Column(name = "account_provider")
        private List<String> accountSet = new ArrayList<String>();

While in list with order, the data structure maintains an indexing order. So, if you remove one of the elements. rest of the elements got shifted automatically.

在按顺序排列时,数据结构维护索引顺序。因此,如果您删除其中一个元素。其余元素自动移动。

Hence, this type of list is used to maintain the order in which the elements are inserted into the list.

因此,这种类型的列表用于维护元素插入列表的顺序。

@ElementCollection
    @OrderColumn(name="account_provider_order")
    @CollectionTable(name = "account_user",joinColumns=@JoinColumn(name="user_id"))
    @Column(name = "account_provider")
    private List<String> accountSet = new ArrayList<String>();

Also, note that although ordering is persisted in the seperate column of table. It doesn't appears on the object state, when you fetch. Hence, it is just used for internal operations.

另请注意,尽管排序保留在表的单独列中。当您获取时,它不会出现在对象状态上。因此,它仅用于内部操作。