Finding repeated words on a string and counting the repetitions

后端 未结 29 897
梦谈多话
梦谈多话 2021-02-05 17:49

I need to find repeated words on a string, and then count how many times they were repeated. So basically, if the input string is this:

String s = \"House, House         


        
29条回答
  •  南方客
    南方客 (楼主)
    2021-02-05 18:04

    Try this,

    public class DuplicateWordSearcher {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
    
        String text = "a r b k c d se f g a d f s s f d s ft gh f ws w f v x s g h d h j j k f sd j e wed a d f";
    
        List list = Arrays.asList(text.split(" "));
    
        Set uniqueWords = new HashSet(list);
        for (String word : uniqueWords) {
            System.out.println(word + ": " + Collections.frequency(list, word));
        }
    }
    

    }

提交回复
热议问题