我的输入是 n 个字符串。我想获取唯一值,以及这些不区分大小写的字符串的出现次数。
我想到了获取数组中的输入;对其进行排序并进行循环以计算出现次数。还有别的办法吗?
最佳答案
您可以使用 Stream api 工具来获取您想要的内容:
List<String> list = Arrays.asList("hello","world","Hola","Mundo","hello", "world","Hola","Mundo","mundo","Hello","Hola","mundo","Mundo");
Map<String, Long> ocurrences = list
.stream()
.map(String::toLowerCase) // make case insensitive
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(ocurrences);
输出:
{world=2, mundo=5, hello=3, hola=3}
关于Java 字符串数组最大最小唯一出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669180/