java 方法 示例_Java Collectionsfrequency()方法与示例

java 方法 示例

集合类的frequency()方法 (Collections Class frequency() method)

  • frequency() method is available in java.util package.

    frequency()方法在java.util包中可用。

  • frequency() method is used to return the frequency of the given Object (obj) to the given Collection (co) or in other words, this method is used to return the number of elements in the given collection which is same as the given Object (obj).

    frequency()方法用于将给定Object(obj)的频率返回给定Collection(co),换句话说,该方法用于返回给定Collection中与给定Object相同的元素数(obj)。

  • frequency() method is a static method, so it is accessible with the class name and if we try to access the method with the class object then also we will not get an error.

    frequency()方法是一个静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,那么也不会收到错误。

  • frequency() method may throw an exception at the time of returning frequency.

    frequency()方法在返回频率时可能会引发异常。

    NullPointerException: This exception may throw when the given parameter Collection (co) if null exists.

    NullPointerException :如果给定参数Collection(co)存在null,则可能引发此异常。

Syntax:

句法:

    public static int frequency(Collection co, Object obj);

Parameter(s):

参数:

  • Collection co – represents the Collection object in which to calculate the frequency of the given Object (obj).

    Collection co –表示要在其中计算给定对象(obj)频率的Collection对象。

  • Object obj – represents the object whose frequency is to be calculated.

    对象obj –表示要计算其频率的对象。

Return value:

返回值:

The return type of this method is int, it returns the frequency of the given object with respect to the given collection.

此方法的返回类型为int ,它返回给定对象相对于给定集合的频率。

Example:

例:

// Java program is to demonstrate the example
// of int frequency() of Collections

import java.util.*;

public class Frequency {
    public static void main(String args[]) {
        // Instantiate a LinkedList   
        List link_l = new LinkedList();

        // By using add() method is to
        // add elements in linked list
        link_l.add(10);
        link_l.add(20);
        link_l.add(30);
        link_l.add(40);
        link_l.add(50);
        link_l.add(40);
        link_l.add(30);

        // Display LinkedList
        System.out.println("link_l: " + link_l);

        System.out.println();

        // By using frequency() method is to
        // return the object frequency of the
        // given collection linked ist
        int f1 = Collections.frequency(link_l, 40);
        int f2 = Collections.frequency(link_l, 30);

        // Display frequency
        System.out.println("Collections.frequency(link_l,40): " + f1);
        System.out.println("Collections.frequency(link_l,30): " + f2);
    }
}

Output

输出量

link_l: [10, 20, 30, 40, 50, 40, 30]

Collections.frequency(link_l,40): 2
Collections.frequency(link_l,30): 2


翻译自: https://www.includehelp.com/java/collections-frequency-method-with-example.aspx

java 方法 示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值