唯一想到的答案是递归或以某种方式将 List 转换为字符串,但给定一个字符串列表,例如
List<String> items = Arrays.asList("a,b,a,c,d,e,a".split("\\s*,\\s*"));
如何在不使用迭代的情况下找到任何类型列表中特定值的出现次数(我想您可以忽略库方法的工作原理),例如上面的 'a' 的值为 3。
是的,这是一道面试题:)
最佳答案
如何在不遍历集合的情况下检查每个元素?为了避免明确编码迭代可以使用 Collections.frequency()
其中:
Returns the number of elements in the specified collection equal to the specified object. More formally, returns the number of elements e in the collection such that (o == null ? e == null : o.equals(e)).
关于java - 返回匹配列表项的计数而不迭代(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16919710/