1

I am new to collections and looking for help. I am trying to search a map using a key, and return the values of the key which is from another object. This is my code so far.

public class Employer {
    Map<String, NewHire> employee = new HashMap<>();
}

public void addEmployee(String fullName, String age, String location, String JobTitle) {
    NewHire newEmployee = new NewHire(age, location, JobTitle);
    this.employee.put(fullName, newEmployee);
}

The code for the other object is -

public class NewHire {
    private String age;
    private String location;
    private String jobTitle;
}

public NewHire(String aAge, String aLocation, String aJobTitle) {
    this.age = aAge;
     this.location = aLocation;
     this.jobTitle = aJobTitle;
}

I then create like so -

Employer CompanyA = new Employer();

CompanyA.addEmployee("JohnSmith", "23", "London", "Service Desk");

I wanted to create a method that can search the map for a key specified by the user, in this case "JohnSmith", and if found, it then shows me the age, location and jobTitle of that person but I really am not sure how I would go about this.

2 Answers 2

2

The best way to go about it in my opinion is the way Titulum said, using Optional. I would just leave another way, a bit not so nice, but you may understand it better.

You can Override the toString() method in the NewHire class and use it, or create getters for the properties:

@Override
public String toString(){
   return String.format("Age: %s\nLocation: %s\nJobTitle: %s", age, location, jobTitle);
}

// getters
public String getJobTitle() {
    return jobTitle;
}

public String getLocation() {
    return location;
}

public String getAge() {
    return age;
}

On your Employer class, if you want to use the not so much nicer way of doing it (although i recommend using Optional):

public NewHire getEmployeeByName(String fullName){
    return employee.get(fullName);
} 

Then to use it:

Employer employer = new Employer();
employer.addEmployee("JohnSmith", "23", "London", "Service Desk");

NewHire newHire = employer.getEmployeeByName("sJohnSmith");

if(newHire != null) {
    System.out.println(newHire.toString());

    // using getters
    System.out.println(newHire.getAge());
    System.out.println(newHire.getJobTitle());
    System.out.println(newHire.getLocation());
}
Sign up to request clarification or add additional context in comments.

4 Comments

No need to explicitly call toString() for System.out.println() as one of the overloaded methods of println() takes Object as parameter and internally, that will call toString() if the object passed is not null, else just "null". So, if the purpose is just to print, no need of explicit null check also
We have not been taught Optional as of yet (that comes later), so I wouldn't be able to use it for now :(
@NewtoJavaHelp Then you can use it like that, if you don't want to use the toString() method, you can create getters for all the properties and use them instead.
@NewtoJavaHelp If the answer solved your problem, please mark it as a solution. Thanks!
1

You can simply write the method as follows:

public Optional<NewHire> findByFullName(String fullName) {
  return Optional.ofNullable(employee.get(fullName));
}

This will return you an Optional, which is an Object in Java that contains either something or nothing. To see if the Optional contains anything you can do:

Optional<NewHire> possiblyFoundNewHire = findByFullName("SomeName");
possibleFoundNewHire.ifPresent(newHire -> {
  System.out.println(newHire); // Or formatted as you would like.
});

1 Comment

We have not been taught Optional as of yet (that comes later), so I wouldn't be able to use it for now :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.