2

I have no any special ideas to have my collection ordered or not to allow duplicates in the collection. In my entity class what should I use ? Set or a List? Is there any consideration in performance wise ?

Lets say we want to perform CRUD operations on this entity . I don't want to have the results ordered . So what should I use ? List or Set in the Entity. What should be user in order to maximize performance

What are the valid cases of using List and Set in an entity considering performance ? Not for ordering or to restrict duplicates?

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50
  • 2
    I think this one is too broad. The answer is generally "It depends on.". Lists and Sets have both valid use cases. – Viktor Seifert Jul 09 '13 at 08:13
  • 1
    check this http://stackoverflow.com/questions/4655392/which-java-type-do-you-use-for-jpa-collections-and-why and this http://stackoverflow.com/questions/13101882/jpa-onetomany-list-vs-set – vels4j Jul 09 '13 at 08:19
  • @vels4j sorry vels. The first link does not discuss the performance wise and I know that list for ordering and set for allow duplicates. Second link does not have an answer – Sanjaya Liyanage Jul 09 '13 at 08:27
  • @ViktorSeifert yes I accept that this seems broad. But a detailed answer will be handy If someone can address wider range concerns – Sanjaya Liyanage Jul 09 '13 at 08:28

2 Answers2

3

As mentioned in another post given in the links in comment by vels4j, the following is definitely true.

List: Allows duplicate elements in it.

Set: All elements should be unique.

If you talk about performance, one point that I'm aware of is as follows:

If you take a List & map it to a table using Hiberante, you need to add an extra column as the index. This column will work as position/index/order of the element in the List because List is an ordered collection. See this for more details on index.

In case of Set no such column is required.

Now take an example where you need to remove an element from the List/Set.

In case of List, after removing element, you need to update all other elements of the List to update their index. This is an overhead.

While in case of Set, as you don't have index column, you need not to worry about other elements' index.

So my suggestion is: If you don't need your collection to be in a particular order while fetching it from the database, you should go for Set.

RAS
  • 8,100
  • 16
  • 64
  • 86
  • 1
    thanks Ras ,I also feel that way .Anyway I wanted to confirm it from an expert. here is my upvote for you. I'll wait more to accept an answer. – Sanjaya Liyanage Jul 09 '13 at 09:35
  • @SanjayaLiyanage sure. Even I'm looking for some more useful answers. This is just one point that I'm aware of. – RAS Jul 09 '13 at 09:38
0

Find an element by value in a HashSet is O(1). In an ArrayList, it's O(n). But dictionary structure HashSet is probably a bit slower than an ArrayList. And ArrayLists are faster at random access.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115