CopyOnWriteArraySet<Integer>() usage

hi,

I wonder when and how we use below class

new CopyOnWriteArraySet<Integer>();

Is it new addition to java. Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
The CopyOnWriteArray classes (List, Set etc.) are all for use in concurrent environments where you have multiple threads accessing a shared data structure.

The idea behind these is that when they are modified, the class creates a new copy of the data.
When you read from them, you always receive that copy, so the set of data isn't changing while your client is using it.

E.g. Say you had a set of people who are currently online at experts exchange.  Obviously that set is changing from one minute to the next, so if you want to dump out the list of all people currently online, it's best to take a snapshot of the current group of people and dump that out.  This class does all of that for you automatically.

Doug
0
gudii9Author Commented:
any good sample working example on this concept. please advise
0
dpearsonCommented:
There's lots more examples for CopyOnwriteArrayList since that's used a lot more than CopyOnWriteArraySet (people generally want to use Maps, not Sets and for a Map you use ConcurrentHashMap).

Here's an example explaining the CopyOnwriteArrayList behavior.  It's the same concepts that apply to the Set version:
http://javacodeimpl.blogspot.com/2014/04/copyonwritearraylist-in-java-example.html#.VXXrcs9ViTE

Doug
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

gudii9Author Commented:
i cannot access this url here. let me try from home
0
gudii9Author Commented:
There's lots more examples for CopyOnwriteArrayList since that's used a lot more than CopyOnWriteArraySet (people generally want to use Maps, not Sets and for a Map you use ConcurrentHashMap).

people generally want to use List or Set or Map more? I am not clear on this. please advise
0
dpearsonCommented:
You will find a lot of code that uses Lists and Maps.

Set is much less commonly used.

That's because people either want to store a list of things in order (use a List)
or they want a fast way to lookup information from some key (use a Map).

A set isn't good for either of those.  It's mostly used when you want a collection of things and you need to make sure there aren't any duplicates in the collection.  There are times when you want that, but it's just less common.

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.