• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to verify if a data structure could perform efficiently with parallel stream

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm reading OCP study guide (OCP_ Oracle Certified Professional Java SE 8 Programmer II Study Guide), in chapter 7, in a part talking about Using the One-Argument collect() Method, it is said that in order for reduction to be performed effeciently these requirments need to be fulfilled :
■■ The stream is parallel.
■■ The parameter of the collect operation has the Collector.Characteristics.CONCURRENT characteristic.
■■ Either the stream is unordered, or the collector has the characteristic Collector.Characteristics.UNORDERED.
So I wonder how to verify if a data structure fulfill the last 2 rules, for example I tried for the ConcurrentHashMap that is said to fulfill them but I didn't find any thing
 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The data structure doesn't fulfill the second requirement, the collector does. You can check the Collector.characteristics() method for the CONCURRENT and the UNORDERED characteristics.

Usually you create a stream from a data source yourself, and you already know if the data source has an intrinsic order or not. Lists are ordered. Sets are unordered, except if they're a subtype of SortedSet.
 
yas sine
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you provide an example please.
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An example of what?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:. . . Lists are ordered. . . . .

. . . but it is awkward to make parallel Streams from some Lists. In order to get elements 1000...2000exc from a linked list, you have to iterate it to elements 1000 and 1999, but in the case of an array list it is very easy to find those indices with the get(int) method. So parallel Streams created from an array list tend to give much faster performance than those created from a linked list.

Does that sort of question feature in the OCP exam?
 
Stephan van Hulst
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only if you implement the stream by accessing the elements using the get() method. In reality, the stream is likely implemented with a Spliterator, which is precisely designed to solve these problems efficiently.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic