java:warning:iterator是一个原始类型

时间:2018-05-07 03:36:50

标签: java eclipse servlets iterator

我不明白为什么我的代码有这样的警告,这是我的代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

     response.setContentType("text/html");  
     PrintWriter out=response.getWriter(); 
    try {

             InputStream csvfile = null;

             FileItemFactory factory = new DiskFileItemFactory();

             ServletFileUpload upload = new ServletFileUpload(factory);
             Map<String, List<FileItem>> file = upload.parseParameterMap(request);

             Iterator entries = file.entrySet().iterator();
             while (entries.hasNext()) {
                  Entry thisEntry = (Entry) entries.next();
                  List<FileItem> value = (List) thisEntry.getValue();
                    for (FileItem uploadItem : value) {
                        csvfile = uploadItem.getInputStream();
                      }  // end of for loop
             }//while

            InputStreamReader isr = new InputStreamReader(csvfile);
            BufferedReader reader = new BufferedReader(isr);

            String text = "";
            reader.readLine();

我收到警告:迭代器是原始类型。对泛型类型迭代器的引用e

请告诉我一些如何解决这个问题,谢谢你的进步。

2 个答案:

答案 0 :(得分:2)

迭代器是参数化类型。

Iterator entries = file.entrySet().iterator();

应该是:

Iterator< Entry< String, List<FileItem> > > entries = file.entrySet().iterator();

迭代器的类型应为Entry<...>,因为条目集会从地图中返回一组条目。 ...应与地图的类型相同。在你的情况下:

// Type of map
Map<String, List<FileItem> > 

// Type of entries
Entry< String, List<FileItem> >

// Type of iterator
Iterator< Entry< String, List<FileItem> > >

不要获取迭代器并调用hasNext(),而是让java通过增强的for循环为你做这些:

// Just get the entry set, not the iterator
Set< Entry<String, List<FileItem> > > entrySet = file.entrySet();

// Java will inject the iterator, hasNext(), etc for you
// This reads: "For each entry of type XYZ in entrySet, do the following..."

for (Entry< String, List<FileItem> > entry : entrySet) {

    // Get the list from entry, entry is the result of entrySet.next()
    List<FileItem> list = entry.getValue();

    // Nested enhanced for loop use another iterator
    for (FileItem uploadItem : list) {

        csvfile = uploadItem.getInputStream();
    }  
}

答案 1 :(得分:0)

Iterator更改为Iterator<Entry<String, List<FileItem>>>,同样将Entry更改为Entry<String, List<FileItem>>

但看起来你真的想要在地图files中循环遍历值。在这种情况下,只需使用files.values()。无需使用任何IteratorEntry,以下内容将替换整个while循环:

for (List<FileItem> value : files.values()) {
    for (FileItem uploadItem : value) {
         csvfile = uploadItem.getInputStream();
    }
}

您的完整代码如下所示:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");  
    PrintWriter out = response.getWriter();

    try {
        InputStream csvfile = null;
        FileItemFactory factory = new DiskFileItemFactory();

        ServletFileUpload upload = new ServletFileUpload(factory);
        Map<String, List<FileItem>> file = upload.parseParameterMap(request);

        for (List<FileItem> value : files.values()) {
            for (FileItem uploadItem : value) {
                 csvfile = uploadItem.getInputStream();
            }
        }

        InputStreamReader isr = new InputStreamReader(csvfile);
        BufferedReader reader = new BufferedReader(isr);

        String text = "";
        reader.readLine();