Why is Stream.sorted not type-safe in Java 8?

2018-11-09T11:40:04

This is from the Stream interface from Oracle's implementation of JDK 8:

public interface Stream<T> extends BaseStream<T, Stream<T>> {
    Stream<T> sorted();
} 

and it is very easy to blow this up at run time and no warning will be generated at compile time. Here is an example:

class Foo {
    public static void main(String[] args) {
        Arrays.asList(new Foo(), new Foo()).stream().sorted().forEach(f -> {});
    }
}

which will compile just fine but will throw an exception at run time:

Exception in thread "main" java.lang.ClassCastException: Foo cannot be cast to java.lang.Comparable

What could be the reason that the sorted method was not defined where the compiler could actually catch such problems? Maybe I am wrong but isn't it this simple:

interface Stream<T> {
    <C extends Comparable<T>> void sorted(C c);
}

?

Obviously the guys implementing this (who are light years ahead of me as far as programming and engineering is considered) must have a very good reason that I am unable to see, but what is that reason?

Copyright License:
Author:「Koray Tugay」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/53219523/why-is-stream-sorted-not-type-safe-in-java-8

About “Why is Stream.sorted not type-safe in Java 8?” questions

This is from the Stream interface from Oracle's implementation of JDK 8: public interface Stream&lt;T&gt; extends BaseStream&lt;T, Stream&lt;T&gt;&gt; { Stream&lt;T&gt; sorted(); } and it
I've done some testing with Streams in special with DirectoryStreams of the nio-package. I simply try to get a list of all files in a directory sorted by last modified date and size. The JavaDoc o...
Java Streams sport both sorted and limit methods, which respectively return a sorted version of a stream and return a stream just returning a specified number of items of a stream. When these opera...
I want to determine if a list is anagram or not using Java 8. Example input: "cat", "cta", "act", "atc", "tac", "tca" I have written the following function that does th
I read that someone claims JSON is type-safe. I know that plain Javascript is not. But how could a representation format like JSON can be type-safe ?
I'm currently using JDOM for doing some simple XML parsing, and it seems like nothing's type safe - I had a similar issue with using the built-in Java DOM parser, just with lots more API to wade th...
What is type-safe? What does it mean and why is it important?
I am trying to use Java reflection to replace the value for an object's field at runtime, but in a type-safe way. Let's assume we have the following object. import cats.Eval object Foo { val b...
Is any practical way to reference a method on a class in a type-safe manner? A basic example is if I wanted to create something like the following utility function: public Result validateField(Obj...
Is any practical way to reference a method on a class in a type-safe manner? A basic example is if I wanted to create something like the following utility function: public Result validateField(Obj...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.