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

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?

Related

Kotlin : How to convert Array<List> to arrayof? [duplicate]

This question already has answers here:
Java ArrayList to Kotlin Array
(4 answers)
Closed 1 year ago.
So, I try to make a line chart using AAChartModel. I have ArrayList in my code. But in AAchartModel they used an arrayOf instead of ArrayList.
So My Question is, how do I convert from ArrayList to arrayOf ?
arrayOf() returns just an Array. So you can call arrayList.toTypedArray()

thenComparing breaks substring on String [duplicate]

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()

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>.

Java- Convert treeSet to List [duplicate]

This question already has answers here:
Converting a TreeSet to ArrayList?
(3 answers)
Closed 6 years ago.
how can I convert this to a List of the same object type?
TreeSet<FieldBean> Fields = new TreeSet<FieldBean>();
Thanks
One of the constructors of ArrayList takes a collection as an argument and fills the list with the items contained in that collection:
List<FieldBean> list = new ArrayList<FieldBean> (fields);

Categories

Resources