Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

7. Consider this class: Write a method called build Index that takes an Array Li

ID: 3569275 • Letter: 7

Question

7. Consider this class: Write a method called build Index that takes an Array List of person objects and constructs a map from strings (the person's name) to person objects, just like a phone book. The kind of loop you use is up to you. Consider the Person class from the previous question. Write a method called reverse Index that takes a map as a parameter. It's parameter is a mapping from names to person objects, just like the phone book map returned by build Index in the previous question. From this parameter, construct a new map that represents a reverse -lookup phone book, that allows you to look up phone numbers and find out who they belong to. In other words, reverse index should return a new map from strings to person objects that contains all the original person objects in the original map, but now using phone numbers as the keys instead of names.

Explanation / Answer

Here you go :)

Comment if u have any doubts.

//Person class

import java.util.*;

public class Person
{
   String name;
   String phoneNumber;
   public String getName()
   {
       return this.name;
   }
   public String getPhoneNumber()
   {
       return this.phoneNumber;
   }
  
   public Map buildIndex(ArrayList<Person> list)
   {
       Map<String,Person> map=new HashMap<String,Person>();
      
       for(int i=0;i<list.size();i++)
           map.put(list.get(i).getName(), list.get(i));
      
       return map;
   }
  
   public Map reverseIndex(Map<String,Person> map)
   {
       Map<String,Person> newMap=new HashMap<String,Person>();
      
       for(int i=0;i<map.size();i++)
       {
           newMap.put(map.get(i).getPhoneNumber(), map.get(i));
       }
       return newMap;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote