• Post Reply Bookmark Topic Watch Topic
  • New Topic

Missing the end of a HashMap  RSS feed

 
Tony McManus
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a project where we were only given part of the HashMap. I'm having a lot of trouble understanding how this is set up and how to work with the data. Can someone suggest a way to close this HashMap successfully?

Some points that I'm not sure how to approach:
- The HashMap obviously calls to an object titled "Entity". I'm assuming that object then defines the parameters in the parentheses. The object "entity" then has different variable names. I'm not sure how to deal with the differently named object variables in addition to the different parameters.
- I receive a compiler error because the "Entity" is not defined. I thought that I should write a java bean to define it, based on solving the question above. Is that a good approach?

Thanks in advance for the help!

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap<String, Entity> means that the map accepts Strings as keys objects of type Entity as values. You are adding Customer, CSR,Technician e.t.c to the map as values so those classes (Customer, CSR,Technician e.t.c) must extend a class called Entity or implement an interface called Entity for that to work.
 
Tony McManus
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote:HashMap<String, Entity> means that the map accepts Strings as keys objects of type Entity as values. You are adding Customer, CSR,Technician e.t.c to the map as values so those classes (Customer, CSR,Technician e.t.c) must extend a class called Entity or implement an interface called Entity for that to work.


Thanks for the reply. I know that I need to implement an interface called "Entity", but I'm not really sure how to differentiate between customer, csr, tech, etc. Here is some code I tried while focusing just on the HashMap titled "items". I'm pretty sure that you'll see that it doesn't work, but I don't really understand what I"m doing wrong.

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Tony,

are you sure that you have your HashMaps correct? They all have this 'Entity' as value, but if I follow
your code, then this 'Entity' can be a lot of different things. Ranging from Customer to LiveEvent
and Movie.

The 'Entity' class that you define is not suitable for a Customer, for instance.
Now, if you must do it this way, then maybe it is wise to just
define an interface 'Entity', with no methods, and then define separate classes for
whatever you want to store in the hashmaps. These classes all should then
implement this empty interface.

But the danger of this is that you then easily can put a Movie in your 'users' map.

Greetz,
Piet
 
Arthur Vinicius Rebelo
Ranch Hand
Posts: 30
1
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,

Although you declared in HashMap instantiation that it must accept an Entity object type, afterward you never passed to your HashMap one Entity instance. You tried to pass others object types and those objects must be an Entity (i.e. extends Entity class or implement an Entity interface). Lets take the extend Entity approach.

Because you never instantiate an Entity you don't really have to worry about class Entity body. What you have to worry about is Customer, Manager, Movie, TVPlan, etc. implementation. Lets take Customer as an example.

In line 8 you instantiate a new Customer, passing six Strings to the constructor. That means you have to implement a class called Customer that has a constructor accepting six Strings as arguments (and extend Entity class !)

Like this:



Now your line 8 became valid because the class Customer is an Entity (class Customer extends Entity) so you can pass it to your HashMap and you created a constructor that accepts your arguments from line 8. Try to do this with your others instantiations.
 
Tony McManus
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:hi Tony,

are you sure that you have your HashMaps correct? They all have this 'Entity' as value, but if I follow
your code, then this 'Entity' can be a lot of different things. Ranging from Customer to LiveEvent
and Movie.

The 'Entity' class that you define is not suitable for a Customer, for instance.
Now, if you must do it this way, then maybe it is wise to just
define an interface 'Entity', with no methods, and then define separate classes for
whatever you want to store in the hashmaps. These classes all should then
implement this empty interface.

But the danger of this is that you then easily can put a Movie in your 'users' map.

Greetz,
Piet


Piet - thanks for trying to help. The code is from my prof, so it's legit...just missing something (that I obviously can't figure out). It's an online course through a very reputable university, but the guy isn't very good about responding when people need help .
 
Tony McManus
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arthur Vinicius Rebelo wrote:Hi Tony,

Although you declared in HashMap instantiation that it must accept an Entity object type, afterward you never passed to your HashMap one Entity instance. You tried to pass others object types and those objects must be an Entity (i.e. extends Entity class or implement an Entity interface). Lets take the extend Entity approach.

Because you never instantiate an Entity you don't really have to worry about class Entity body. What you have to worry about is Customer, Manager, Movie, TVPlan, etc. implementation. Lets take Customer as an example.

In line 8 you instantiate a new Customer, passing six Strings to the constructor. That means you have to implement a class called Customer that has a constructor accepting six Strings as arguments (and extend Entity class !)

Like this:



Now your line 8 became valid because the class Customer is an Entity (class Customer extends Entity) so you can pass it to your HashMap and you created a constructor that accepts your arguments from line 8. Try to do this with your others instantiations.


I didn't actually understand this the first time I read it, because I'm using JavaBeans (and I didn't think beans use the 'extends' in the class). I'm allowed to use servlets in addition to beans, but frankly the organization of all of that is murky to me.

Now that i've re-read it, however, it makes a lot more sense. I'll give it a try!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!