I would like to reverse the order of a text string using Java Java API Java 8.
This is what I have tried, but it does not make the investment.
import java.util.Comparator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ReversedStrings {
public static String solution(String str) {
final Comparator<String> compareTo = String::compareTo;
return Stream.of(str).sorted(compareTo.reversed()).collect(Collectors.joining());
}
}
If you need a unit test I share the unit test code I use with JUnit
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class ReversedStringsTest {
@Test
public void sampleTests() {
assertEquals("dlrow", ReversedStrings.solution("world"));
}
}
Is there any way to invest a string using streams?