Tagged Questions
0
votes
1answer
33 views
Trouble with generics, comparators, to sort Map
The original purpose is to retrieve a sorted-by-value list of items in a HashMap.
The rough code (names just simplified):
public abstract class Thing<T> implements Iface<T> {
...
-5
votes
3answers
35 views
What kind of values we can add to List<B> type? [on hold]
Class A{
public static void main(String args[])
List<B> list=new ArrayList<B>();
list.add("what type of value should we add here");
}
Class B{
int a;
boolean b;
...
0
votes
4answers
37 views
Why we have to mention generic type parameter before the method return type?
Here on this page, it has the below code example to introduce the generic methods?
public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
return ...
4
votes
2answers
50 views
Varargs of type Class in Java
If I, for example, have a method that uses varargs for Class types that extends a super class like
this:
public static <E extends Example> void test(Class<E>... es){}
Then I try to ...
0
votes
2answers
34 views
Anyway to pass a generic type in a constructor to a parent class constructor?
Is there anyway to get a generic type in a class constructor in order to pass it to a parent constructor?
Given base class:
public class BaseSupport<T>{
private Class<T> type;
...
0
votes
0answers
48 views
How to use generics in this situation? [on hold]
I have a project with WebService but I will change the response in an only object, however the principal problem is how do I keep the value of every object?.
I have 3 objects:
Cliente
Product
...
-1
votes
2answers
64 views
Generics, need laymen explanation?
I came across the following snippet of code that uses generics.
public class Generics<T> {
public static <T> T replaceIfNull(T objectToCheck, T defaultValue) {
return ...
1
vote
2answers
49 views
Generics overloading of method - workaround
this question was asked many times but I couldn't find elegant workaround for it.
This example works as desired:
public class RequestWrapper<T> {
private final T request;
private final ...
0
votes
3answers
43 views
What does ArrayList without any generic type set mean and do?
I noticed that it's possible to define a variable like this, without specifying it's generic type.
While this seems to compile, I noticed some strange issues when I did it to more complicated classes, ...
1
vote
1answer
33 views
Cyclomatic Generics Code Design
Trying to speak abstract:
I try to design a generic pattern for a network of Components and directed Connectors. All Componenents are connected by Connectors.
So, with no loss of generality, imagining ...
4
votes
2answers
45 views
nested wild cards and subtyping. How to understand when types are compatible?
Usually when generics are explains say something like this:
List<?> list = new ArrayList<?>();
This code(above) produce error because compiler doesn't know which type to instantiate.
...
4
votes
1answer
26 views
Java Generics: unbounded wildcard generalization not working past second level [duplicate]
I guess this is a very confusing title, but I don't know what else to call - probably answered somewhere as well, but I couldn't find anything. Take this example:
List<Class<? extends ...
-1
votes
1answer
16 views
Using an iterator for generic type on a custom class
import java.util.*;
public class MyClass<Item> implements Iterable<Item> {
public Iterator<Item> iterator(){ return new ListIterator();}
public class ListIterator ...
0
votes
1answer
22 views
Implementing a Generic Method using Cast and Generics
I read the posts:
- Very simple Java Dynamic Casting
- java: how can i do dynamic casting of a variable from one type to another?
But it did not answer exactly what I was looking for.
I need to ...
0
votes
2answers
44 views
How to instantiate generics using wild card?
Lets research some generic instantion situation using wild card:
1
This code
List<?> list = new ArrayList<?>();
generates following error:
required: class or interface without bounds
...
6
votes
3answers
114 views
difference between creation unbounded and bounded wild card type array?
Why is this code valid
ArrayList<?>[] arr = new ArrayList<?>[2];
but the following two are not?
ArrayList<? extends Object>[] arr = new ArrayList<? extends Object>[2];
...
0
votes
0answers
25 views
Instantiate Generic Type with params [duplicate]
I have several types "Point". there is general class Point and several inheritants of it:
NEPoint extends Point
I have a generic Reader that reads points from some kind of Reader.
public class ...
1
vote
2answers
32 views
How to return Generic type from same method for parent and child classes
Here is my scenario
I've 3 classes.
class Animal {
public getWeight(){ ..... }
public getHeight(){ ..... }
}
class Dog extends Animal {
public getDogType() { ...}
}
class Cat extends ...
1
vote
1answer
28 views
Restrict generic Java input to one of two types
I have two different classes, Stack<E> and Queue<E>, which have methods having the exact same names (void add(E item), E remove(), boolean isFull() & boolean isEmpty()).
I'm looking ...
9
votes
2answers
106 views
A generics confusion: deceiving the compiler
Consider a code:
public class GenericsConfusion {
public static <T> Class<T> get(Class<T> clazz) {
Map<Class, Class> map = new HashMap<Class, Class>();
...
0
votes
2answers
67 views
Generics: What is the difference between ?, Object, and raw type of a single instance?
I know the difference between a Collection<?>, Collection<Object> and Collection.
The first takes any type only, the second must allow all Objects, and the third is unchecked.
But for a ...
1
vote
1answer
31 views
What's the benefit of wildcard generics in Java? [duplicate]
What benefit gives us the second signature method with wildcard <?>?
public static <T> void firstCheckList(List<T> myList, T obj)
public static <T> void ...
2
votes
2answers
44 views
Guava TypeToken and generic classes
I'm using Guava TypeToken class in my project, but I'm getting an unexpected result.
I have MyGenericClass<T>:
public class MyGenericClass<T> implements MyInterface {
private ...
0
votes
0answers
27 views
WeakReferences in a HashMap
I want to store a HashMap of Callbacks. These callbacks are generics. For some reasons the garbage collector can and should remove those instances. The keys of this map should be the type of the ...
2
votes
1answer
35 views
Lambda: Declare Generic Type Inline
My Goal
I want to write a generic timing function (I removed the actual timing code for this question as it doesn't really matter).
I have a function with unknown input and output. I want to run ...
1
vote
1answer
17 views
Spring AOP - using joinpoint to get generic class type (JAVA)
I am trying to obtain the generic type of a class using the JoinPoint in Spring AOP. But there doesnt seem to be a way to do that.
This is the class whose methods I am intercepting.
public abstract ...
0
votes
0answers
47 views
How to fixes raw types warning in this code? [on hold]
I'm creating a new project using cascading (a library for hadoop abstraction), and following the example of Impatient. But my IDE throws many warning, because of raw types warning. First I search how ...
0
votes
1answer
13 views
The method searchingStacks(Stack<E>, E) in the type SearchStack<E> is not applicable for the arguments (Stack<String>, String)
I am trying to create a generic method to search a stack for element "e" using a queue and return the stack to its original statues .
but i keep getting the error "The method searchingStacks(Stack, ...
0
votes
2answers
22 views
generic compareTo sorting different object classes
I am trying to use compareTo to return the largest or maximum of various types of objects. This works for integers, strings, but I am having when I try to include circles...
How can I get one generic ...
0
votes
1answer
26 views
Injecting list of generics in Spring 4.1
The following issue was encountered while upgrading Spring 3.2 -> 4.1
There is a Metadata hierarchy, like: AMetadata extends Metadata,
BMetadata extends Metadata etc.
There is a Processor ...
0
votes
1answer
38 views
generic type inside parameter
i have the following:
public <T extends Node> T create(Class<T> classType, String id, Transaction t){
T obj;
try {
NodeKey nodeKey = new NodeKey(classType, id);
...
13
votes
7answers
179 views
+50
Encapsulate class-specific and method-specific generics in one type
Let's say I need a class that is bounded to a generic Comparable type:
class A<T extends Comparable<T>> {
// this is just an example of usage of T type
List<T> comparables;
...
1
vote
2answers
66 views
Passing parameters in generically implement methods in hashmap
I am following this question
How to generically implement calling methods stored in a HashMap?
I am trying to pass parameters while calling executeCommand
function
Example code is as follows,
...
2
votes
2answers
66 views
Stuck with Java Generic Classes
I have an assignment and I'm stuck. The assignment is to write a generic class for this method:
public static void main(String[] args) {
ValueStore<Object> myStore1 = new ...
1
vote
3answers
41 views
Is it possible to create a 2D jagged array of a generic type in Java?
Is it possible to create an two-dimensional array of a generic type parameter with only a size specified in Java?
To illustrate, can we create an array like this:
T[][] array = new T[x][];
1
vote
3answers
54 views
Cannot pass ArrayList<Impl> to method taking ArrayList<Interface>
I have an interface and a class implementing it:
public interface IFoo{
Integer bar();
}
public class Foo implements IFoo{
@Override
public Integer bar(){
return 42;
}
}
...
0
votes
1answer
32 views
Anonymous Inner classes inside generics
I have created an ArrayList of 'T' :
ArrayList<T> al = new ArrayList<>();
However, I want to override how the T's inside this ArrayList are compared (e.g. by using anonymous inner ...
2
votes
2answers
31 views
Java generics incompatible types compilation error for generic static method call in Oracle Java SE 8u20 JDK
When compiling the code below with the Oracle Java SE 8u20 JDK, the first three assignments compile fine (for the works* variables), but the fourth assignment (for the fails1 variable) generates the ...
0
votes
0answers
16 views
Upper bounds wildcard with Predicate [duplicate]
I'm using a Predicate with a bounded type parameter:
// test for odd integer value
Predicate<? extends Number> p = num-> num.intValue() % 2 != 0;
Integer i = 23;
if(p.test(i)) {
// do ...
-1
votes
1answer
33 views
Generic return types in java returns object
I have a method that is intended to allow consumers of my SDK to pull objects out of an internal store. These objects inherit from a base class that is a generic class. The true type of the object ...
0
votes
1answer
18 views
Using superclass type to filter templates removes modifications to static variable in superclass
In order to explain I'll use an example
//Superclass.java
public class Superclass
{
static String TypeInfo = "Superclass";
}
//Subclass.java
public class Subclass extends Superclass
{
...
0
votes
1answer
52 views
How to infer class from generic type in Java?
I'm having trouble with understanding how to call a generic entity service. The code below works but I need to maintain getEntityClass() on each bean which I believe is totally redundant.
public ...
-1
votes
0answers
11 views
Generic Stack and Non-recursive Towers of Hanoi
I'm trying to write a method to solve the Towers of hanoi puzzle without using any recursion methods and using Generic Stack, the following is the java file, It runs and asks for input, After I do the ...
0
votes
2answers
49 views
Get instance of generic class from an enum attribute in Java
I have an enum in my class which contains an Class attribute, as so:
public enum DatEnum {
CONSTANT_ONE(ClassOne),
CONSTANT_TWO(ClassTwo);
private Class<? extends UpperBoundClass> ...
0
votes
1answer
34 views
Issue with Exception type handling when not throwing them - wanting a more generic version of multi-catch
Sorry for the TL;DR, but I feel like it needs some explanation or it will be misunderstood.
I have a method that makes a call to (generally external) code which I expect to sometimes throw a ...
0
votes
3answers
48 views
Java - is an array of type Class<? extends MyType> possible?
I've just been diving into Java generics and have come across something puzzling. I have a List of Class objects. These Class objects are of classes that extend the class MyType. What I want to do is ...
0
votes
0answers
40 views
Adding to a Generic Collection [duplicate]
Why does the following code give compiler error?
List<? extends Map<String,Object>> multi = new ArrayList<HashMap<String,Object>>();
multi.add(new ...
0
votes
2answers
51 views
Getting “Not Applicable” compile error with generic type
I have the following classes:
public interface IEntity<T extends IEntity<T>>
{
T createNewEntity(IEntityCreator creator);
}
public interface IEntityCreator
{
<T extends ...
0
votes
1answer
45 views
Operand + for Number in Java
If I have two Object implement Number Interface, is it possible to add them together?
public static <T extends Number> T[] prefixSum(T [] arr,T zero){
int n = arr.length;
T [] arr2 = ...
-1
votes
0answers
20 views
How can I use Java to write an Http SDK that defines Http implementation at runtime
I am building an SDK for our API, and would like to avoid hard coding the use of a specific Http library, like Apache HttpClient, in our SDK code. All of the various libraries have similar types of ...