Type Conversion Error using For-Each Loop












1















I'm getting this error on my for-each loop:




"Type mismatch: Cannot convert from element type Object to Employee"




private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

public HashMap getEmployeeDatabase() {
return employeeDatabase;
}

for(Employee e: c.getEmployeeDatabase().values())
{
e.print();
}


If it matters - 'Employee' contains int(id),String(name),double(salary). The int(id) is copied for use as the Integer key for my HashMap when the HashMap is populated.



Edit: The issue was with the getEmployeeDatabase accessor returning a raw type, thank you to those who answered.



For those wondering about 'c' variable:



Company c = new Company();


That's where it came from. The Company class default constructor uses a private method to populate employeeDatabase from Scanner.










share|improve this question




















  • 1





    Return type HashMap is a raw generic. DO NOT use raw generics. Specify the correct type parameters, i.e. HashMap<Integer,Employee>

    – Andreas
    Nov 14 '18 at 3:16











  • can you show your complete code?

    – GauravRai1512
    Nov 14 '18 at 3:24
















1















I'm getting this error on my for-each loop:




"Type mismatch: Cannot convert from element type Object to Employee"




private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

public HashMap getEmployeeDatabase() {
return employeeDatabase;
}

for(Employee e: c.getEmployeeDatabase().values())
{
e.print();
}


If it matters - 'Employee' contains int(id),String(name),double(salary). The int(id) is copied for use as the Integer key for my HashMap when the HashMap is populated.



Edit: The issue was with the getEmployeeDatabase accessor returning a raw type, thank you to those who answered.



For those wondering about 'c' variable:



Company c = new Company();


That's where it came from. The Company class default constructor uses a private method to populate employeeDatabase from Scanner.










share|improve this question




















  • 1





    Return type HashMap is a raw generic. DO NOT use raw generics. Specify the correct type parameters, i.e. HashMap<Integer,Employee>

    – Andreas
    Nov 14 '18 at 3:16











  • can you show your complete code?

    – GauravRai1512
    Nov 14 '18 at 3:24














1












1








1








I'm getting this error on my for-each loop:




"Type mismatch: Cannot convert from element type Object to Employee"




private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

public HashMap getEmployeeDatabase() {
return employeeDatabase;
}

for(Employee e: c.getEmployeeDatabase().values())
{
e.print();
}


If it matters - 'Employee' contains int(id),String(name),double(salary). The int(id) is copied for use as the Integer key for my HashMap when the HashMap is populated.



Edit: The issue was with the getEmployeeDatabase accessor returning a raw type, thank you to those who answered.



For those wondering about 'c' variable:



Company c = new Company();


That's where it came from. The Company class default constructor uses a private method to populate employeeDatabase from Scanner.










share|improve this question
















I'm getting this error on my for-each loop:




"Type mismatch: Cannot convert from element type Object to Employee"




private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

public HashMap getEmployeeDatabase() {
return employeeDatabase;
}

for(Employee e: c.getEmployeeDatabase().values())
{
e.print();
}


If it matters - 'Employee' contains int(id),String(name),double(salary). The int(id) is copied for use as the Integer key for my HashMap when the HashMap is populated.



Edit: The issue was with the getEmployeeDatabase accessor returning a raw type, thank you to those who answered.



For those wondering about 'c' variable:



Company c = new Company();


That's where it came from. The Company class default constructor uses a private method to populate employeeDatabase from Scanner.







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 3:34







macsters

















asked Nov 14 '18 at 3:10









macstersmacsters

92




92








  • 1





    Return type HashMap is a raw generic. DO NOT use raw generics. Specify the correct type parameters, i.e. HashMap<Integer,Employee>

    – Andreas
    Nov 14 '18 at 3:16











  • can you show your complete code?

    – GauravRai1512
    Nov 14 '18 at 3:24














  • 1





    Return type HashMap is a raw generic. DO NOT use raw generics. Specify the correct type parameters, i.e. HashMap<Integer,Employee>

    – Andreas
    Nov 14 '18 at 3:16











  • can you show your complete code?

    – GauravRai1512
    Nov 14 '18 at 3:24








1




1





Return type HashMap is a raw generic. DO NOT use raw generics. Specify the correct type parameters, i.e. HashMap<Integer,Employee>

– Andreas
Nov 14 '18 at 3:16





Return type HashMap is a raw generic. DO NOT use raw generics. Specify the correct type parameters, i.e. HashMap<Integer,Employee>

– Andreas
Nov 14 '18 at 3:16













can you show your complete code?

– GauravRai1512
Nov 14 '18 at 3:24





can you show your complete code?

– GauravRai1512
Nov 14 '18 at 3:24












2 Answers
2






active

oldest

votes


















3














Your method return type is the raw type. Don't use raw types.



private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

public HashMap getEmployeeDatabase() {
return employeeDatabase;
}


Should be something like



private static Map<Integer, Employee> employeeDatabase = new HashMap<>();

public Map<Integer, Employee> getEmployeeDatabase() {
return employeeDatabase;
}





share|improve this answer

































    0














    When you dont specify generics, Object is assumed, then



    public HashMap getEmployeeDatabase()


    Is actually



    public HashMap<Object, Object> getEmployeeDatabase()


    So



    for(Employee e : c.getEmployeeDatabase().values()) {
    e.print();
    }


    Is incorrect because values() will return



    Collection<Object>





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292649%2ftype-conversion-error-using-for-each-loop%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      Your method return type is the raw type. Don't use raw types.



      private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

      public HashMap getEmployeeDatabase() {
      return employeeDatabase;
      }


      Should be something like



      private static Map<Integer, Employee> employeeDatabase = new HashMap<>();

      public Map<Integer, Employee> getEmployeeDatabase() {
      return employeeDatabase;
      }





      share|improve this answer






























        3














        Your method return type is the raw type. Don't use raw types.



        private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

        public HashMap getEmployeeDatabase() {
        return employeeDatabase;
        }


        Should be something like



        private static Map<Integer, Employee> employeeDatabase = new HashMap<>();

        public Map<Integer, Employee> getEmployeeDatabase() {
        return employeeDatabase;
        }





        share|improve this answer




























          3












          3








          3







          Your method return type is the raw type. Don't use raw types.



          private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

          public HashMap getEmployeeDatabase() {
          return employeeDatabase;
          }


          Should be something like



          private static Map<Integer, Employee> employeeDatabase = new HashMap<>();

          public Map<Integer, Employee> getEmployeeDatabase() {
          return employeeDatabase;
          }





          share|improve this answer















          Your method return type is the raw type. Don't use raw types.



          private static HashMap<Integer,Employee> employeeDatabase = new HashMap<Integer,Employee>();

          public HashMap getEmployeeDatabase() {
          return employeeDatabase;
          }


          Should be something like



          private static Map<Integer, Employee> employeeDatabase = new HashMap<>();

          public Map<Integer, Employee> getEmployeeDatabase() {
          return employeeDatabase;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 3:19

























          answered Nov 14 '18 at 3:14









          Elliott FrischElliott Frisch

          154k1391180




          154k1391180

























              0














              When you dont specify generics, Object is assumed, then



              public HashMap getEmployeeDatabase()


              Is actually



              public HashMap<Object, Object> getEmployeeDatabase()


              So



              for(Employee e : c.getEmployeeDatabase().values()) {
              e.print();
              }


              Is incorrect because values() will return



              Collection<Object>





              share|improve this answer




























                0














                When you dont specify generics, Object is assumed, then



                public HashMap getEmployeeDatabase()


                Is actually



                public HashMap<Object, Object> getEmployeeDatabase()


                So



                for(Employee e : c.getEmployeeDatabase().values()) {
                e.print();
                }


                Is incorrect because values() will return



                Collection<Object>





                share|improve this answer


























                  0












                  0








                  0







                  When you dont specify generics, Object is assumed, then



                  public HashMap getEmployeeDatabase()


                  Is actually



                  public HashMap<Object, Object> getEmployeeDatabase()


                  So



                  for(Employee e : c.getEmployeeDatabase().values()) {
                  e.print();
                  }


                  Is incorrect because values() will return



                  Collection<Object>





                  share|improve this answer













                  When you dont specify generics, Object is assumed, then



                  public HashMap getEmployeeDatabase()


                  Is actually



                  public HashMap<Object, Object> getEmployeeDatabase()


                  So



                  for(Employee e : c.getEmployeeDatabase().values()) {
                  e.print();
                  }


                  Is incorrect because values() will return



                  Collection<Object>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 3:31









                  JuanJuan

                  525




                  525






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292649%2ftype-conversion-error-using-for-each-loop%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly