是否可以将Set转换为List?

时间:2018-11-27 02:52:24

标签: java list set

我有一个应该返回列表的函数。该函数在解析文件并识别拼写错误的单词时,建议使用不同的单词。我必须返回一个列表。但是,当我这样做时,我最终会得到一遍又一遍的建议。我意识到在集合中存储“建议”字词会解决此问题(这样,我只会存储任何给定建议的一个实例)。唯一的问题是我不能返回该Set,因为返回类型是一个列表(同样,我不能更改它)。有没有解决这个问题的方法?

我将提供以下功能。

public static List<String> getSuggestions(String word){
    List<String> letters = Arrays.asList("a", "b", "c", "d", "e", "f", "g",
                                       "h", "i", "j", "k", "l", "m", "n",
                                       "o", "p", "q", "r", "s", "t", "u",
                                       "v", "w", "x", "y", "z"); 
    Set<String> suggestions = new HashSet();
    StringBuilder builder = new StringBuilder(word); 
    for(int i = 0; i <= builder.length(); i++){
        for(String string: letters){
            StringBuilder suggestion = new StringBuilder(builder.toString());
            suggestion.insert(i, string); 
            if(dictionary.contains(suggestion.toString().toLowerCase())){
                suggestions.add(suggestion.toString());
            }
        }
    }
    for(int i = 0; i <= builder.length()-2; i++){
        for(String string: letters){
            StringBuilder suggestion = new StringBuilder(builder.toString());
            char one = suggestion.charAt(i + 1);
            char two = suggestion.charAt(i);
            suggestion.replace(i, i + 1, String.valueOf(one));
            suggestion.replace(i+1, i + 2, String.valueOf(two));
            if(dictionary.contains(suggestion.toString().toLowerCase())){
                suggestions.add(suggestion.toString());
            }
        }
    }
    for(int i = 0; i <= builder.length(); i++){
        for(String string: letters){
            StringBuilder suggestion = new StringBuilder(builder.toString());
            suggestion.replace(i, i + 1, "");
            if(dictionary.contains(suggestion.toString().toLowerCase())){
                suggestions.add(suggestion.toString());
            }
        }
   }
   return suggestions;
}

1 个答案:

答案 0 :(得分:2)

假设您希望一切按顺序更改

def build_train_fn(self):
    """ Train function for the Policy Network.

    This function replaces model.fit(X, y).
    """

    with self.graph.as_default():

        K.set_session(self.session)

        action_prob_placeholder = self.model.output
        action_onehot_placeholder = K.placeholder(shape=(None, ACTIONS), name="action_onehot")
        discount_reward_placeholder = K.placeholder(shape=(None,), name="discount_reward")

        action_prob = K.sum(action_prob_placeholder * action_onehot_placeholder, axis=1)
        log_action_prob = K.log(action_prob)

        loss = - log_action_prob * discount_reward_placeholder
        loss = K.mean(loss)

        adam = optimizers.Adam()

        updates = adam.get_updates(params=self.model.trainable_weights,
                                   loss=loss)

        self.train_fn = K.function(inputs=[self.model.input,
                                           action_onehot_placeholder,
                                           discount_reward_placeholder],
                                   outputs=[],
                                   updates=updates)

Set<String> suggestions = new HashSet();

然后从中更改最终的Set<String> suggestions = new LinkedHashSet<>();

return

return suggestions;