Intro to computer science in java question please help! 7. Consider this class:
ID: 3569294 • Letter: I
Question
Intro to computer science in java question please help!
7. Consider this class: Write a method called buildlndex that takes an ArrayList 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. 8. Consider the Person class from the previous question. Write a method called reverselndex 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 buildlndex 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, reverselndex 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 you 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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.