Using hashsets with Java

So I’m experimenting with this code

Using the compiler on

I’m just trying to add these strings to a set then print them however I get this error

/MyClass.java:4: error: cannot find symbol
	Set< String > set = new HashSet<>();
	^
  symbol:   class Set
  location: class MyClass
/MyClass.java:4: error: cannot find symbol
	Set< String > set = new HashSet<>();
	                        ^
  symbol:   class HashSet
  location: class MyClass
2 errors

What’s going on? How do I fix this?

is this all your code?
it may be better to link to a public space where we can see your full code.
(do you think you might be missing an import statement?)

Yeah that’s it. This is the exact code I have on the JDoodle editor.

import java.util.*;

add that to the top

1 Like

It worked! Thanks a ton. I’m new to Java, what does this line do exactly? What is it importing?

ah! Well, if you are new to java, you may want to learn some fundamentals about java libraries and how to import them.

here’s some resources you can look into

Also, the code returns

a
x
z

I understand hash sets (or just sets?) cannot contain duplicates. Although, why in this order? Should it not be Apple, banana, cherry? Why is banana first?

this would be because java Hashsets do not guarantee the order of the data.

1 Like

So would it be accurate to say that hash set order is randomized?

not random. Just no promise that the order is maintained.