• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in LinkedHashSet. Unable to put the elements

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Eclipse neon IDE
 
Sucheta Shrivastava
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Student doesn't implement equals and hash code. The hash map doesn't know what to do with them.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We need to see the code for your Student class.
 
Sucheta Shrivastava
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ carey.

I want the syntax
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic