Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a Set Data Structure in Java?

I have always wondered how would you implement a Set in Java. Can we implement it just like we implement a HashMap using a LinkedList and an object(Cell) which holds a Key and Value? How would you handle the uniqueness part?

like image 405
ArsenalRocks Avatar asked Mar 19 '15 04:03

ArsenalRocks


People also ask

Which data structure is used to implement set in Java?

The set interface is present in java. util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set.

How are sets implemented in Java?

The Java platform contains three general-purpose Set implementations: HashSet , TreeSet , and LinkedHashSet . HashSet , which stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration.

What is a set data structure Java?

Set is a data structure that is used as a collection of objects. Java supports it through its Java Collection library. There are several ways to implement a set and Java supports three different implementations along with useful operations like intersection between sets.

What is the implementation of set?

There are three general-purpose Set implementations — HashSet , TreeSet , and LinkedHashSet . Which of these three to use is generally straightforward. HashSet is much faster than TreeSet (constant-time versus log-time for most operations) but offers no ordering guarantees.


1 Answers

Set internally implements a map.So each value in a set is just a key in map.So its uniqueness in maintained.

Here is the link.So that you get clear idea how set works internally. Also few stack Answers. First , Second

like image 171
rns Avatar answered Nov 09 '22 14:11

rns