Collection elements are much needed to have one-to-many, many-to-many, relationships, etc., Any one type from below can be used to declare the type of collection in the Persistent class. Persistent class from one of the following types:
java.util.List
java.util.Set
java.util.SortedSet
java.util.Map
java.util.SortedMap
java.util.Collection
Let us see where can we use List, Set, and Map by seeing the differences among them
List
Set
Map
Duplicates are allowed.
Duplicates are not allowed.
Duplicates are not allowed.
Insertion order is maintained.
Insertion order is not maintained.
Insertion order is not maintained.
Index element helps to identify an element in the list. Hence need to use this whenever the index is possible and it is mandatory.
For uniquely maintaining the collection of elements, we can go for the set.
As a key/value pair pattern means we can go for Map.
The Main Java file is similar to the one that is used for the list.
Major difference between List and Bag: In List, the index element is mandatory, and order is maintained. In Bag, there is no index element and hence no order.
SortedSet and SortedMap by the name itself we can come to know that if a set is sorted(ascending order by default)/a map is sorted(ascending order by default)