---
# System prepended metadata

title: IntStream、DoubleStream的用法
tags: [ocpjp]

---

# IntStream、DoubleStream的用法
**Question:**
```java=
//line n1
Double d = str.average().getAsDouble();
System.out.println("Average : "+d);
```
## Which should be inserted into line n1to print Average = 2.5?
A. IntStream str = Stream.of (1, 2, 3, 4);
**B. IntStream str = IntStream.of (1, 2, 3, 4);**
C. DoubleStream str = Stream.of (1.0, 2.0, 3.0, 4.0);
D. Stream str = Stream.of (1, 2, 3, 4);
- [x] **Answer: B**

:::info
IntStream、DoubleStream才可以直接使用average方法
:::

**IntStream寫法如下:**
```java=
public static void main(String[] args) {
    IntStream str = IntStream.of(1,2,3,4);
    Double d = str.average().getAsDouble();
    System.out.println("Average : "+d);
}
```
**DoubleStream寫法如下:**
```java=
public static void main(String[] args) {		
    DoubleStream str = DoubleStream.of (1.0, 2.0, 3.0, 4.0);
    Double d = str.average().getAsDouble();
    System.out.println("Average : "+d);
}
```
**Stream寫法如下:**
```java=
public static void main(String[] args) {		
    Stream<Double> str = Stream.of(1.0,2.0,3.0,4.0);
    Double d = str.mapToDouble(a->a).average().getAsDouble();
    System.out.println("Average : "+d);
}
```

###### tags: `ocpjp`

