📌  相关文章
📜  Java中的 LongAccumulator longValue() 方法及示例

📅  最后修改于: 2022-05-13 01:55:27.711000             🧑  作者: Mango

Java中的 LongAccumulator longValue() 方法及示例

Java.LongAccumulator.longValue()是Java中的一个内置方法,它等效于get() 方法,该方法只返回当前值。

句法:

public long longValue()

参数:此方法不接受任何参数。

返回值:该方法返回当前值。

下面的程序说明了上述方法:

程序 1

// Program to demonstrate the longValue() method
  
import java.lang.*;
import java.util.concurrent.atomic.LongAccumulator;
  
public class GFG {
    public static void main(String args[])
    {
        LongAccumulator num = new LongAccumulator(Long::sum, 0L);
  
        // accumulate operation on num
        num.accumulate(42);
        num.accumulate(10);
  
        // longValues current value
        num.longValue();
  
        // Print after longValue operation
        System.out.println(" the current value is: " + num);
    }
}
输出:
the current value is: 52

方案二

// Program to demonstrate the longValue() method
  
import java.lang.*;
import java.util.concurrent.atomic.LongAccumulator;
  
public class GFG {
    public static void main(String args[])
    {
        LongAccumulator num = new LongAccumulator(Long::sum, 0L);
  
        // accumulate operation on num
        num.accumulate(5);
        num.accumulate(10);
  
        // longValues current value
        num.longValue();
  
        // Print after longValue operation
        System.out.println(" the current value is: " + num);
    }
}
输出:
the current value is: 15

参考:https: Java/util/concurrent/atomic/LongAccumulator.html#longValue–