简体   繁体   English

实现使用Java计算一个字符串数组中每个字符串存在次数的算法的最快方法

[英]the quickest way to Implement an algorithm to count how many times each string is present in an array of strings using Java

Just wondering what the quickest way to Implement an algorithm to count how many times each string is present in an array of strings using Java would be? 只是想知道实现一种算法以使用Java计算字符串数组中每个字符串存在多少次的最快方法是什么?

this is what i have tried and it works but im worried it might be "cheating" as it strays away from the question a bit: 这是我尝试过的方法,并且有效,但是我担心它可能会“作弊”,因为它偏离了这个问题:

{
    String[] stringArray = {"Random", "Words", "Here","Random", "Words", "Here","Random", "Words", "Here","Random", "Words", "Here"};

    List asList = Arrays.asList(stringArray);
    Set<String> mySet = new HashSet<>(asList);

    mySet.stream().forEach((s) -> {
        System.out.println(s + " " +Collections.frequency(asList,s));
    });
}

The easiest way is to use Map#merge() : 最简单的方法是使用Map#merge()

Map<String, Integer> m = new HashMap<>();
for (String s : array)
    m.merge(s, 1, Integer::sum);

after that, m will hold strings as keys and occurrences as values: 之后, m将把字符串作为键,将出现的值作为值:

m.forEach((k, v) -> System.out.format("%s occured %s time(s)\n", k, v));

By using Collectors in streams: 通过在流中使用收集器:

Arrays.stream(list).collect(Collectors.groupingBy(e -> e, Collectors.counting()))

So, if you have something like this: 因此,如果您有这样的事情:

        String[] list = new String[4];
        list[0] = "something";
        list[1] = "gfddfgdfg";
        list[2] = "something";
        list[3] = "somet444hing";
        System.out.println(Arrays.stream(list).collect(Collectors.groupingBy(e -> e, Collectors.counting())));

output will be: 输出将是:

{gfddfgdfg=1, something=2, somet444hing=1}

I would use the groupingBy to return a map of counts. 我将使用groupingBy返回计数图。

Map<String, Long> counts = Stream.of(array)
                           .collect(Collectors.groupingBy(w -> w, Collectors.counting()));

to print these as well you can do 也可以打印这些

Stream.of(array)
      .collect(Collectors.groupingBy(w -> w, Collectors.counting()))
      .forEach((k, v) -> System.out.println(k + " occurred " + v " times));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计算 position 中每种类型有多少件的优雅方法 - Elegant way to count how many pieces of each type are present in a position 有什么方法可以计算字符串递归方法在 Java 中运行的次数? - Is there any way I can count how many times a string recursive method runs in Java? 通过使用Java集合中的通配符返回字符串列表的最快方法 - Quickest way to return list of Strings by using wildcard from collection in Java 查找每个元素在数组java中的次数 - finding how many times each element is in the array java 如何使用Java中的count子方法和目标字符串对添加到列表数组的字符串进行计数? - How to count strings added to a list array using count sub method and a target string in Java? 在数组中存储和访问字符串的最快方法 - Quickest way of storing and accessing Strings in an array 比较Java中字符串的最快方法是什么? - What's the quickest way to compare strings in Java? 如何查看文本文件中存在多少次字符串数组中的单词 - How to see how many times words from string array are present in a text file 字符串和子字符串以及主字符串中存在多少次子字符串 - String and substring and how many times substring present in main string 有没有更有效的方法来计算每个连续字母在字符串中出现的次数? (爪哇) - Is there a more efficient way to count the number of times each consecutive letter appears in a string? (java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM