thenComparing breaks substring on String [duplicate] - java

This question already has answers here:
Java 8 Comparator comparing doesn't chain
(2 answers)
How do I get Comparator.comparing to correctly infer type parameters?
(2 answers)
Very confused by Java 8 Comparator type inference
(4 answers)
comparing and thenComparing gives compile error
(1 answer)
Comparator.reversed() does not compile using lambda
(4 answers)
Closed 3 years ago.
I have a list of Strings with two characters and want to sort by first character ascending and second character descending, for example "S1", "T1", "T2" should be sorted to "S1", "T2", "T1".
Seemed simple enough:
strings.sort(Comparator.comparing(code -> code.substring(0,1))
.thenComparing(code -> code.substring(1,2).reversed()));
After typing out the first part
strings.sort(Comparator.comparing(code -> code.substring(0,1))
Anything is still okay, autocomplete tells me about substring (Intellij), but when I add
.thenComparing(code -> code.substring(1,2).reversed()));
Somehow Java doesn't know anymore that it's working with Strings and gives me the error
Cannot resolve method 'substring' in 'Object'
for the .substring method calls on both.
Same happens when using stream().sorted()

Related

Is there any method to sort a string array in specific order in java which has alphanumeric elements in it [duplicate]

This question already has answers here:
Sorting arraylist in alphabetical order (case insensitive)
(10 answers)
Closed 11 months ago.
I have an array named "testArray" with elements: {"fdvsd","sd","Edit1648004502584","zxz","automatioion","acas","teg"}
Once I use Arrays.sort(testArray), the elements are sorted as below:
{"Edit1648004502584","acas","automatioion","fdvsd","sd","teg","zxz"}
But I need the array to be sorted as follows:
{"acas","automatioion","Edit1648004502584","fdvsd","sd","teg","zxz"}
Please help me out.
If I understand correctly the issue you are having is the capitalisation of the Strings, when you want it to ignore case. Try the following code:
Arrays.sort(testArray, String::compareToIgnoreCase);

Why I cannot cast raw collection elements in stream API? [duplicate]

This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Why does this java 8 stream operation evaluate to Object instead of List<Object> or just List?
(2 answers)
Closed 2 years ago.
Consider having 2 snippets
List<?> list=...
list.stream().map(Map.class::cast).map(m->m.get("whatever")). ... //compiles and works fine
List rawList=...
rawList.stream().map(Map.class::cast).map(m->m.get("whatever")). ... //does not compile
Second stream casting does not work as map returns Stream while in first example it returns Stream<Map>. Why is that?

Converting Object[] to List<Object> in this one-liner function in Java [duplicate]

This question already has answers here:
Converting Array to List
(9 answers)
Retrieving a List from a java.util.stream.Stream in Java 8
(15 answers)
Closed 3 years ago.
still very new to Java, my apologies if this has appeared before.
Basically here is the original code
public static MarketType[] convert(final String[] values) {
return ofNullable(values).map(v -> Stream.of(values))
.orElse(Stream.empty())
.map(v -> getMarketType(v))
.toArray(MarketType[]::new);
}
Since other functions changed, I really need the return type to be List<MarketType> instead of MarketType[], but is there any way that can achieve this with the minimal amount of modification for the original code?
I have been trying to put different things in the toArray function but nothing really worked.
Any help appreciated!

What different between the 2 statements?about how to use java 8 stream [duplicate]

This question already has answers here:
why does List<String>.toArray() return Object[] and not String[]? how to work around this?
(5 answers)
java: (String[])List.toArray() gives ClassCastException
(5 answers)
Closed 3 years ago.
I want to convert a Double list to double array, and tried 2 ways (line 2 and 3), but line 3 can not pass compilation and reports an error:
Non-static method can not referenced from a static context
by IDEA tips.
when compiled by maven it report:
Incompatible type: invalid method reference.
List<Double> res = new ArrayList<>();
double[] doubles = res.stream().mapToDouble(Double::doubleValue).toArray();
Arrays.stream(res.toArray()).mapToDouble(Double::doubleValue).toArray();
toArray() returns an Object[], so Arrays.stream(res.toArray()) returns a Stream<Object>.
You need to pass a Double[] to Arrays.stream() in order to get a Stream<Double>, which will allow you to map to double with Double::doubleValue:
Arrays.stream(res.toArray(new Double[res.size()])).mapToDouble(Double::doubleValue).toArray();
Your first Stream pipeline works, since res.stream() returns Stream<Double>.

String split() method not giving right result [duplicate]

This question already has answers here:
Java String.split() sometimes giving blank strings
(3 answers)
Why in Java 8 split sometimes removes empty strings at start of result array?
(3 answers)
Closed 8 years ago.
I have a string like "2020". When I split it with split(""). Then i checked its length. It is giving me 5 . But it should give 4. What is the reason

Categories

Resources