When use sorting, Hibernate will load the associated Book entities from the database and use a Java Comparator to sort them in memory. That is not a good approach for huge Sets of entities. Ordering uses an ORDER BY clause in the SQL statement to retrieve the entities in the defined order.
Among the applicable definitions found were the following: SORT: To arrange (things, etc.) according to a kind or quality, or after some settled order or system; to separate and put into different sorts or classes. ORDER: The action of putting or keeping in order.
An ordered collection represents a group of objects, known as its elements. Ordered collections allow duplicate elements. This interface is typically used to pass ordered collections around and manipulate them where maximum generality is desired.
- The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator. - The efficiency depends on the size of the collection. - Ordered collection is sorted by specifying the order-by clause for sorting this collection when retrieval.
An ordered collection means that the elements of the collection have a specific order. The order is independent of the value. A List is an example.
A sorted collection means that not only does the collection have order, but the order depends on the value of the element. A SortedSet is an example.
In contrast, a collection without any order can maintain the elements in any order. A Set is an example.
An ordered collection maintains the order of the elements based on the sequence you put stuff into/remove them from the collection.
A sorted collection keeps the elements sorted based on a sort criteria.
Java uses "ordered collection" to mean a collection such as List, where (unlike HashSet), the collection remembers what order the elements are supposed to be in. So elements can be added to the collection at a particular "place" in the order.
Java uses "sorted collection" to mean a collection such as SortedSet, where (unlike List), the order that the iterator traverses the collection is in accordance with a specified Comparator or the natural order of the elements.
So the difference is whether the ordering depends on the values ("sorted"), or is a property that elements have independently of their value ("ordered").
Yes, though the concepts are similar.
List
is an ordered collection: each element has an index, which forms an ordering of the elements, but not usually related to any property of the elements themselves.
SortedMap
and SortedSet
are sorted collections, which means that iteration through the collection will happen in a sequence derived from the elements themselves. For example, if you have a SortedSet<String>
then the Strings will be sorted according to the lexicographical sort order.
An ordered Collection can be sorted but doesn't have to be (e.g. after using Collections.sort()
) when the external ordering is identical with the elements' sort order. A sorted collection is always implicitly ordered (i.e. there is always a "first" element, and it's always the same as long as you don't add another, smaller one).
An ordered collection is a collection that keep track of a consecutive index which every element is inserted in.
A sorted collection is an ordered collection when the order additionally depends on the value of the element to be inserted in, throughout the use of the Comparable interface which provides you with a method to define the sorting criteria.
I hope it could help.
Sorted would imply ordering according to an implementation of Comparable or Comparator. Ordered would imply that it is following the insertion order or some other definition of order that is consistent and defined, but otherwise arbitrary.
So a sorted list of strings would be sorted according to the String.compareTo method. A list might contain a list of strings inserted in arbitrary order, but that order will always remain the same.
Of course there are methods on the Collections class to sort a list.
A sorted collection usually mean the elements are sorted from minimun value to maxinum value or vice versa depending on the attribute(s) of the elements on which algorithms work.
for a interger collections, the sorted may be from min number to max number for a person collection, it may be sored by the height of persons or the weight of persons, etc.
When talking about order, it usually means the order of insertion. The order may be changed after sorting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With