Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in LinkedHashSet. Unable to put the elements  RSS feed

 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Student class has roll number, standard, first name and last name. And LinkedHashMap class has the key as student object and value as an integer. My question is why is the Student object giving error of compile time as cannot make static reference to a non static  type Student. I have made all the attributes in STudent class as static. And error -  LinkedHashMap is a raw type. References to generic type LinkedHashMap<Student,Integer> should be
parameterized



 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, this syntax is wrong.


It needs to be:


As far as Student, are both of your classes in the same package? What command or IDE are you using to compile?
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jeanne
Yes they are in the same  package. Still it is showing compile time error despite changing the syntax
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Eclipse neon IDE
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have modified the code to  . And error is

Multiple markers at this line
- The type LinkedHashMap is not generic; it cannot be parameterized with arguments <Student,
Integer>
- Cannot make a static reference to the non-static type Student
- Cannot make a static reference to the non-static type Integer


 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You named your LinkedHashMap class after another that already exists. Don't reuse names from the standard API.

You're probably trying to use java.util.LinkedHashMap (which is generic), but the compiler thinks you want to use the class you declared (which is not generic).
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Stephan thanks. But again an error - All the outputs are the same .
roll no=2 standard=6 firstname=Kajari lastname=Agrawal  2
roll no=2 standard=6 firstname=Kajari lastname=Agrawal  4
roll no=2 standard=6 firstname=Kajari lastname=Agrawal  6
roll no=2 standard=6 firstname=Kajari lastname=Agrawal  5
roll no=2 standard=6 firstname=Kajari lastname=Agrawal  7
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Student doesn't implement equals and hash code. The hash map doesn't know what to do with them.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need to see the code for your Student class.
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out that i should require a hashcode and equals in the student method of linkedhashmap

I am however getting compiler error . can you please correct my code.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When designing hashCode() and equals() you need to take into account which fields you'll want to use. I'm assuming  in your case that firstname and lastname together make up a unique key for each object, and that rollno and std are not unique or don't add any additional uniqueness to firstname and lastname. If that's the case then you might only want to use firstname and lastname when you create hashCode() and equals().
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the future, please post the compiler error not just that you got one.

In the equals method, it isn't compiling because you are trying to return a bunch of comma separated values:


That doesn't work. What is the logical "and" operator in Java?
 
Sucheta Shrivastava
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ carey.

I want the syntax
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an article on how to write equals() correctly.

I thought that is one of the better explanation than most that I've seen. Yep, there's a fair bit that goes into doing it correctly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!