从 Arraylist<String> 中获取恰好出现 3 次的字符串 [英] Get the Strings that occur exactly three times from Arraylist&lt;String&gt;

查看:17
本文介绍了从 Arraylist<String> 中获取恰好出现 3 次的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayList,其中包含一些重复的值和出现三次的元素,我想将那些出现三次的值专门收集到另一个 ArrayList 中,例如

I have an ArrayList which contains some values with duplicates and elements that occur thrice, I want to collect those values that occur thrice specifically into another ArrayList like

Arraylist<String> strings;   //contains all strings that are duplicates and that occur thrice

在这里,我只想获取在另一个数组列表中出现三次的字符串.

Here, I want to get only the Strings that occur thrice in another array list.

Arraylist<String> thrice;    //contains only elements that occur three times.

目前,我有一个 解决方案 用于处理重复项,但我不能扩展它来仅获取出现三次的字符串,请帮我找出来.

Currently, I have a solution for dealing with duplicates but I cannot extend this for only getting strings that occur thrice, this please help me to find out.

推荐答案

使用 Collections.frequency 为:

/**
 * @param input the list as your input
 * @param n number of occurrence (duplicates:2 , triplets:3 etc..)
 * @param <T> (type of elements)
 * @return elements with such conditional occurrent in a Set
 */
static <T> Set<T> findElementsWithNOccurrence(List<T> input, int n) {
    return input.stream()
            .filter(a -> Collections.frequency(input, a) == n) // filter by number of occurrences
            .collect(Collectors.toSet()); // collecting to a final set (one representation of each)
}

注意:这将是一种 O(n^2) 方法,因为它使用 Collections.frequency 再次遍历整个集合以获取频率.但是建议对您正在寻找的内容采用更具可读性和通用性的方法.此外,这有意将最终输出收集到 Set,因为毕竟 List 可以再次重复.

Note: This would be an O(n^2) approach since its using Collections.frequency which iterates over the entire collection again to get the frequency. But proposed for a more readable and generic approach towards what you're looking for. Also, this intentionally collects final output to a Set, since a List can again have duplicates after all.

或者,您可以使用该方法来计算 Java-8 中元素出现的频率 并迭代从而创建的 Map 根据需要处理过滤并在同一迭代中收集输出:

Alternatively, you can use the method to count the frequency of elements in Java-8 and iterate over the entries of the Map created thereby to process filtering as desired and collect the output in the same iteration :

/**
 * @param input the list as your input
 * @param n     number of occurrence (duplicates :2 , triplets :3 etc)
 * @param <T>   (type of elements)
 * @return elements in a set
 */
static <T> Set<T> findElementsWithNOccurrence(List<T> input, int n) {
    return input.stream() // Stream<T>
            .collect(Collectors.groupingBy(Function.identity(), 
                    Collectors.counting())) // Map<T, Long>
            .entrySet() // Set<Map.Entry<T,Long>>
            .stream() // Stream<Map.Entry<T,Long>>
            .filter(e -> e.getValue() == n) // filtered with frequency 'n'
            .map(Map.Entry::getKey) // Stream<T>
            .collect(Collectors.toSet()); // collect to Set
}

这篇关于从 Arraylist<String> 中获取恰好出现 3 次的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆