Java的内置函数Collections.frequency(list,element)的复杂性是什么?

时间:2018-02-11 17:55:02

标签: collections complexity-theory frequency

//下面的代码用于String的ArrayList。我想知道Collections.frequency()函数的强制性是什么。

    ArrayList<String> list = new ArrayList<>();
    list.add("sample");
    list.add("sample1");
    list.add("sample");
    list.add("sample");
    list.add("sample");
    list.add("sample");
    System.out.println("sample is repeated : " + Collections.frequency(list, "sample"));

1 个答案:

答案 0 :(得分:2)

public static int frequency(Collection<?> c, Object o) { int result = 0; if (o == null) { for (Object e : c) if (e == null) result++; } else { for (Object e : c) if (o.equals(e)) result++; } return result; } 具有以下实现(在Java 9中):

O(n)

所以它是<p class="stock <?php echo esc_attr( $class ); ?>"><?php echo wp_kses_post( $availability ); ?></p>