如何为返回类型推断这些Java泛型语法?
我在Java中遇到了这种语法如何为返回类型推断这些Java泛型语法?,java,generics,Java,Generics,我在Java中遇到了这种语法 ImmutableMap.Builder<String, Object> builder = ImmutableMap.<String, Object>builder() ImmutableMap.Builder=ImmutableMap.Builder() 在左边,我可以理解构建器采用两种类型:String和Object。但是右手的sytax让我困惑。 当我读到它的时候 ImmutableMap.XXX//XXX应为属性或静态方法 嗯,
ImmutableMap.Builder<String, Object> builder = ImmutableMap.<String, Object>builder()
ImmutableMap.Builder=ImmutableMap.Builder()
在左边,我可以理解构建器采用两种类型:String和Object。但是右手的sytax让我困惑。
当我读到它的时候
是不可变的。
//为什么XXX现在是
,一种泛型类型Immutable.builder()代码>//现在,这是builder()返回类型吗?它将返回不可变。
。构建器现在的行为如何像一个独立的函数expect(mockDao.findId(role, name)).andReturn(Optional.<Long>absent());
expect(mockDao.findId(role,name)).andReturn(可选的.缺席());
缺席()是Optinal的一个通用静态方法,我可以理解它是否是可选的。缺席(),但将其设置为Optional.缺席()
而不是Optional.缺席()
让我有点困惑
我应该研究哪些来源来澄清我对这个概念的理解?大多数情况下,当一个方法是通用的时,例如:
public static <T> Optional<T> absent()
Optional<String> opt = Optional.absent();
要指定要使用哪个泛型类型调用方法,语法是在方法名称之前指定泛型类型:
Set<Object> set = Collections.<Object>singleton("foo");
Set=Collections.singleton(“foo”);
大多数情况下,当方法是泛型时,例如:
public static <T> Optional<T> absent()
Optional<String> opt = Optional.absent();
要指定要使用哪个泛型类型调用方法,语法是在方法名称之前指定泛型类型:
Set<Object> set = Collections.<Object>singleton("foo");
Set=Collections.singleton(“foo”);
大多数情况下,当方法是泛型时,例如:
public static <T> Optional<T> absent()
Optional<String> opt = Optional.absent();
要指定要使用哪个泛型类型调用方法,语法是在方法名称之前指定泛型类型:
Set<Object> set = Collections.<Object>singleton("foo");
Set=Collections.singleton(“foo”);
大多数情况下,当方法是泛型时,例如:
public static <T> Optional<T> absent()
Optional<String> opt = Optional.absent();
要指定要使用哪个泛型类型调用方法,语法是在方法名称之前指定泛型类型:
Set<Object> set = Collections.<Object>singleton("foo");
Set=Collections.singleton(“foo”);
您必须以某种方式提供该类型。如果我有
public static <T> T getRandom(List<T> list) { ... }
publicstatict getRandom(列表){…}
这是一种从列表中返回随机元素的方法,该类型是从我传入的对象推断出来的:
List<String> list = // whatever
String randomString = getRandom(list); // <- the type String gets filled in here
List=//随便什么
String randomString=getRandom(列表);// 你必须以某种方式提供类型。如果我有
public static <T> T getRandom(List<T> list) { ... }
publicstatict getRandom(列表){…}
这是一种从列表中返回随机元素的方法,该类型是从我传入的对象推断出来的:
List<String> list = // whatever
String randomString = getRandom(list); // <- the type String gets filled in here
List=//随便什么
String randomString=getRandom(列表);// 你必须以某种方式提供类型。如果我有
public static <T> T getRandom(List<T> list) { ... }
publicstatict getRandom(列表){…}
这是一种从列表中返回随机元素的方法,该类型是从我传入的对象推断出来的:
List<String> list = // whatever
String randomString = getRandom(list); // <- the type String gets filled in here
List=//随便什么
String randomString=getRandom(列表);// 你必须以某种方式提供类型。如果我有
public static <T> T getRandom(List<T> list) { ... }
publicstatict getRandom(列表){…}
这是一种从列表中返回随机元素的方法,该类型是从我传入的对象推断出来的:
List<String> list = // whatever
String randomString = getRandom(list); // <- the type String gets filled in here
List=//随便什么
String randomString=getRandom(列表);//有意思。。当方法不是泛型时,向任何方法调用添加任何泛型类型规范显然是合法的System.out.println(“Hello!”.toString())
interest。。当方法不是泛型时,向任何方法调用添加任何泛型类型规范显然是合法的System.out.println(“Hello!”.toString())
interest。。当方法不是泛型时,向任何方法调用添加任何泛型类型规范显然是合法的System.out.println(“Hello!”.toString())
interest。。当方法不是泛型时,向任何方法调用添加任何泛型类型规范显然是合法的System.out.println(“你好!”.toString())
谢谢!这解释得很清楚。为什么那种类型的怪癖会在课后给出。在方法名之前?它可能是Collections.singleton(“foo”)
(有点像类型转换)?如果你想做String foo=SomeClass.bla().bling().bing().bizz()
?嗯,很有趣。我明白了。根据我的思考过程,这可能是(((SomeClass.bla()).bling()).bing()).bizz())
谢谢!这解释得很清楚。为什么那种类型的怪癖会在课后给出。在方法名之前?它可能是Collections.singleton(“foo”)
(有点像类型转换)?如果你想做String foo=SomeClass.bla().bling().bing().bizz()
?嗯,很有趣。我明白了。根据我的思考过程,这可能是(((SomeClass.bla()).bling()).bing()).bizz())
谢谢!这解释得很清楚。为什么那种类型的怪癖会在课后给出。在方法名之前?它可能是Collections.singleton(“foo”)
(有点像类型转换)?如果你想做String foo=SomeClass.bla().bling().bing().bizz()
?嗯,很有趣。我明白了。根据我的思考过程,这可能是(((SomeClass.bla()).bling()).bing()).bizz())
谢谢!这解释得很清楚。为什么那种类型的怪癖会在课后给出。在方法名之前?它可能是Collections.singleton(“foo”)
(有点像类型转换)?如果你想做String foo=SomeClass.bla().bling().bing().bizz()
?嗯,很有趣。我明白了。根据我的思考过程,这可能是(((SomeClass.bla()).bling()).bing()).bizz())