1

最初にコードを投稿します。

public static <T> ConcurrentMap<String,Collection<?>> reduceMap(ConcurrentMap<String, ConcurrentHashMap<String, Collection<T>>> map) {
    ConcurrentMap<String, Collection<?>> smallerMap = new ConcurrentHashMap<String, Collection<?>>();
    for (String material : map.keySet()) {
        for(String genre: map.get(material).keySet()) {
            if (smallerMap.get(genre) == null) {
                smallerMap.put(genre, map.get(material).get(genre));
            }
            else {
                Collection<?> stories = smallerMap.get(genre);
                for (Object o : map.get(material).get(genre)) {
                    if (!smallerMap.get(genre).contains(o)) {
                        stories.add(o); // error here
                    }
                }
                smallerMap.put(genre, stories);
            }
        }
    }   
    return smallerMap;
}

エラー: The method add(capture#5-of ?) in the type Collection<capture#5-of ?> is not applicable for the arguments (Object)

より大きなマップ コンテンツの例:

 (reading material -> genre -> stories)
 books -> fiction -> story1,story3
 books -> tale -> story2,story4
 novels-> fiction -> story2, story3
 novels-> tale - > story3

結果として得られる小さいマップには、次のものが必要です。

 fiction -> story1,story2,story3
 tales -> story2,story3,story4

大きなマップのコンテンツを小さなマップに結合しようとしています。2 つのマップの違いは、大きい方のマップには外側のインデックス (読み物の種類) があることです。小さい方のマップには、大きい方のマップ (ジャンル) の 2 番目のインデックスと同じ情報が含まれています。大きなマップの 2 番目のインデックスの内部にあるものを結合し、最初の外側のインデックスなしで小さなマップに配置できるようにしたいと考えています。一致するジャンルに非重複を追加したい。エラー行を通過できません。を に変更してみ?ましたT。他に何を試すべきかわかりません。

編集:

の戻り値の型を保持することは可能ですか??

4

2 に答える 2

3

タイプ セーフにするには、次のコードを使用する必要があります。

public static <T> ConcurrentMap<String,Collection<T>> reduceMap(ConcurrentMap<String, ConcurrentHashMap<String, Collection<T>>> map) {
    ConcurrentMap<String, Collection<T>> smallerMap = new ConcurrentHashMap<String, Collection<T>>();
    for (String material : map.keySet()) {
        for(String genre: map.get(material).keySet()) {
            if (smallerMap.get(genre) == null) {
                smallerMap.put(genre, map.get(material).get(genre));
            }
            else {
                Collection<T> stories = smallerMap.get(genre);
                for (T o : map.get(material).get(genre)) {
                    if (!smallerMap.get(genre).contains(o)) {
                        stories.add(o); // error here
                    }
                }
                smallerMap.put(genre, stories);
            }
        }
    }   
    return smallerMap;
}
于 2013-06-24T19:28:03.023 に答える