首页 > 解决方案 > 在集合中键入安全警告

问题描述

我在下面的代码片段中试图获得最大日期组。代码运行良好,但它显示警告说明类型安全:取消选中调用。有什么遗漏需要检查?

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

为什么它来了,我应该如何避免它..?

任何建议都会有所帮助。

标签: javagenericscollections

解决方案


推荐阅读