I already read this related post. When it comes to String operations, streams seem to attract a huge amount of ceremony. If you want to parse a String as a stream of chars on which you might want to do some operations, you need to convert them to an IntStream
first, map to Object
, then cast the int
to char
, eventually casting the char
to String
and then return it.
And people say imperative style programming has a ceremony overhead. Please correct me if I am completely doing this wrong. My intention is not to mock around but to understand Java streams better because I generally appreciate them.
// Simple method which encrypts all chars in a string
String input = "Hel!lo";
String result = input.chars() // Need to convert into an IntStream
.mapToObj(e -> Character.toUpperCase((char) e)) // Need to map to Object (!) and then cast to char
.map(CryptoMath::encryptChar) // Calling the encryption
.map(String::valueOf) // Need to cast to String again...
.collect(joining("")); // Finally done
System.out.println(result);