Find the most common String in ArrayList()

后端 未结 12 949
无人及你
无人及你 2020-12-08 16:59

Is there a way to find the most common String in an ArrayList?

ArrayList list = new ArrayList<>();
list.add(\"t         


        
12条回答
  •  长情又很酷
    2020-12-08 17:38

    As per question, Specifically just to get word, not the number of times (i.e. value of key).

    String mostRepeatedWord 
        = list.stream()
              .collect(Collectors.groupingBy(w -> w, Collectors.counting()))
              .entrySet()
              .stream()
              .max(Comparator.comparing(Entry::getValue))
              .get()
              .getKey();
    

提交回复
热议问题