Latest Questions tagged Generics

Browse all latest questions tagged Generics

136
Votes
Published 14 Sep, 2022
Java - Java: Instanceof and Generics

Before I look through my generic data structure for a value's index, I'd like to see if it is even an instance of the type this has been parametrized to. But Eclipse complains when I do this: @Overr...

51
Votes
Published 16 Oct, 2022
Java - Delete numbers larger than x in an ArrayList

I'm a noob in java and right now I'm learning about generics. This code should delete any integer larger than five. I typed [10, 11, 12, 1], in theory, I should only get [3, 4, 6, 1]. But I'm getting...

112
Votes
Published 02 Sep, 2022
Java - How to create a custom deserializer in Jackson for a generic type?

Imagine the following scenario: class <T> Foo<T> { .... } class Bar { Foo<Something> foo; } I want to write a custom Jackson deserializer for Foo. In order to do that (fo...

146
Votes
Published 06 Sep, 2022
Java - What do constructor type arguments mean when placed *before* the type?

I've recently come across this unusual (to me) Java syntax...here's an example of it: List list = new <String, Long>ArrayList(); Notice the positioning of the <String, Long> type argume...

19
Votes
Published 04 Sep, 2022
Java - Enum.valueOf(Class<T> enumType, String name) question

I am trying to get around a compile error ("Bound mismatch: ...") relating to dynamic enum lookup. Basically I want to achieve something like this: String enumName = whatever.getEnumName(); Class&lt...

84
Votes
Published 06 Sep, 2022
Java - Why can the Monad interface not be declared in Java?

Before you start reading: This question is not about understanding monads, but it is about identifying the limitations of the Java type system which prevents the declaration of a Monad interface. I...

56
Votes
Published 06 Sep, 2022
Java - passing List<String> to method expecting List<Object>

In Page 112 of CHAPTER 5 GENERICS in the book - Effective Java , these sentences appears Just what is the difference between the raw type List and the parameterized type List&lt;Object&gt; ...Wh...

16
Votes
Published 06 Sep, 2022
Java - Type safety: Unchecked cast from Object to List

SqlOutParameter out = new SqlOutParameter(&quot;out_refcursor&quot;,OracleTypes.CURSOR ,new StudentRowMapper()); // some code.. MapSqlParameterSource parame...

279
Votes
Published 04 Sep, 2022
Java - How do I make the method return type generic?

Consider this example (typical in OOP books): I have an Animal class, where each Animal can have many friends. And subclasses like Dog, Duck, Mouse etc which add specific behavior like bark(), quac...

142
Votes
Published 02 Sep, 2022
Java - Java Generics (Wildcards)

I have a couple of questions about generic wildcards in Java: What is the difference between List&lt;? extends T&gt; and List&lt;? super T&gt;? What is a bounded wildcard and what is an unbounded wi...

51
Votes
Published 02 Sep, 2022
Java - Java Generics With Any Type <?>

I'm trying to write a method in Java that will be able to add a custom Key object to an array, or change an already existing key in the array if there is one. However, I can't seem to get it to work....

6
Votes
Published 26 Apr, 2022
React - TypeScript: remove key from type/subtraction type

I want to define a generic type ExcludeCart&lt;T&gt; that is essentially T but with a given key (in my case, cart) removed. So, for instance, ExcludeCart&lt;{foo: number, bar: string, cart: number}&gt...

34
Votes
Published 24 Aug, 2022
Java - Java comparing generic types

In Java, I wrote a Binary Search Tree class that adds nodes using recursion. Now I want to generalize it using Generics so I can learn more about them. public class GBinNode&lt;T&gt; { T item;...

55
Votes
Published 22 Aug, 2022
Java - Java generics incompatible types (no instance(s) of type variable(s) exist)

The question title may seem to be same as some other post but the content is different. So please don't mark it duplicate. Problem: I have the below class: public class SCDTO extends RDTO { privat...

43
Votes
Published 19 Aug, 2022
Java - Kotlin Error : 'public' function exposes its 'public/*package*/' return type argument

I am new to Kotlin and trying out to write some project using the language. I am using Java library and extending a class from the library in my project and I am seeing this error message. 'public' f...