String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
I have a simple array of planet names and I want to reverse each name that was added to the array (not the string order).
I continue to struggle with lambda syntax:
Arrays.sort(planets, (first, second) -> new StringBuilder(first).reverse().toString());
System.out.println(Arrays.toString(planets));
Currently I get:
Multiple markers at this line
- The method
reverse()
in the typeStringBuilder
is not applicable for the arguments(String)
- Type mismatch: cannot convert from
String
toint
Response:
Thank you all for suggestions... asList got me close. I still didn't sort correctly.
So i need to find a way to make array.sort work because this was the assignment (to take array.sort and lambda it).
I have pinged the instructor to ask for clarification here, but i am running out of time.
Here is my work attempting to keep array.sort
// lambda expression , long to short
System.out.println("5) Sorted by length (descending):");
Arrays.sort(planets, (first, second) -> second.length() - first.length());
System.out.println(Arrays.toString(planets));
// lambda expression , reverse the name , then sort in ascending order
// hint use new StringBuilder(first).reverse().toString() to reverse the string
System.out.println("6) Sorted in dictionary order of the reversed name (ascending)");
Arrays.sort(planets, (first, second) -> new StringBuilder(first).reverse().toString().compareTo(second));
System.out.println(Arrays.toString(planets));
// lambda expression , reverse the name , then sort in descending order
System.out.println("7) Sorted in dictionary order of the reversed name (descending)");
Arrays.sort(planets, (first, second) -> new StringBuilder(second).reverse().toString().compareTo(first));
System.out.println(Arrays.toString(planets));
What I cannot for the life of me figure out here is that all 3 expressions get me exactly the same result... each of those gives me the original array sorted longest to shortest.
This is my result:
5) Sorted by length (descending): [Neptune, Mercury, Jupiter, Uranus, Saturn, Venus, Earth, Mars]
6) Sorted in dictionary order of the reversed name (ascending) [Neptune, Mercury, Jupiter, Uranus, Saturn, Venus, Earth, Mars]
7) Sorted in dictionary order of the reversed name (descending) [Neptune, Mercury, Jupiter, Uranus, Saturn, Venus, Earth, Mars]
By Using StringBuilder StringBuilder or StringBuffer class has an in-build method reverse() to reverse the characters in the string. This method replaces the sequence of the characters in reverse order. The reverse method is the static method that has the logic to reverse a string in Java.
String reversed = str. chars() . mapToObj(c -> (char)c) . reduce("", (s,c) -> c+s, (s1,s2) -> s2+s1);
The lambda function assigned to full_name takes two arguments and returns a string interpolating the two parameters first and last .
To reverse a string in java, we can first convert it to StringBuilder which is nothing but a mutable string. Class StringBuilder provides an inbuilt function called reverse(), which reverses the given string and provides you with the final output.
An alternative solution which does not use Stream API and updates the strings in-place:
Arrays.asList(planets).replaceAll(s -> new StringBuilder(s).reverse().toString());
Demo:
String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune" };
Arrays.asList(planets).replaceAll(s -> new StringBuilder(s).reverse().toString());
System.out.println(Arrays.toString(planets));
// [yrucreM, suneV, htraE, sraM, retipuJ, nrutaS, sunarU, enutpeN]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With