Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to convert char[] to stream in java 8 [duplicate]

I am writing a program in which a method takes char[][] as input and returns char[]. Method is as 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;
}


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 method getTableFromTwoChits() is compiling fine, whereas line 3 of main method is not compiling.
Please explain what is the reason behind such behaviour?

Compilation error is mentioned as 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
like image 776
ash164 Avatar asked Jan 01 '18 12:01

ash164


People also ask

Can we use stream with char array in Java?

Java doesn't provide char stream for primitive char. Therefore, we can't apply Arrays. stream() method to a char[] array. However, we can get an IntStream (a stream of ints) of characters and use that to generate Stream<Characters> .

How do I find duplicates in a list of strings in Java 8?

In Java 8 Stream, filter with Set. Add() is the fastest algorithm to find duplicate elements, because it loops only one time. Set<T> items = new HashSet<>(); return list.

Can you convert an array to stream?

stream() A good way to turn an array into a stream is to use the Arrays class' stream() method. This works the same for both primitive types and objects.

What is the function used to duplicate a stream?

CopyTo(Stream) Reads the bytes from the current stream and writes them to another stream. Both streams positions are advanced by the number of bytes copied.


2 Answers

In

Arrays.stream(inputTwoChits)

you are passing a char[][] array to <T> Stream<T> stream(T[] array), which is fine, since char[] is a reference type.

On the other hand, in

Arrays.stream(outputTwoChits)

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

You can produce an IntStream from a char[] using:

new String(outputTwoChits).chars()
like image 91
Eran Avatar answered Oct 08 '22 19:10

Eran


You cannot create a CharStream with Arrays.stream(outputTwoChits) as there is no overload in the Arrays class that takes a char[] and returns a CharStream (infact there is no CharStream at all) and doing Stream.of(outputTwoChits) would not provide what you'd expect in this particular case.

However, since you're only doing this for printing purposes you can do something like:

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

or construct a String object from the char array and call the chars method on it which should produce an IntStream and then project from int to char prior to printing to the console.

new String(outputTwoChits).chars().forEach(c -> System.out.println((char)c));
like image 27
Ousmane D. Avatar answered Oct 08 '22 18:10

Ousmane D.