我有一个泛型课,Foo<T>.在一个方法中Foo,我想得到类型为T的类实例,但我无法调用T.class.
使用它的首选方法是什么T.class?
我试图了解reified关键字的目的,显然它允许我们对泛型进行反思.
但是,当我把它放在外面时它的效果一样好.任何人都在关心何时产生实际差异?
我无法理解thenApply(和之间的区别thenCompose().
那么,有人可以提供有效的用例吗?
来自Java文档:
thenApply(Function<? super T,? extends U> fn)
Run Code Online (Sandbox Code Playgroud)
返回一个新的
CompletionStage,当该阶段正常完成时,将使用此阶段的结果作为所提供函数的参数执行.
thenCompose(Function<? super T,? extends CompletionStage<U>> fn)
Run Code Online (Sandbox Code Playgroud)
返回一个新的
CompletionStage,当此阶段正常完成时,将使用此阶段作为所提供函数的参数执行.
我得到的第二个参数thenCompose扩展了CompletionStage,thenApply而不是.
有人可以提供一个例子,我必须使用thenApply何时使用thenCompose?
我不理解泛型和数组之间的联系.
我可以使用泛型类型创建数组引用:
private E[] elements; //GOOD
Run Code Online (Sandbox Code Playgroud)
但无法使用泛型类型创建数组对象:
elements = new E[10]; //ERROR
Run Code Online (Sandbox Code Playgroud)
但它有效:
elements = (E[]) new Object[10]; //GOOD
Run Code Online (Sandbox Code Playgroud) 为什么这段代码没有任何例外?
public static void main(String args[]) {
List<Integer> a = new ArrayList<Integer>();
try {
a.getClass()
.getMethod("add", Object.class)
.invoke(a, new Double(0.55555));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(a.get(0));
}
Run Code Online (Sandbox Code Playgroud) 所以我在Spring 3.2中有很多泛型,理想情况下我的架构看起来像这样.
class GenericDao<T>{}
class GenericService<T, T_DAO extends GenericDao<T>>
{
// FAILS
@Autowired
T_DAO;
}
@Component
class Foo{}
@Repository
class FooDao extends GenericDao<Foo>{}
@Service
FooService extends GenericService<Foo, FooDao>{}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,对于泛型的多个实现,自动装配会引发有关多个匹配bean定义的错误.我假设这是因为@Autowired类型擦除之前的进程.我找到或想出的每一个解决方案看起来都很难看,或者只是莫名其妙地拒绝工作.解决这个问题的最佳方法是什么?
我想知道在Java中专门化泛型类型的选项是什么,即在模板化的类中对某些类型进行特定的覆盖.
在我的例子中,我是一个泛型类(类型为T)通常返回null,但返回""(空字符串),当T是String类型时,或者当它是Integer类型时返回0(零)等.
仅提供方法的类型特定重载会产生"方法不明确"错误:
例如:
public class Hacking {
public static void main(String[] args) {
Bar<Integer> barInt = new Bar<Integer>();
Bar<String> barString = new Bar<String>();
// OK, returns null
System.out.println(barInt.get(new Integer(4)));
// ERROR: The method get(String) is ambiguous for the type Bar<String>
System.out.println(barString.get(new String("foo")));
}
public static class Bar<T> {
public T get(T x) {
return null;
}
public String get(String x) {
return "";
}
}
}
Run Code Online (Sandbox Code Playgroud)
是使用特定类型子类化泛型类的唯一选项(请参阅以下示例中的StringBar?
public static void main(String[] args) {
Bar<Integer> barInt = new Bar<Integer>();
StringBar …Run Code Online (Sandbox Code Playgroud) 在Java泛型之前,Collection.toArray()无法知道开发人员期望的数组类型(特别是对于空集合).据我了解,这是成语背后的主要理由collection.toArray(new E[0]).
使用泛型,Collection<E>.toArray()只能返回一个充满实例E和/或其特化的数组.我想知道为什么返回类型仍然Object[]不是E[].在我看来,返回一个E[]而不是不Object[]应该破坏现有的代码.
请参阅:Collection.toArray(),Collection.toArray(T[])以及相关主题的java:(字符串[])List.toArray()给出ClassCastException异常
我试图了解如何将的Double值包含在ArrayList中Integer。的numList是ArrayList的Integer,并且从它的值是一个Double。
这是代码:
package bounded.wildcards;
import java.util.ArrayList;
import java.util.List;
public class GenericsDemo {
public static void main(String[] args) {
// Invariance Workaround
List<Integer> numList = new ArrayList<>();
GenericsDemo.invarianceWorkaround(numList);
System.out.println(numList);
}
static <T extends Number> void invarianceWorkaround(List<T> list) {
T element = (T) new Double(23.3);
list.add(element);
}
}
Run Code Online (Sandbox Code Playgroud)
这将编译并运行而不会出现错误。
java ×9
generics ×8
arraylist ×1
autowired ×1
collections ×1
java-8 ×1
kotlin ×1
kotlin-reified-type-parameters ×1
spring-mvc ×1
toarray ×1