Are Java 8 threads just not intended for use with characters and strings?

I already read this related post. When it comes to string operations, the threads seem to attract a huge amount of ceremony. If you want to parse String as a stream of characters in which you can perform some operations, you first need to convert them to IntStream and then to an Object map, and then drop the int to char , eventually casting the char to String , and then return it .

And people say persistent style programming has overhead. Please correct me if I completely do it wrong. My intention is not to mock, but to better understand Java threads, 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); 
+6
source share
2 answers

If you can use Unicode code codes instead of characters, this gets a little less cumbersome than working on char :

 String input = "Hel!lo"; String result = input.codePoints() .map( Character::toUpperCase ) .collect( StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append ) .toString(); System.out.println(result); 

There is no need for boxing without converting to a string at the collection point, and you are unlikely to be able to trigger surrogate pairs in your input. One of those nice times is when it’s less painful to implement something that fits a wider range of inputs.

+3
source

With Eclipse Collections, you can complement the missing parts in the standard Java library. The following will work using the CharAdapter if CryptoMath.encryptChar() returns a char .

 String result = CharAdapter.adapt(input) .collectChar(Character::toUpperCase) .collectChar(CryptoMath::encryptChar) .makeString(""); 

Note. I am a committer for Eclipse collections.

+1
source

All Articles