Type Conversion Error using For-Each Loop
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
add a comment |
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
1
Return typeHashMapis 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
add a comment |
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
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
java
edited Nov 14 '18 at 3:34
macsters
asked Nov 14 '18 at 3:10
macstersmacsters
92
92
1
Return typeHashMapis 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
add a comment |
1
Return typeHashMapis 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
add a comment |
2 Answers
2
active
oldest
votes
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;
}
add a comment |
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>
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited Nov 14 '18 at 3:19
answered Nov 14 '18 at 3:14
Elliott FrischElliott Frisch
154k1391180
154k1391180
add a comment |
add a comment |
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>
add a comment |
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>
add a comment |
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>
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>
answered Nov 14 '18 at 3:31
JuanJuan
525
525
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Return type
HashMapis 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