我有这个
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Scanner;
import static java.lang.System.*;
public class Relatives
{
private Map<String,Set<String>> map;
public Relatives()
{
map = new TreeMap<String,Set<String>>();
}
public void setPersonRelative(String line)
{
String[] personRelative = line.split(" ");
String person = personRelative[0];
String relative = personRelative[1];
if(map.containsKey(person))
{
map.get(person).add(relative);
}
else
{
Set<String> relatives = new TreeSet<String>();
relatives.add(relative);
map.put(person,relatives);
}
}
/**
* Returns the String version of the set containing person's relatives
* (see last line of sample output)
* @param person the person whose relative set should be returned as a String
* @param the string version of person's relative set
*/
public String getRelatives(String person)
{
return map.keySet();
}
我怎样才能将地图作为字符串返回并使其看起来像这样
Bob 与 John 有关 Tom
Dot 与 Chuck Fred Jason Tom
Elton 与 Linh 有关
我已经尝试过类型转换,尽管我认为它不会起作用并且解析它也不起作用,而这就是我目前所拥有的