What can we do by using sets? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What can we do by using sets?

Today I study Java and know a new datastruct named sets, so could you tell me the uses of it by some examples? I personally think we can also do it with Hashmap. For example, every time we put data, do: Hashmap<object, Integer> map = new Hashmap<>(); Integer value = map.get(data); if(value == null) map.put(data, 1); else System.out.println(“already exist”); So does Hashset have any advantages compared with Hashmap like speed, memory or any other advantages?

20th Jun 2018, 4:17 PM
movis
movis - avatar
7 Answers
+ 5
one of the benefit (HashSet)you seen in this code problem : https://code.sololearn.com/cdMTNP97UZML/?ref=app
21st Jun 2018, 6:38 AM
Nitish kumar jha
Nitish kumar jha - avatar
+ 3
Convenience! If you are using a Map like a Set, you should just use a Set. I mean, the language gives it to you for free. While you can use a Map as a Set*, it's just clumsy. And we can take your idea to the extreme: Instead of a Map, you can just use two arrays, one for the keys, one for the values. And instead of two arrays you can just use one array that is twice as long. So are there really any advantages of HashMap over a simple array? The performance of a HashMap and a HashSet are the same. "O(1)" for getting and "amortized O(1)" for setting, as you would say it in big O notation (read that as: "dang fast"). As I said, it's about picking the right tool for the job :P There's the saying that "if all you have is a hammer everything starts to look like a nail", and your hammer is definitely a HashMap! ___ * That's because the keys of a Map form a Set as I said above. In your code you don't really ever look at the values (they're all 1), and you only use the keys, which are a Set. Interesting!
21st Jun 2018, 12:16 PM
Schindlabua
Schindlabua - avatar
+ 1
A Set is more like a List and less like a Map. With a Map you have a key and a value type (in your example object and integer respectively), and a List is an ordered collection of a single type; you probably know of arrays or ArrayList, so yeah, that. The difference between a Set and a List is that in a Set, all the elements in it are unique. So for example you could make a List with a hundred elements that are all "4"; but a Set either contains "4" or it doesn't, and it never contains multiple "4"s. They are like mathematical sets if you are familiar with those. (Excercise: Convince yourself that all the keys of a HashMap behave like a Set! Your facebook friends are a set too, you can't friend one person twice.) So it's less about speed or memory footprint and more about picking the datastructure that fits the problem best! (Yes you can beat a HashMap into looking like a set, but in that case the set will outperform. Plus the set is just nicer to work with.)
20th Jun 2018, 6:28 PM
Schindlabua
Schindlabua - avatar
+ 1
nitish kumar jha thanks and I try to make it with hashmap. https://code.sololearn.com/c8uauhwSefH4/?ref=app So compared with hashmap, where are advantages of hashset?
21st Jun 2018, 11:22 AM
movis
movis - avatar
+ 1
Schindlabua Amazing! Really help me a lot and thanks for your enthusiasm.
21st Jun 2018, 12:59 PM
movis
movis - avatar
+ 1
movis good use of concept
22nd Jun 2018, 4:03 AM
Nitish kumar jha
Nitish kumar jha - avatar
0
Schindlabua thanks for the help:)
21st Jun 2018, 3:32 AM
movis
movis - avatar