Skip to content
Advertisement

Tag: generics

incompatible types: int cannot be converted to T

I’m getting this error, here is the necessary code. Assume the methods not here work correctly. Any help would be awesome. (I am coding on a text file in Ubuntu btw) Here is the error message: (after compiling) BinarySearchTree.java:132: error: incompatible types: int cannot be converted to T insert(y); ^ where T is a type-variable: T extends Comparable<? super T>

Java generics: Map nested json response to Java objects

Scenario: I’m working with an unusual external API in which every attribute is a map with multiple values. In order to convert this response into simple Java objects, I had to do some dirty unboxing. Below is one of the typical java class. As you can see how I’m unboxing data from response and mapping them to my java class:

Why is this lambda expression cast using an ampersand?

recently I stumbled over the following code in the Java Comparator class: What’s confusing me is the (Comparator<T> & Serializable) part. Since the method only returns a Comparator I don’t see the use in casting to Serializable. I also don’t see the reason to ever cast anything this way, or am I missing something? It seems to me that if

Wildcard not allowed for Iterable in Java

I have following code setup. But this is not allowed as wildcard is not allowed for Iterable. What actually Java tries to a achieve here by not allowing this? What modifications would get this working? Answer When a class instance is created, it invokes the initializer of the super type. But you can’t use wildcards for instance creation, hence your

When building containers why is using Java Generics better than using the Object Class? (Java Generics & DataStructures)

So I have been reviewing my data structures and came across an interesting thought regarding Java generics and the Object class. I have implemented and run a “generic bag” in two different ways (Notice below: IObjectBag.java, ObjectBag.java, IGenericBag.java, and GenericBag.java) and have used them both (Notice: Below main.java and Output). I have removed some of the unnecessary code as per

Mockito thenReturn returns null when coming from generic function

I want to mock the ResultSet in a class, whose parameters come from a generic return function (getSQLValue): The table class with the signature method: And this is the unit test class: The problem is that the ResultSet is always null, I can confirm when debugging. Answer Turns out that Mockito, after “accepting” the first argument as Integer, fails to

Advertisement