Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help me solve/understand this question. I have tried googling so much but failed.

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me solve/understand this question. I have tried googling so much but failed.

    The question:
    Fix the following code so the the map.get() call retrieves the expected value. Do not change the main method.

    import java.util.HashMap;
    import java.util.Map;

    public class StudentTest {
    public static final class Student {
    public Student( String name ) {
    this.name = name;
    }
    private String name;
    }

    public static void main(final String[] args ) {
    Map<Student, String> map = new HashMap<>();
    map.put( new Student( "john"), "present" );
    System.out.println( map.get( new Student( "john" ) ) );
    }
    }

    Now I know I can get the answer by changing the main method like so:
    Student s = new Student("john")
    System.out.println( map.get( s) );

    but how can I achieve this without changing the main method?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help me solve/understand this question. I have tried googling so much but failed.

    Have you read the API doc for the Map class's get method to see what it does and what it uses?
    Basically you need to add some methods to the Student class so that get works.
    The API doc http://docs.oracle.com/javase/8/docs/api/index.html

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: November 4th, 2013, 05:38 AM
  2. Need Help to Solve The 2 Question...
    By pineman in forum Java Theory & Questions
    Replies: 3
    Last Post: June 1st, 2012, 09:48 AM
  3. Need Help to Solve The 2 Question...
    By pineman in forum Object Oriented Programming
    Replies: 4
    Last Post: June 1st, 2012, 09:43 AM
  4. How i can solve this question? help me please ...
    By thecraetiveman in forum Java Theory & Questions
    Replies: 49
    Last Post: May 8th, 2012, 02:43 PM
  5. please help solve this question
    By thatguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 29th, 2012, 06:53 PM