Set type accepts no parameters
package set;
import java.util.*;
public class Set
{
public static void main(String[] args)
{
String [] things = {"appple", "bob", "ham", "bob", "bacon"};
List<String> list = Arrays.asList(things);
System.out.printf("%s ", list);
System.out.println();
Set<String> set = new HashSet<String>(list);
System.out.printf("%s ", set);
}
}
When I try to run this program, I keep getting this error for my set declaration. What am I doing wrong?
source to share
Rename the class public class Set
to a name that does not hide java.util.Set
.
Your own class Set
doesn't take any type parameters. Therefore Set<String>
does not commit compilation.
For example:
package set;
import java.util.*;
public class SetTest
{
public static void main(String[] args)
{
String [] things = {"appple", "bob", "ham", "bob", "bacon"};
List<String> list = Arrays.asList(things);
System.out.printf("%s ", list);
System.out.println();
Set<String> set = new HashSet<String>(list);
System.out.printf("%s ", set);
}
}
source to share
You have hidden the import java.util.Set
by naming your own local class Set
( set.Set
has higher visibility and is not generic). You can rename set.Set
or use the full name (and diamond operator <>
). Change
Set<String> set = new HashSet<String>(list);
to something like
java.util.Set<String> set = new HashSet<>(list);
source to share
Although it is not a good idea to specify classes that are already present in the Java API, etc.
Adding this will be enough
java.util.Set<String> set = new HashSet<String>(list);
However, you have made some changes to the existing
package set;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
public class Set {
public static void main(String[] args) {
String[] things = { "appple", "bob", "ham", "bob", "bacon" };
List<String> list = Arrays.asList(things);
System.out.printf("%s ", list);
System.out.println();
java.util.Set<String> set = new HashSet<String>(list);
System.out.printf("%s ", set);
}
}
source to share