Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

OCP Java SE 11 Programmer II Study Guide (Sybex 816) Question Chapter 7 (Concurrency)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the section: "Working with Concurrent Classes" we have a - TABLE 7.9 "Concurrent collection classes".

In this table we have two columns "Elements ordered?" and "Sorted?" what is their meaning?

I tried some searching using Google to see the difference between sorted and ordered collections and found these:

http://www.javapractices.com/topic/TopicAction.do?Id=65

and

https://stackoverflow.com/questions/1084146/what-is-the-difference-between-an-ordered-and-a-sorted-collection#:~:text=Java%20uses%20%22ordered%20collection%22%20to,%22place%22%20in%20the%20order.

There many explanations one of it is:

'For convenience, "ordering" will here refer to the order of items returned by an Iterator, and "sorting" will here refer to sorting items according to Comparable or Comparator.'

Executed the following code:



The output is:

gopher,rabbit
Ordering attempt 1.
gopher
rabbit
Ordering attempt 2.
gopher
rabbit

According to this code and the definition I wrote - the ConcurrentSkipListSet is sorted and not ordered.
In the table it is written it is sorted and ordered.

Is my understanding wrong or there is a mistake/error in the table?




 
Marshal
Posts: 72974
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Borislav Mirchev wrote:. . .
'For convenience, "ordering" will here refer to the order of items returned by an Iterator, and "sorting" will here refer to sorting items according to Comparable or Comparator.'
. . .

That quote isn't quite accurate. And that SO thread shows how much confusion there is about ordering.
All collections have an order, which is how their element are returned from an Iterator. What are commonly called ordered collections are those where the iteration order is related to the insertion order. Lists, most Queues, and Stacks behave like that, but some of them are ordered backwards. It is also often possible to alter the iteration order; a List can be sorted or shuffled, for example.
Some collections iterate their elements in order of some feature of those elements (getting larger or getting smaller) and those are usually called sorted. A tree set automatically sorts its elements as they are inserted.
Some collections can be iterated without the order being easily predictable. A hash set returns its elements in some sort of order to do with their hash codes, and that order might change if the collection becomes larger. These are often called unordered, but that term isn't accurate.

The ConcurrentSkipListSet documentation says

. . . The elements of the set are kept sorted . . .

Please show us the table if possible; you are probably right that you have found an error.
 
Borislav Mirchev
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for the detailed explanation! The table is the attached file TheTable2.jpg. Excuse me for the bad quality. The values in red were suspicious at the time of my first post.
TheTable2.jpg
The table
The table
 
Campbell Ritchie
Marshal
Posts: 72974
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the book's errata page and the link I gave you earler; I still think you are correct.
 
Borislav Mirchev
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
https://www.selikoff.net/ocp11-2/ it is the book errata page. The issue I've found is not mentioned there. The documentation says the iterator() method returns the elements in ascending order, not in the order of insertion. And if I'm properly understanding the terminology ConcurrentSkipListSet is not an ordered collection as mentioned in the TABLE 7.9.

The exact text that can be put in Errata is:

Chapter 7, in Table 7.9 for ConcurrentSkipListSet in the column: Elements ordered? is written Yes.

It should be:

Chapter 7, in Table 7.9  for ConcurrentSkipListSet in the column: Element ordered? the correct value is No.

or something similar.

It is in case an ordered collection is meant that where iterator() returns the elements in order of addition.

In case iterator() returns the element in any possible order or order of insertion for CopyOnWriteArraySet in Java 11 doc is written: "Returns an iterator over the elements contained in this set in the order in which these elements were added."
while in the table in the column: Element ordered?  is written "No" while it should be "Yes".

Probably this table needs an additional review and/or explanation of the Element ordered?'s meaning.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic