Piano Guidance
Photo by Los Muertos Crew Pexels Logo Photo: Los Muertos Crew

Why is a HashSet faster than an array?

HashSet is designed to have expected constant time add , contains and remove operations, meaning that the time won't change much regardless of how many elements are in the set. Arrays have linear operations for all of these, but lower overhead.

Is Grade 8 piano good?
Is Grade 8 piano good?

Grade 8 piano is a momentous achievement for many who have committed years of practise to the instrument, and achieving a Distinction at Grade 8 is...

Read More »
Why do pianists sit at the edge of the chair?
Why do pianists sit at the edge of the chair?

Pianists usually sit at the edge of the piano bench to allow their legs to comfortably use the pedals. Sitting at the edge of the piano bench...

Read More »

The choice greatly depends on what do you want to do with it.

If it is what mentioned in your question:

I have a collection of objects that are guaranteed to be distinct (in particular, indexed by a unique integer ID). I also know exactly how many of them there are If this is what you need to do, the you need neither of them. There is a size() method in Collection for which you can get the size of it, which mean how many of them there are in the collection. If what you mean for "collection of object" is not really a collection, and you need to choose a type of collection to store your objects for further processing, then you need to know, for different kind of collections, there are different capabilities and characteristic. First, I believe to have a fair comparison, you should consider using ArrayList instead Array, for which you don't need to deal with the reallocation. Then it become the choice of ArrayList vs HashSet, which is quite straight-forward: Do you need a List or Set? They are for different purpose: Lists provide you indexed access, and iteration is in order of index. While Sets are mainly for you to keep a distinct set of data, and given its nature, you won't have indexed access. After you made your decision of List or Set to use, then it is a choice of List/Set implementation, normally for Lists, you choose from ArrayList and LinkedList, while for Sets, you choose between HashSet and TreeSet. All the choice depends on what you would want to do with that collection of data. They performs differently on different action. For example, an indexed access in ArrayList is O(1), in HashSet (though not meaningful) is O(n), (just for your interest, in LinkedList is O(n), in TreeSet is O(nlogn) ) For adding new element, both ArrayList and HashSet is O(1) operation. Inserting in the middle is O(n) for ArrayList, while it doesn't make sense in HashSet. Both will suffer from reallocation, and both of them need O(n) for the reallocation (HashSet is normally slower in reallocation, because it involve calculation of hash for each element again). To find if certain element exists in the collection, ArrayList is O(n) and HashSet is O(1). There are still lots of operations you can do, so it is quite meaningless to discuss for performance without knowing what you want to do.

Is Moonlight Sonata difficult?
Is Moonlight Sonata difficult?

Moonlight Sonata is not so easy to play, and it holds such depths of emotions that beginners can never replicate. The piece requires a lot of hand...

Read More »
Can you shift without letting off the gas?
Can you shift without letting off the gas?

What is Power shifting and how is it done? Power shifting is when you change gears in a manual transmission equipped car at full throttle, without...

Read More »

Which map is best in Java?

If you want to store your data into a map in a general purpose then a HashMap is a good option to choose. The HashMap provides rapid storage and retrieval operations. It is chaotic in nature because it stores unorderly arrangement of entries.

About The Author

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Do old pianos have gold in them?
Do old pianos have gold in them?

Despite all the glitter, those two instruments very rarely contain quantities of precious metals that can be extracted by a qualified gold...

Read More »
Can jazz be taught?
Can jazz be taught?

Jazz is traditionally learned by ear. You can learn repertoire and solos from sheet music, but that's not how jazz has been passed on through the...

Read More »
Which country suffered the most in ww2?
Which country suffered the most in ww2?

Officially, roughly 8.6 million Soviet soldiers died in the course of the war, including millions of POWs.

Read More »
Who did drop D tuning first?
Who did drop D tuning first?

Early hard rock songs tuned in drop D include The Beatles' "I Want You (She's So Heavy)" and Led Zeppelin's "Moby Dick", both first released in...

Read More »