HashSet应用时的警告,如何去除?

中才德创 2014-06-25 03:24:29
Set<Integer> set = new HashSet();报的警,如下:

Multiple markers at this line
- Type safety: The expression of type HashSet needs unchecked conversion to conform to
Set<Integer>
- HashSet is a raw type. References to generic type HashSet<E> should be parameterized

Multiple markers at this line
- HashSet is a raw type. References to generic type HashSet<E> should be parameterized
- Type safety: The expression of type HashSet needs unchecked conversion to conform to
Set<String>


import java.util.HashSet;
import java.util.Set;

class FindSame {
public static void main(String args[]) {

System.out.println("This is a simple java program");

int arrayInt[] = {1, 2, 1, 3, 4, 6, 7};
checkSameIntItems(arrayInt);

String arrayStr[] = {"1", "2", "1", "3", "4", "6", "7"};
checkSameStrItems(arrayStr);
}

private static void checkSameIntItems(int[] array) {
Set<Integer> set = new HashSet();
boolean bCheckSame = false;

for (int i=0; i<array.length; i++) {
if (!set.add(array[i])) {
bCheckSame = true;
}
}

System.out.println("The hash items:");
for (Object o : set) {
System.out.println(o);
}

if (bCheckSame) {
System.out.println("The array has same item");
}
else {
System.out.println("The array has not same item");
}
}

private static void checkSameStrItems(String[] array) {
Set<String> set = new HashSet();
boolean bCheckSame = false;

for (int i=0; i<array.length; i++) {
if (!set.add(array[i])) {
bCheckSame = true;
}
}

System.out.println("The hash items:");
for (Object o : set) {
System.out.println(o);
}

if (bCheckSame) {
System.out.println("The array has same item");
}
else {
System.out.println("The array has not same item");
}
}
}
...全文
778 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zy_think123 2014-06-27
  • 打赏
  • 举报
回复
看你的样子是打算判断集合中的元素是否相等了,没有这么麻烦,如果你只是想要解决这个问题的话,那么他们的提示完全够了, 但是看到你这儿我觉得可以优化一下,你用:

Set<String> set=new TreeSet<String>();
这样存储的元素就不会有重复的了,没有必要写的这么复杂
vnvlyp 2014-06-27
  • 打赏
  • 举报
回复
Set<Integer> set = new HashSet<>(); Set<String> set = new HashSet<>(); JDK7以后可以自动匹配,但<>不能少
中才德创 2014-06-26
  • 打赏
  • 举报
回复
引用 3 楼 maihao110 的回复:
Set<Integer> set = new HashSet<Integer>(); Set<String> set = new HashSet<String>();
确实应该如此!
qq_16322383 2014-06-26
  • 打赏
  • 举报
回复
泛型讲究精确匹配,你前面set匹配泛型,等号后面就不能不匹配了
逍遥jc 2014-06-26
  • 打赏
  • 举报
回复
可以加上@SuppressWarnings("unchecked")。但作用不大。忽略警告就是欺骗自己,一般我都不这么做。最好就是加上泛型。
a137655624 2014-06-26
  • 打赏
  • 举报
回复
集合的警告是泛型未检查。。。。你可以给他加上泛型 17 行 Set<Integer> set = new HashSet<Integer>(); 或者用注解@SuppressWarnings("unchecked")。。。。但是后者不建议使用
业余草 2014-06-25
  • 打赏
  • 举报
回复
集合的话,泛型,可以@SuppressWarnings("unchecked"),最好不要使用这些去掉检查。但这句注解很起作用的
tony4geek 2014-06-25
  • 打赏
  • 举报
回复
@SuppressWarnings("unchecked")
  • 打赏
  • 举报
回复
Set<Integer> set = new HashSet<Integer>(); Set<String> set = new HashSet<String>();
姜小白- 2014-06-25
  • 打赏
  • 举报
回复
取消警告check
wlzzlw 2014-06-25
  • 打赏
  • 举报
回复
在这个方法上面加个注解就可以了: @SuppressWarnings("unchecked")

62,630

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧