Failed to convert char [] to stream in java 8

I am writing a program in which a method takes char [] [] as input and returns char []. The method below:

private static char[] getTableFromTwoChits(char[][] inputTwoChits) { Map<Character, Character> map = new HashMap<>(); Arrays.stream(inputTwoChits).forEach(x -> map.put(x[0], x[1])); map.entrySet().forEach(System.out::println); char[] result = new char[inputTwoChits.length+1]; int index=0; char startPoint = inputTwoChits[0][0]; do { result[index] = startPoint;index++; startPoint = map.get(startPoint); }while(startPoint != inputTwoChits[0][0]); result[index] = startPoint; return result; } 


The main method is as follows:

 public static void main(String[] args) { char[][] inputTwoChits = {{'A','B'},{'C','D'},{'B','C'},{'E','F'},{'F','A'},{'D','E'}}; char[] outputTwoChits = getTableFromTwoChits(inputTwoChits); Arrays.stream(outputTwoChits).forEach(System.out::println); } 


Line 2 in the getTableFromTwoChits () method compiles fine, while line 3 of the main method does not compile.
Please explain what is the reason for this behavior?

A compilation error is mentioned below:

 /CircularTableTwoChits.java:21: error: no suitable method found for stream(char[]) Arrays.stream(outputTwoChits).forEach(System.out::println); ^ method Arrays.<T#1>stream(T#1[]) is not applicable (inference variable T#1 has incompatible bounds equality constraints: char upper bounds: Object) method Arrays.<T#2>stream(T#2[],int,int) is not applicable (cannot infer type-variable(s) T#2 (actual and formal argument lists differ in length)) method Arrays.stream(int[]) is not applicable (argument mismatch; char[] cannot be converted to int[]) method Arrays.stream(long[]) is not applicable (argument mismatch; char[] cannot be converted to long[]) method Arrays.stream(double[]) is not applicable (argument mismatch; char[] cannot be converted to double[]) where T#1,T#2 are type-variables: T#1 extends Object declared in method <T#1>stream(T#1[]) T#2 extends Object declared in method <T#2>stream(T#2[],int,int) Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 1 error 
+5
source share
5 answers

Character shell class

You are trying to create a char stream that is of type primitive. Try this code with the wrapper class Character . You will get the desired result.

+3
source

You cannot create a CharStream with Arrays.stream(outputTwoChits) since there is no overload in the Arrays class, takes a char[] and returns a CharStream (there is actually no CharStream at all), and doing Stream.of(outputTwoChits) will not provide what you expect in this particular case.

However, since you are only doing this for printing, you can do something like:

 Arrays.stream(new String(outputTwoChits).split("")).forEach(System.out::println); 

or create a String object from a char array and call the chars method on it, which should create an IntStream , and then run the project from int to char before printing to the console.

 new String(outputTwoChits).chars().forEach(c -> System.out.println((char)c)); 
+6
source

There are only four types of stream, for elements of type int , long , double and reference types (objects). So, since there is no char stream, Arrays.stream cannot be applied to the char[] array.

The canonical representation of the character stream is IntStream and acquires it without copying or side add-ons such as

 CharBuffer.wrap(charArray).chars().forEach(c -> System.out.println((char)c)); 

Since interpreting int values โ€‹โ€‹by default is an "integer", you need to use the lambda expression, casting it to char to interpret it as a "character".

If you don't need strings between characters, you can just print the entire array on one line using

 System.out.println(String.valueOf(charArray)); 
+6
source

IN

 Arrays.stream(inputTwoChits) 

you pass the char[][] array to the <T> Stream<T> stream(T[] array) , which is good since char[] is a reference type.

On the other hand in

 Arrays.stream(outputTwoChits) 

you are passing char[] , and char not a reference type, so it does not match the signature of <T> Stream<T> stream(T[] array) .

You can create an IntStream from char[] using:

 new String(outputTwoChits).chars() 
+3
source

You can use the static method IntStream.range() , which you can use as follows to create an index stream, and then use them to map the value in charArray to a Character -stream:

 Stream<Character> chars = IntStream.range(0, charArray.length) .mapToObj(i -> charArray[i]); 

The .mapToObj() method is .mapToObj() because we are working with IntStream and we want to convert a primitive unboxed stream to a streaming source stream.

+2
source

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


All Articles