Java中的ConcurrentHashMap size()方法

这个 JAVAutil。同时发生的ConcurrentHashMap。大小() 方法是Java中的一个内置函数,它计算该映射中键值映射的数量并返回整数值。

null

语法:

public int size()

返回值: 函数返回一个 整数 值,表示此映射中键值映射的数量。

下面的程序演示了 大小() 方法:

项目1:

// Java Program Demonstrate size()
// method of ConcurrentHashMap
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, String>
chm = new ConcurrentHashMap<Integer, String>();
chm.put( 100 , "Geeks" );
chm.put( 101 , "for" );
chm.put( 102 , "Geeks" );
// Display the number of
// key-value mappings
System.out.println( "The number of "
+ "key-value mappings is "
+ chm.size());
}
}


输出:

The number of key-value mappings is 3

项目2:

// Java Program Demonstrate size()
// method of ConcurrentHashMap
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentHashMap<Integer, Integer>
chm = new ConcurrentHashMap<Integer, Integer>();
chm.put( 1 , 100 );
chm.put( 2 , 200 );
chm.put( 3 , 300 );
chm.put( 4 , 400 );
chm.put( 5 , 500 );
chm.put( 6 , 600 );
// Display the number of
// key-value mappings
System.out.println( "The number of "
+ "key-value mappings is "
+ chm.size());
}
}


输出:

The number of key-value mappings is 6

参考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentHashMap.html#size()

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享