Forums Register Login

Problem in LinkedHashSet. Unable to put the elements

+Pie Number of slices to send: Send
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



+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
@Jeanne
Yes they are in the same  package. Still it is showing compile time error despite changing the syntax
+Pie Number of slices to send: Send
I am using Eclipse neon IDE
+Pie Number of slices to send: Send
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


1
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
@ 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
+Pie Number of slices to send: Send
Student doesn't implement equals and hash code. The hash map doesn't know what to do with them.
+Pie Number of slices to send: Send
We need to see the code for your Student class.
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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().
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
@ carey.

I want the syntax
+Pie Number of slices to send: Send
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.
Slideshow boring ... losing consciousness ... just gonna take a quick nap on this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 507 times.
Similar Threads
please help resolve the problem while running the code
Generate employee id trouble
Validation problem please help??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:57:15.