• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Need explanation for this Question  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What question ? You've just posted some code.

Also you should UseCodeTags when posting code.
 
Bartender
Posts: 2184
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

As Joanne already said, it is a good idea to UseCodeTags (← click this) when posting. I added them for you this time. Doesn't it look better?

Also, please QuoteYourSources (← click this) when posting questions related to exams.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Doesn't it look better?


A bit, but you could have added some indentation as well.
 
Paweł Baczyński
Bartender
Posts: 2184
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joanne Neal wrote:A bit, but you could have added some indentation as well.


I could but I didn't want to. I left the code exactly as OP posted it.
On OCPJP exam you have to deal with poorly indented code.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:On OCPJP exam you have to deal with poorly indented code.


Okay. I didn't know that.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dharmendra,
As far the problem you may be facing from this code is that you cannot retrieve your values by whatever arguments you try, like if you enter args[0] as "Charis" you cannot retrieve your object back , the reason being:-
1. HashMap uses hashCode() to match the key (Friends) in your case and equals to find the correct value , which is not overridden in your Friends class.
2. So when your are using get(new Friends(args[0])) , it is generating a new object with a random hashCode() generated by original hashCode() function of an Object Class and it returns null as it may not match the hashcode generated by original Charis Friends instance that was added in the map.


Solution:

Override hashCode() and equals() ) in your Friends class to something unique number like name.length and equals to make them meaningfull equal and you can access it.

The Map retrieval works like this:-
1.Check if hashCode matches with any hashCode in map
2. If Yes, find the correct object with equals method.

 
Paweł Baczyński
Bartender
Posts: 2184
46
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swaraj, your code:Why not just this?This is exactly the same but much shorter.

Also, note that your method does not stick to the contract of Object.equals:
  • It will throw NullPointerException when o is null.
  • It will throw ClassCastException when o is not of type Friends.
  •  
    Swaraj Pal
    Greenhorn
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Pawel , just used extended code to explain the concept of equals... your code is prefectly more efficient too..
     
    Rancher
    Posts: 1090
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Dharmendra,

    Welcome again. Actually, not quoting the source of the question is not optional at JavaRanch. I see that they even have a policy on that.

    Javaranch Policy wrote:When asking any question, please quote your sources. Quoting your sources means that you mention where you got the question from: for example the title and authors of a book, the name of a mock exam, address of a website where you got the question from etc.

    This is especially important in the certification forum, for the posting of mock questions. This is needed for several reasons:

    It is polite and professional to do so. Giving attribution is an important habit, and the author of the source would appreciate it.

    For mocks, it will help people find the mock exam. For other cases, it will provide people with a source of information. In either case, it will help put your question in context.

    It helps you if other people can check that you have interpreted the question (or the information) in the same way as the author intended.

    Note: Do not post questions that you got on the actual exam. Those questions are copyrighted and you are not allowed to republish them.



    Besides that, most books and such materials have copyrights associated with them. Mentioning the source of the question often helps with copyrights.
    Also, attribution is always a nice thing.

    So please take a moment to mention the source of your question. And welcome again.

    Chan.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!