0

私はこれを持っています

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 の血縁者

私は型キャストを試しましたが、それが機能するとは思いませんでしたし、それも機能しませんでした。

4

1 に答える 1

1

私は次のようなものから始めます:

public String getRelatives(String person)
{        
    StringBuilder sb = new StringBuilder();
    sb.append(person);
    sb.append(" is related to ");
    for(String relative : map.get(person))
    {
        sb.append(relative);
        sb.append(' ');
    }
    return sb.toString();
}

または、もう少し複雑にして、誰かが誰ともうまく関係していない場合を処理したい場合は、次のようにします。

public String getRelatives(String person)
{   
    StringBuilder sb = new StringBuilder();
    sb.append(person);
    Set<String> relatives = map.get(person);
    if(relatives == null || relatives.isEmpty())
    {
        sb.append("is not related to anyone.");
    }
    else
    {
        sb.append(" is related to ");
        for(String relative : relatives)
        {
            sb.append(relative);
            sb.append(' ');
        }
    }
    return sb.toString();
}

マップが正しく初期化されていて、マップがマップされるセットがあれば、問題ありません。

基本的には を作成しStringBuilder(これはやり過ぎかもしれませんが、それでも良い習慣です)、必要なものを詰め込んでから、その.toString()メソッドを呼び出します。

for ループは、 の内容を反復処理しSet、StringBuilder に親族の名前を詰め込み、間隔を空けるためのスペース文字を挿入します。


その他の注意事項:

private Map<String,Set<String>> map;


public Relatives()
{
    map = new TreeMap<String,Set<String>>();
}

次のようにすることができます:

private Map<String, Set<String>> map = new TreeMap<String, Set<String>>();

または、Java 7 を使用している場合は、単純に次のようにします。

private Map<String, Set<String>> map = new TreeMap<>();

(この方法では、マップを初期化するだけの場合、明示的なコンストラクターは必要ないことに注意してください)

これも変更します:

if(map.containsKey(person))
{
    map.get(person).add(relative);
}
else
{
    Set<String> relatives = new TreeSet<String>();
    relatives.add(relative);
    map.put(person,relatives);
}

に:

if(!map.containsKey(person))
{
    map.put(person, new TreeSet<String>());
}
map.get(person).add(relative);

よりシンプルで冗長性を回避

于 2013-09-27T00:01:48.317 に答える