0
Plzz help which type of error is this???
4 Answers
+ 2
There is nothing wrong with the code.
Just import Set and HashSet.
import java.util.Set;
import java.util.HashSet;
+ 2
Izac Espinoza
For clarification, there actually is nothing wrong with using;
Set<Integer> values = new HashSet<Integer>();
and is usually the preferred way with collection types. It's a typical use of polymorphism. The only issue(s) I see from a glance at the code is the missing imports.
0
So pretty much, you did'nt have the HASHSET object imported at the top of your code, and you declared the Hashset slightly wrong, here's the fixed code I hope it works for you :)
/*Without this import, the program doesn't know what a hashset is, this just brings it into the picture so you can use it, "import java.util.*;" imports all java stuff you might need, so you don't have to do a bunch of import statements. */
import java.util.*;
public class ShowCollection
{
public static void main(String[] args) {
/*Here is where you messed up , you had declared the hashset like this:
Set<Integer>values, the correct way is below */
HashSet<Integer>values=new HashSet<Integer>();
values.add(1);
values.add(2);
values.add(3);
values.add(4);
for(int x:values)
{
System.out.println(x);
}
}
}