java使用Hashset计算数组中的字符串数量,无重复项
我试图使用哈希集来计算字符串数组中的字符串数量,而不计算重复的字符串。但是,该程序无法正常工作。例如,这段代码打印出“4”,而实际上它们只有3个唯一的字符串。有人知道这为什么不正确吗
String centers[]=new String[1000];
/* Only for Testing Purposes*/
centers[0] = "Soccer";
centers[1] = "Soccer";
centers[2]= "Baseball";
centers[3] = "Table Tennis";
centers[4] = "Soccer";
List<String> centerList = Arrays.asList(centers);
Set<String> uniqueCenters = new HashSet<String>();
uniqueCenters.addAll(centerList);
Integer numberOfUniqueStrings = uniqueCenters.size();
System.out.println(numberOfUniqueStrings);
共 (0) 个答案