12 Replies Latest reply: Feb 7, 2011 2:49 AM by 837422 RSS

    The type Set is not generic; it cannot be parameterized with arguments <K>?

    830049
      When I use Hashtable or HashMap to get the keySet, it shows the error of "The type Set is not generic; it cannot be parameterized with arguments <K>".


      The following is my code:


      Hashtable table = new Hashtable();
      table.put("A", new Integer(1));
      table.put("B", new Integer(2));


      Iterator its = table.keySet().iterator(); // <<<<<<<<<<<<<< this line shows the error "The type Set is not generic; it cannot be parameterized with arguments <K>"

      How can I solve it? Please help me! I have no idea on it. It works fine in 1.4.

      Best regards,
      Eric
        • 1. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
          EJP
          I do not get that error from that code.

          I conclude that that isn't the real code.

          I do get a couple of warnings.
          • 2. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
            Opal
            Should be:
            Hashtable<String, Integer> table = new Hashtable<String, Integer>();
            table.put("A", new Integer(1));
            table.put("B", new Integer(2));
            Iterator<String> its = table.keySet().iterator();
            As EJP said the code doesn't produce any errors. These are warnings. Read about generics.
            • 3. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
              830049
              But it shows error in the eclipse editor and cannot compile.

              Please see the image for details.


              https://docs.google.com/leaf?id=0BwprZzDjueGeNDY4NzdlYzktYjI4NS00N2E2LWE5MmItMzdmYzIzZGQ0YmRk&hl=en


              Eric
              • 4. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                EJP
                As I concluded above, the code showing in the editor window in that picture is not the code you posted above. You're not going to get anywhere here until you post some code that actually produces that error message.

                There are several things seriously wrong here. The code in the picture uses HashMap, while the code you posted above uses Hashtable. A collection that you have just created is empty, so iterating over it will never execute the code block concerned. You seem to have two variables called 'tmp', which is causing at least some of this confusion. The second one that is declared in the picture seems pointless to me. Did you mean to call it 'out' instead of 'tmp'?
                • 5. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                  830049
                  The original is my codes, please help!
                  public static List findDlicApp(Date startDate, Date endDate, Connection con) {
                            String SQL = getSQL("app.sql");
                            
                            String where = getSQL("app.sql.where");
                            
                            boolean hasWhere = false;
                            
                            /*
                            if (startDate != null) {
                                 SQL = SQL + " where d.create_date >= to_date('" + DMSUtil.convertDateToString(startDate) + "', 'yyyy-MM-dd')";
                                 hasWhere = true;
                            }
                            */
                            
                            if (endDate != null) {
                                 if (hasWhere) {
                                      SQL = SQL + " and to_date(to_char(d.create_date, 'yyyy-mm-dd'), 'yyyy-mm-dd') <= to_date('" + DMSUtil.convertDateToString(endDate) + "', 'yyyy-MM-dd')";
                                 } else {
                                      SQL = SQL + " where to_date(to_char(d.create_date, 'yyyy-mm-dd'), 'yyyy-mm-dd') <= to_date('" + DMSUtil.convertDateToString(endDate) + "', 'yyyy-MM-dd')";
                                 }
                                 
                                 hasWhere = true;
                            }
                            
                            
                            
                            if (hasWhere) {          
                                 SQL = SQL + " and " + where;
                            } else {
                                 SQL = SQL + " where " + where;
                            }
                            
                            SQL = SQL + " " + getSQL("app.sql.order");
                                      
                            
                            //System.out.println(SQL);
                            
                            //Connection con =  getParaDMConnection("findDlicApp");
                            
                            HashMap<String, DlicApp> dlicDocs = new HashMap<String, DlicApp>();
                            
                            List result = new ArrayList();
                            
                            try {
                            
                                 Statement stmt = con.createStatement();
                                 ResultSet rs = stmt.executeQuery(SQL);
                                                               
                                 long lastDocID = 0;
                                 
                                 boolean hasStartDate = false;
                                 
                                 while(rs.next()) {
                                      long docID = rs.getLong("docID");
                                      String docName = rs.getString("docName");
                                      String refNo = rs.getString("refNo");
                                      java.util.Date createDate = rs.getDate("createDate");
                                      String creator = rs.getString("creator");
                                      String profileType = rs.getString("profileType");
                                      String assunto = rs.getString("assunto");
                                      String fromEntity = rs.getString("fromEntity");     
                                      String location = getLocation(docID, con);
                                      long fieldID = rs.getLong("fieldID");
                                      String fieldValue = rs.getString("fValue");
                                      
                                      DlicApp doc = null;
                                      
                                      if (dlicDocs.containsKey(String.valueOf(docID))) {
                                           doc = (DlicApp)dlicDocs.get(String.valueOf(docID));
                                      } else {
                                           doc = new DlicApp();
                                      }
                                      
                                      
                                      doc.setId(docID);
                                      doc.setDocName(docName);
                                      doc.setReferenceNo(refNo);
                                      doc.setCreateDate(createDate);
                                      doc.setCreator(creator);
                                      doc.setProfileType(profileType);
                                      doc.setAssunto(assunto);
                                      doc.setFrom(fromEntity);
                                      
                                      //if (doc.getStartDate() == null) {                    
                                      //     doc.setStartDate(createDate);
                                      //}
                                      
                                      doc.setLocation(location);
                                      
                                      
                                      if (fieldValue != null) {                    
                                           
                                           if (fieldID == 1114) {
                                                doc.setChineseName(fieldValue);
                                           }
                                           
                                           if (fieldID == 1115) {
                                                doc.setPortugueseName(fieldValue);
                                           }
                                           
                                           if (fieldID == 1116) {
                                                doc.setApplicationCategory(fieldValue);
                                           }
                                           
                                           if (fieldID == 1118) {
                                                doc.setStatus(fieldValue);
                                           }
                                           
                                           if (fieldID == 1119) {
                                                Date d = DMSUtil.parseDate(fieldValue, "yyyy-MM-dd");
                                                doc.setStartDate(d);
                                                
                                                hasStartDate = true;
                                           }
                                           
                                           if (fieldID == 1120) {
                                                Date d = DMSUtil.parseDate(fieldValue, "yyyy-MM-dd");
                                                                                                                                          
                                                if (!StringUtils.isEmpty(fieldValue)) {
                                                     
                                                     //System.out.println(docName + ":" + fieldValue + ">>>>>>>>>findDlicApp>>>>>>>>>>>>>>>>>>APP END DATE: " + d);
                                                     
                                                     doc.setEndDate(d);
                                                }
                                           }
                                      }
                                      /*
                                      if (docID != lastDocID) {                    
                                           doc.setRelatedDocs(findRelatedDoc(docID, con));
                                           
                                           lastDocID = docID;
                                      }
                                      */
                                      
                                      dlicDocs.put(String.valueOf(docID), doc);
                                 }
                                 
                                 stmt.close();
                                 rs.close();
                                 
                                 
                                 
                                 Iterator<String> its = dlicDocs.keySet().iterator();
                                 while(its.hasNext()) {
                                      String id = (String)its.next();
                                      DlicApp a = (DlicApp)dlicDocs.get(id);
                                      
                                      a.setRelatedDocs(findRelatedDoc(a.getId(), con));
                                      
                                      dlicDocs.put(id, a);
                                 }
                                 
                                 
                                 
                                 result.addAll(dlicDocs.values());
                                 
                                 /*
                                 // take out start date is not in the given period
                                 int n = 0;
                                 while(true) {
                                      
                                      if (n < result.size()) {
                                           DlicApp a = (DlicApp)result.get(n);               
                                           
                                           Date sd = a.getStartDate();
                                           
                                           if (!isWithin(sd, startDate, endDate)) {
                                                result.remove(n);
                                           } else {
                                                n++;
                                           }
                                           
                                      } else {
                                           break;
                                      }
                                 }
                                 */
                                 
                                 
                            } catch(Exception e) {
                                 e.printStackTrace();
                            }
                  
                  
                            if (result.size() > 0) {
                                 Collections.sort(result, new DmsDocComparator());
                            }
                            
                            return result;
                       }
                  Edited by: EJP on 13/01/2011 14:41: added code tags for you. Please use them next time.
                  • 6. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                    EJP
                    Once again this code does not produce the error message claimed. Assuming you are using java.util.HashMap.

                    Further comments below.

                    1. You should be using PreparedStatements with the ? and setObject() method rather than building variables into the SQL like this. It is much easier and much clearer.
                         public static List<DlicApp> findDlicApp(Date startDate, Date endDate, Connection con)
                    ...
                              List result = new ArrayList();
                    Why aren't this List and the return type generic?
                    public static List<DlicApp> findDlicApp(Date startDate, Date endDate, Connection con)
                    ...
                              List<DlicApp> result = new ArrayList<DlicApp>();
                                        if (dlicDocs.containsKey(String.valueOf(docID))) {
                                             doc = (DlicApp)dlicDocs.get(String.valueOf(docID));
                                        } else {
                                             doc = new DlicApp();
                                        }
                    So you are overwriting any previous document with that ID with a new one. This cannot possibly be correct. Either you won't be getting duplicate document IDs or you should be returning them all, surely?

                    Note that that cast to (DlicApp) is redundant, as the compiler would have told you.
                                             if (fieldID == 1114) { ... }
                                             if (fieldID == 1115) { ... }
                                             if (fieldID == 1116) { ... }
                                             if (fieldID == 1118) { ... }
                                             if (fieldID == 1119) { ... }
                                             if (fieldID == 1120) { ... }
                    This is a job for a case statement. You don't even have 'else's between these ifs. Poor code.
                                   stmt.close();
                                   rs.close();
                    These two closes should be the other way about and they should both be in finally{} blocks.
                                        String id = (String)its.next();
                                        DlicApp a = (DlicApp)dlicDocs.get(id);
                    The typecasts here are unnecessary, as the compiler would have told you.
                    • 7. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                      818890
                      Hi there,

                      Could you please paste the import statements you have at the top of this class file exactly as they appear.

                      Regards,
                      Kate
                      • 8. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                        830049
                        import java.io.InputStream;
                        import java.sql.Connection;
                        import java.sql.DriverManager;
                        import java.sql.PreparedStatement;
                        import java.sql.ResultSet;
                        import java.sql.Statement;
                        import java.util.ArrayList;
                        import java.util.Collections;
                        import java.util.Date;
                        import java.util.GregorianCalendar;
                        import java.util.HashMap;
                        import java.util.Hashtable;
                        import java.util.Iterator;
                        import java.util.List;
                        import java.util.Properties;
                        • 9. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                          EJP
                          What JDK are you using? java -version please. Also do you have any HashMap.class files lying around anywhere?
                          • 10. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                            830049
                            I am using jdk-1.6.0.23 within eclipse.



                            Best regards,
                            Eric
                            • 11. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                              EJP
                              It sounds to me like you have a bodgy version of java.util.HashMap or Hashtable or Set lying around somewhere. Failing that I would probably remove and reinstall the JDK at this point.
                              • 12. Re: The type Set is not generic; it cannot be parameterized with arguments <K>?
                                837422
                                Hi ,

                                One thing you can try :-

                                I was getting the same problem before, and I overcome with change in sequence of jre library.
                                Thanks and Regards ,
                                Dnyanesh