I am trying to build a generic merge method, part of a mergeSort algorithm but I am stuck on how to deal with the comparison. This is my code so far: public static Object[] merge(Object[] left, Object[…
Tag: generics
Is it possible to make a generic functional interface with optional parameter(s)?
I’m trying to create a functional interface in Java with a single method (of course) that can take any type of parameter(s) and return any data type (i.e. a generic method). This is what I have so far: Calculator.java Main.java The error says: bad operand types for binary operator ‘+’ Is it possible to create a generic functional interface with
Java enum with generic attributes
I have been trying to create enum that contains generic attribute, for example: SomeValue1 and SomeValue2 classes implement SomeValue interface. For some reason <T extends SomeValue> is marked with “Unexpected bound” error. If I replace T with ?, there is no error. But it is still puzzling me why it is happening when I use T. Any help would be
Does it matter if function returns T or object if T can not be inferred from context?
I found the following source code in gson: public
Casting Issue for Generic Array of Node Objects
I am having problem with java generic array creation where I needed to make an array of type Node. So, I did this for declaration: private Node<E> [] nodes; and later for initialization, nodes = (Node<E>[]) new Node [values.length]; When I try to do something like set the Node object’s attribute value nodes[i].setValue(values[i]); , I get NullPointerException, meaning that there
Java Generics – quick question on Greater Than Method with Objects
I have an assignment in my Java class, we are learning Generics, I have looked in my notes, my lessons, and even on the internet and I still cant figure out what this last assignment question is asking me to do, which is: Write a generic “greater-than” function that takes two objects as arguments, each of which has a “value”
How to call Scala curry functions from Java with Generics
A Scala code has a retry mechanism which is based on currying function: object RetryUtil { def retry[T](retry: Int, timeout: FiniteDuration)(exc: => T): T = { // } } I want to call this …
Java generics impossible assignment?
Every time I think I understand generics better (and can answer without compiling), I get to an example where this theory breaks. Here is a very simple example: And two invocations: To me, none of the invocations should compile. A String is not a supertype of List. Still, the second one compiles. But let’s suppose that this happens because the
How to create a generic wrapper for just any method call?
I want to create a helper method that can wrap/convert just any sync method call into an async Mono. The following is close, but shows an error: This is my code: Answer First you call Mono.fromCallable with a Callable<Callable<? extend T>>. You need to change the call like this: Mono.fromCallable(supplier). Then you will have a problem because Mono.fromCallable will be
What is the difference between “ T” and “T” return types in JavaDocs
Still wrapping my head around generics so help would be appreciated.