Find the most common String in ArrayList()

后端 未结 12 929
无人及你
无人及你 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();
    
    0 讨论(0)
  • 2020-12-08 17:46

    In statistics, this is called the "mode". A vanilla Java 8 solution looks like this:

    Stream.of("test","test","hello","test")
          .collect(Collectors.groupingBy(s -> s, Collectors.counting()))
          .entrySet()
          .stream()
          .max(Comparator.comparing(Entry::getValue))
          .ifPresent(System.out::println);
    

    Which yields:

    test=3
    

    jOOλ is a library that supports mode() on streams. The following program:

    System.out.println(
        Seq.of("test","test","hello","test")
           .mode()
    );
    

    Yields:

    Optional[test]
    

    (disclaimer: I work for the company behind jOOλ)

    0 讨论(0)
  • 2020-12-08 17:47

    If somebody need to find most popular from usual String[] array (using Lists):

    public String findPopular (String[] array) {
        List<String> list = Arrays.asList(array);
        Map<String, Integer> stringsCount = new HashMap<String, Integer>();
        for(String string: list)
        {
            if (string.length() > 0) {
                string = string.toLowerCase();
                Integer count = stringsCount.get(string);
                if(count == null) count = new Integer(0);
                count++;
                stringsCount.put(string,count);
            }
        }
        Map.Entry<String,Integer> mostRepeated = null;
        for(Map.Entry<String, Integer> e: stringsCount.entrySet())
        {
            if(mostRepeated == null || mostRepeated.getValue()<e.getValue())
                mostRepeated = e;
        }
        try {
            return mostRepeated.getKey();
        } catch (NullPointerException e) {
            System.out.println("Cannot find most popular value at the List. Maybe all strings are empty");
            return "";
        }
    
    }
    
    • case non-sensitive
    0 讨论(0)
  • 2020-12-08 17:50

    i know this takes more time to implement but you can use heap data structure by storing in the nodes the count and the string information

    0 讨论(0)
  • 2020-12-08 17:52

    There are a lot of answers suggesting HashMaps. I really don't like them, because you have to iterate through them once again anyway. Rather, I would sort the List

    Collections.sort(list);
    

    and then loop through it. Something similar to

    String prev = null, mostCommon=null;
    int num = 0, max = 0;
    for (String str:list) {
      if (str.equals(prev)) {
        num++;
      } else {
        if (num>max) {
          max = num;
          mostCommon = str;
        }
        num = 1;
        prev = str;
      }
    }
    

    should do it.

    0 讨论(0)
  • 2020-12-08 17:53

    You can use Guava's Multiset:

    ArrayList<String> names = ...
    
    // count names 
    HashMultiset<String> namesCounts = HashMultiset.create(names);
    Set<Multiset.Entry<String>> namesAndCounts = namesCounts.entrySet();
    
    // find one most common
    Multiset.Entry<String> maxNameByCount = Collections.max(namesAndCounts, Comparator.comparing(Multiset.Entry::getCount));
    
    // pick all with the same number of occurrences
    List<String> mostCommonNames = new ArrayList<>();
    for (Multiset.Entry<String> nameAndCount : namesAndCounts) {
        if (nameAndCount.getCount() == maxNameByCount.getCount()) {
            mostCommonNames.add(nameAndCount.getElement());
        }
    }
    
    0 讨论(0)
提交回复
热议问题