如何将泛型放入Supplier中

huangapple go评论107阅读模式
英文:

how to put generics in Supplier

问题

以下是翻译好的内容:

这段代码有什么问题:

  1. public static final <T> Supplier<Map<Task, T>> TASK = () -> new EnumMap<>(Task.class);

我收到以下错误:

  1. 语法错误,期望引用类型而不是标记

如何在这个供应商(Supplier)中添加泛型?

英文:

What is wrong with this:

  1. public static final &lt;T&gt; Supplier&lt;Map&lt;Task, T&gt;&gt; TASK = () -&gt; new EnumMap&lt;&gt;(Task.class);

I get this error:

  1. Syntax error on tokens, ReferenceType expected instead

How can I add generics to this supplier?

答案1

得分: 0

我用以下方法解决了这个问题:

  1. public static final <T> Supplier<Map<Task, T>> mapFactory() {
  2. return () -> new EnumMap<>(Task.class);
  3. }

但是,实际上并不需要使用Supplier:

  1. public static final <T> Map<Task, T> mapFactory() {
  2. return new EnumMap<>(Task.class);
  3. }
英文:

I solved it with this:

  1. public static final &lt;T&gt; Supplier&lt;Map&lt;Task, T&gt;&gt; mapFactory() {
  2. return () -&gt; new EnumMap&lt;&gt;(Task.class);
  3. }

But yes, there is no need for supplier:

  1. public static final &lt;T&gt; Map&lt;Task, T&gt; mapFactory() {
  2. return new EnumMap&lt;&gt;(Task.class);
  3. }

huangapple
  • 本文由 发表于 2020年5月2日 17:53:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61557426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定