简体   繁体   English

如何保证集合中项目的顺序

[英]How do I guarantee the order of items in a collection

I have a list of objects and each and every object in the list have a position which may not change unless explicitly changed, in other words, the objects are in a queue. 我有一个对象列表,列表中的每个对象都有一个位置,除非明确更改,否则位置不得改变,换句话说,对象在队列中。 What collection should I use in my entity class to store these objects and how should that collection be annotated? 我应该在实体类中使用哪个集合来存储这些对象,以及该集合应该如何注释?

I currently have this 我目前有这个

@Entity
class Foo {
  ...
  @OneToMany(mappedBy = "foo", cascade = CascadeType.ALL)
  List<Bar> bars = new ArrayList<Bar>();
  ...
}

If this is not possible with JPA purely, I'm using EclipseLink as my JPA provider, so EclipseLink specific annotations will ok if nothing else helps. 如果仅使用JPA无法做到这一点,那么我将EclipseLink用作我的JPA提供程序,因此,如果没有其他帮助,则EclipseLink特定的批注就可以了。

EDIT: Note people, the problem is not that Java wouldn't preserv the order, I know most collections do, the problem is that I don't know a smart way for JPA to preserv the order. 编辑:注意人们,问题不是Java不会保留订单,我知道大多数集合都可以,问题是我不知道JPA保留订单的明智方法。 Having an order id and making the query order by it is possible, but in my case maintaining the order id is laborious (because the user interface allows reordering of items) and I'm looking for a smarter way to do it. 拥有订单ID并按查询顺序排序是可能的,但是在我的情况下,维护订单ID十分费力(因为用户界面允许对商品进行重新排序),我正在寻找一种更智能的方式。

If you want this to be ordered after round-tripping to SQL, you should provide some sort of ordering ID within the entity itself - SQL is naturally set-oriented rather than list-oriented. 如果希望在往返SQL之后进行排序,则应在实体本身内提供某种排序ID-SQL自然是面向集合的,而不是面向列表的。 You can then either sort after you fetch them back, or make sure your query specifies the ordering too. 然后,您可以在取回它们之后进行排序,或者确保查询也指定了顺序。

If you give the entity an auto-generated integer ID this might work, but I wouldn't like to guarantee it. 如果为实体提供一个自动生成的整数ID,则此方法可能会起作用,但我不希望对此予以保证。

Use a sort order id, as Jon suggested, then add an @OrderBy annotation below the @OneToMany. 使用乔恩建议的排序顺序ID,然后在@OneToMany下添加@OrderBy批注。 This will order any query by the specified field. 这将按指定的字段对任何查询进行排序。

As an example, if you add a new field called "sortId" to Bar, Foo would look like this: 例如,如果您向Bar添加一个名为“ sortId”的新字段,Foo将如下所示:

@Entity
class Foo {
  ...
  @OneToMany(mappedBy = "foo", cascade = CascadeType.ALL)
  @OrderBy("sortId ASC")
  List bars = new ArrayList();
  ...
}

You can 您可以

  1. Sort a List before creation 创建之前对列表进行排序
  2. Sort a List after creation 创建后对列表进行排序
  3. Use a collection that performs a sort on insert. 使用对插入执行排序的集合。 TreeMap, TreeSet TreeMap,TreeSet

A linked list implements the Queue inteface in java and allows you to add things in the middle... 链表在Java中实现了Queue接口,并允许您在中间添加内容。

TBH most of the collections are ordered aren't they... TBH的大多数藏品都是订购的,不是吗?

Check the docs, most say whether or not they are ordered. 检查文档,大多数人说是否已订购。

It's worth trying LinkedList instead of ArrayList, however, as Jon said, you need to find a way of persisting the order information. 值得尝试使用LinkedList而不是ArrayList,但是,正如Jon所说,您需要找到一种保留订单信息的方法。

A solution will probably involve issuing an order number to each entry an storing it as a SortedMap, when converting into the List, if List is that you need. 一个解决方案可能涉及到向每个条目发布订单号,如果您需要列表,则在转换为列表时将其存储为SortedMap。

However, ORM could potentially be clever enough to do all the conversions for you if you stored the collection as LinkedList. 但是,如果将集合存储为LinkedList,则ORM可能足够聪明,可以为您进行所有转换。

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

相关问题 如何保证Java集合 - How to guarantee Java collection 我如何保证单次会话? - How do I guarantee a single session? 如何按特定的订单长度顺序列出字符串项目? - How do I list string items in a specific order length order? 给定集合名称(ArrayList、LinkedList 等)和集合中的项目,如何创建任何集合? - How do I create any Collection given the collection name(ArrayList, LinkedList etc) and items in the collection? 如何在Java中更改数组列表中项目的顺序 - How do I change the order of the items in an arraylist in Java 在存在线程的情况下,如何保证 object 的完整构造 - How do I guarantee the complete construction of an object in the presence of threads 如何保证两个微服务之间的接口不中断? - How do I guarantee that the interface between two microservices is not broken? 我如何保证我的Android SurfaceView是透明的而不是黑色的? - How do I guarantee that my Android SurfaceView is transparent instead of black? 如何根据ArrayList中的顺序对ArrayList中的项目进行排序并更改项目在另一个数组中的位置? - How do I sort the items in the ArrayList and change the places of items in another array depending on order in the ArrayList? 如何保证ThreadPoolExecutor中的FIFO执行顺序 - How to guarantee FIFO execution order in a ThreadPoolExecutor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM