0

I don't understand why my code have a warning like this, this is my code:

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();

I am getting the warning :iterator is a raw type. references to generic type iterator e

How can I resolve this?

TheTechRobo the Nerd
  • 1,249
  • 15
  • 28
Ken Kaneki
  • 125
  • 5
  • 15

2 Answers2

3

Iterators are parameterized types.

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

Should be:

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

The iterator should be of type Entry<...> because the entry set returns a set of entries from the map. The ... Should be the same types as your map. In your case:

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

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

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

Instead of getting the iterator and calling hasNext(), let java do that for you via the enhanced for-loop:

// 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();
    }  
}
  • like this ? Iterator< Entry< String, List > > entries = file.entrySet().iterator(); while (entries.hasNext()) { Entry< String, List > thisEntry = (Entry< String, List >) entries.next(); List value = (List) thisEntry.getValue(); for (List value1 : file.values()) { for (FileItem uploadItem : value1) { csvfile = uploadItem.getInputStream(); } – Ken Kaneki May 07 '18 at 04:05
  • You should not need to cast. That's the reason you put the type in the `<>` angle brackets. If you take the second line of code I wrote, you should be able to safely erase all the casting. Also, you can use the enhanced for loop for cleaner syntax when looping through the entry set. –  May 07 '18 at 04:08
  • @KenKaneki updated answer to include use-case –  May 07 '18 at 04:16
  • im not expert about my english, really thanks for youre help, can you tell me simplify please – Ken Kaneki May 07 '18 at 04:18
  • Using `iterator()` is not necessary. Using this syntax `for (Object obj : collection) { ... }` has the same effect as using `while (iterator.hasNext()) { iterator.next() ... }`, but the first one uses less code because Java calls `iterator(), hasNext() and next()` for you so you don't have to. –  May 07 '18 at 04:21
  • @KenKaneki But regarding the original question, it's because you didn't add the stuff between `<>`, if you want, you can leave the code as is. My suggestion was just to write it in less lines –  May 07 '18 at 04:24
  • im realy thanks to you for youre help, now im understand – Ken Kaneki May 07 '18 at 06:29
0

Change Iterator to Iterator<Entry<String, List<FileItem>>>, and similarly change Entry to Entry<String, List<FileItem>>.

But it looks like you really want to loop over values in the map files. In that case, just use files.values(). There's no need to use any Iterator or Entry, the following replaces your entire while loop:

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

Your complete code looks like this:

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();
k_ssb
  • 6,024
  • 23
  • 47