IntStream Error

Java 8 has a new interface called IntStream . I used the static of() method and found a strange error:

This static IntStream interface IntStream is only available as IntStream.of

But, as you can see in the following code, I definitely used IntStream.of

 import java.util.stream.IntStream; public class Test { public static void main(String[] args) { int[] listOfNumbers = {5,4,13,7,7,8,9,10,5,92,11,3,4,2,1}; System.out.println(IntStream.of(listOfNumbers).sum()); } } 

Also, if you check the API , you will see that the method was declared in the same way that I used.

+6
source share
2 answers

Although IntStream.of (int ...) seems to work, it is more likely that you should use Arrays.stream (int []) .

 public void test() { int[] listOfNumbers = {5, 4, 13, 7, 7, 8, 9, 10, 5, 92, 11, 3, 4, 2, 1}; // Works fine but is really designed for ints instead of int[]s. System.out.println(IntStream.of(listOfNumbers).sum()); // Expected use. System.out.println(IntStream.of(5, 4, 13, 7, 7, 8, 9, 10, 5, 92, 11, 3, 4, 2, 1).sum()); // Probably a better approach for an int[]. System.out.println(Arrays.stream(listOfNumbers).sum()); } 
+2
source

You need to configure the project to use Java 8. For example, if you use maven, put the following snippet in your pom:

  <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </pluginManagement> </build> 
+5
source

Source: https://habr.com/ru/post/973961/


All Articles