java - 在集合中键入安全警告
问题描述
我在下面的代码片段中试图获得最大日期组。代码运行良好,但它显示警告说明类型安全:取消选中调用。有什么遗漏需要检查?
class Movie<T> implements Comparator<T> {
static LinkedHashMap<String, List<Date>> listOfDates = new LinkedHashMap<String, List<Date>>();
static {
listOfDates.put("1", new ArrayList<Date>());
listOfDates.put("2", new ArrayList<Date>());
listOfDates.put("3", new ArrayList<Date>());
listOfDates.put("4", new ArrayList<Date>());
listOfDates.put("5", new ArrayList<Date>());
listOfDates.put("6", new ArrayList<Date>());
listOfDates.put("7", new ArrayList<Date>());
listOfDates.put("8", new ArrayList<Date>());
listOfDates.put("9", new ArrayList<Date>());
listOfDates.put("10", new ArrayList<Date>());
listOfDates.put("11", new ArrayList<Date>());
listOfDates.put("12", new ArrayList<Date>());
}
public static void loadDates(String inpDate) {
String[] dates = "27-05-201901-01-2019,02-01-2019,03-01-2019,04-01-2019,01-02-2019,01-03-2019,21-03-2019,20-05-2019,21-05-2019,22-05-2019,26-05-2019".split(",");
for (int i = 0; i < dates.length; i++) {
try {
Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dates[i]);
String mth = Integer
.toString(date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate().getMonthValue());
listOfDates.get(mth).add(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
System.out.println(finalString(listOfDates));
}
@Override
public int compare(Object date1, Object date2) {
Date d1 = (Date) date1;
Date d2 = (Date) date2;
int i = 0;
if (d1.compareTo(d2) == 0) {
i = 0;
}
else if (d1.compareTo(d2) > 0) {
i = 1;
}
else if (d1.compareTo(d2) < 0) {
i = -1;
}
return i;
}
public static String finalString(LinkedHashMap<String, List<Date>> dates) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
StringBuilder sb = new StringBuilder();
StringBuilder finalsb = new StringBuilder();
for (Map.Entry<String, List<Date>> entry : dates.entrySet()) {
if (entry.getValue().isEmpty() || entry.getValue().size() == 0) {
sb.append(" ").append(",");
} else if (entry.getValue().size() == 1) {
sb.append(dateFormat.format(entry.getValue().get(0))).append(",");
}
else {
sb.append(dateFormat.format(Collections.max(entry.getValue(), new Movie()))).append(","); //WARNING
}
}
int lastIndexOfTab = sb.lastIndexOf(",");
finalsb.append(sb.substring(0, lastIndexOfTab));
return finalsb.toString();
}
public static void main(String[] args) {
loadDates("Enter");
}
}
但是我//WARNING
在其他条件下的评论中收到警告。
我可以知道为什么会收到以下警告吗?
Type safety: Unchecked invocation max(List<Date>, Movie) of the generic method max(Collection<? extends T>, Comparator<? super T>) of type Collections
为什么它来了,我应该如何避免它..?
任何建议都会有所帮助。
解决方案
推荐阅读
- ruby-on-rails - 在构建期间使用 Dockerfile“无法找到 gemfile”在 Rails 上安装 ruby
- python - Python:在 main() 中访问 __version__ 的最优雅方式
- android - 反应原生模块中的“arm64-v8a”、“x86_64”上的应用程序崩溃
- html - 如何修复foreach循环在轮播引导程序4中两次打印数据
- python - 在 Python 中部分应用的高效方法?
- kubernetes - 一个部署资源可以有多个容器吗?
- java - @RequiredArgsConstructor(onConstructor = @__(@Inject)) 和 @RequiredArgsConstructor 有什么区别?
- visual-studio - 在 Visual Studio 颜色主题编辑器中更改错误列表窗口中颜色的类别是什么
- javascript - 如何使用 javascript 将 JSON 转换为普通的 PDF 文本格式或表格格式?
- python - html代码的问题如何放入数据库