简体   繁体   English

为什么在Ebean中使用java.util.List而不是java.util.Set

[英]Why use java.util.List instead of java.util.Set for relations in Ebean

I see that many examples use List for "many" relations, however, Set seems to better fulfil the role since lookup can happen in O(1) while the list search is O(N/2). 我看到许多例子使用List来表示“很多”关系,但是,Set似乎更好地履行了这个角色,因为查找可以在O(1)中发生,而列表搜索是O(N / 2)。 Is there any reason why List is the preferred type to use? 是否有任何理由使用List作为首选类型?

只有在ManyToMany关系的情况下,重复才是重要的,在其他情况下,ORM也会处理List中的重复。

It depends on the situation you use. 这取决于您使用的情况。

  1. Set contain no dublicates while you can hold dublicates in List. Set中不包含dublicates而您可以在List中持有dublicates。
  2. List stores items in the inserted order. 列表以插入的顺序存储项目。

List and Set are semantically different. ListSet在语义上是不同的。 Only looking at the lookup times is a bad idea and can lead to serious problems further down the line. 只查看查找时间是一个坏主意,可能会导致严重的问题。

List can have duplicates, Set cannot. List可以有重复, Set不能。
List also guarantees access by index, which Set s do not. List还保证通过索引进行访问,而Set则没有。 (Although some Set implementations of course might.) (虽然有些Set实现当然可能。)

Check yourself, Ebean's authors answered it already with details in article: 检查自己,Ebean的作者已在文章中详细说明了这一点:

List Set Map or Collection? 列表集地图或集合?

You'll find there a sumarized description of each type and also comparison between chosen types. 您将找到每种类型的sumarized描述以及所选类型之间的比较。

我一直认为List保留了对底层数据库的查询结果的任何顺序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM