- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Java 中的泛型有疑问,特别是通配符。我有一个 Map
( outerMap
),其值为另一个 Map
( innerMap
) 个元素,其值为 List
元素。
请参阅我创建的以下 java 类来演示此行为。不包括导入语句。忽略方法内部流的使用,这是无关紧要的:
public class SSCCE {
public static void main(String[] args) {
// Setup
List<Double> doubles = Arrays.asList(1.0,2.0,3.0);
Map<Integer, List<Double>> innerMap = new HashMap<>();
innerMap.put(1, doubles);
Map<String, Map<Integer, List<Double>>> outerMap = new HashMap<>();
outerMap.put("hello", innerMap);
// Test
// This method call works fine.
Stream<Integer> streamFromInner = getStreamOfMappingToN(innerMap);
// This method call does not work - causes below compilation error.
Stream<Integer> stream = getStreamOfMergedDistinctMappingToN(outerMap);
}
private static <T, U> Stream<T> getStreamOfMappingToN(Map<T, ? extends Collection<U>> map) {
return map.entrySet().stream().map(q -> q.getKey());
}
private static <T, U> Stream<T> getStreamOfMergedDistinctMappingToN(Map<String, Map<T, ? extends Collection<U>>> map) {
return map.entrySet().stream().flatMap(
p -> getStreamOfMappingToN(p.getValue())
).distinct();
}
}
我在第二次方法调用时看到以下编译错误:
method getStreamOfMergedDistinctMappingToN in class SSCCE cannot be applied to given types;
required: Map<String,Map<T,? extends Collection<U>>>
found: Map<String,Map<Integer,List<Double>>>
reason: cannot infer type-variable(s) T,U
(argument mismatch; Map<String,Map<Integer,List<Double>>> cannot be converted to Map<String,Map<T,? extends Collection<U>>>)
where T,U are type-variables:
T extends Object declared in method <T,U>getStreamOfMergedDistinctMappingToN(Map<String,Map<T,? extends Collection<U>>>)
U extends Object declared in method <T,U>getStreamOfMergedDistinctMappingToN(Map<String,Map<T,? extends Collection<U>>>)
任何人都可以建议为什么第二次方法调用会发生这种情况,而不是第一次?两种方法签名都包含 Map<T, ? extends Collection<U>>
,但只有第二个无法匹配调用。如果我更换有问题的 ? extends Collection<U>
与 List<U>
,它工作正常,因为我实际上传递给它一个 List
,但这似乎很懒惰,并没有解释为什么我会看到这个错误。
我已经对此做了一些阅读,有些人遇到了类似的问题并且已经解释了他们的问题 - 许多人评论说通配符的级别很重要 - 但如果不了解这里发生的事情就很难让我将其他解决方案与这个问题联系起来。这仍然很困惑。通过阅读我发现如果我替换 Map<T, ? extends Collection<U>>
与 ? extends Map<T, ? extends Collection<U>>
在第二个方法声明中,它编译得很好,但我不明白为什么,然后为什么第一个方法签名会起作用。
非常感谢您对此的反馈。
最佳答案
您应该将方法更改为以下内容:
private static <T, U extends Collection<?>> Stream<T> getStreamOfMappingToN(Map<T, U> map) {
return map.entrySet().stream().map(q -> q.getKey());
}
private static <T, U extends Collection<?>> Stream<T> getStreamOfMergedDistinctMappingToN(Map<String, Map<T, U>> map) {
return map.entrySet().stream()
.flatMap(p -> getStreamOfMappingToN(p.getValue())).distinct();
}
改变的是,不再将输入类型声明为 ? extends Collection<U>
, 该方法正在接受 Map<T, U>
但是U
被限制为 U extends Collection<?>
.
这个变化代表了你真正想做的:你给了一个Map
作为输入。此 map 有两种类型 T
和 U
. T
可以是我们给出的任何类型,但 U
真的需要一个Collection
的东西,即 U
必须extends Collection<?>
.
要找出您的示例无法编译的原因,请参阅 this answer (或 this one ):通配符仅适用于第一级,不适用于更深的级别。正如您所发现的,您需要添加 ? extends Map<...>
以反驳未递归应用通配符的事实。
不过,我还是建议您使用第一种解决方案,因为我认为它提供了更清晰的代码和正确的意图,而不是乱用通配符。
关于java - 嵌套通配符调用方法编译失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33588553/
我的网址看起来像 '/api/comments/languages/124/component/segment_translation/2' 我知道 url 的哪些部分是静态的;并且是动态的 - 并且
如何使用通配符查找和替换主域之后的所有字符(包括“/”字符)? 例如,我有以下 4 行: intersport-schaeftlmaier.de/ weymouthhondapowersports.c
我有 3 个控件,其 ID 为 control_1、control_2、control_3。 我想隐藏这些控件。 目前我正在使用这个: $('#control_1').hide(); $('#cont
我有一个旧歌曲数据库,我想将其转移到新数据库。我的旧数据库看起来像这样,多个值被填充在一个用逗号分隔的字段中 SONG id | title | artist |
首先,我知道downloads表没有标准化。 我有这两个表: downloads map | author 1 | Nikola 2 | Nikola George 和 mappers mapper_
通配符可用于替代字符串中的任何其他字符。 SQL 通配符 在 SQL 中,通配符与 SQL LIKE 操作符一起使用。 SQL 通配符用于搜索表中的数据。 在 SQL 中,可使用以下通配符:
我在 shell 脚本中有一行看起来像这样: java -jar "$dir/"*.jar ,因为我只想执行该文件夹中恰好命名的 jar 文件。但这并不像我预期的那样有效。我收到错误消息: Error
我想在 Active Directory 用户的所有属性中搜索特定电话号码/分机号。 我可以像这样获取所有属性: get-aduser joesmith -Properties * 但我想过滤结果,例
我在运行 Python 3在 Windows 机器上使用 PowerShell .我正在尝试执行一个 Python 文件,然后使用通配符将多个文件(file1.html、file2.html 等)作为
我有一个 div,并且有一些处于未定义级别的子节点。 现在我必须将每个元素的 ID 更改为一个 div。如何实现? 我想,因为它们有向上的ID,所以如果父级是id='path_test_maindiv
我是 Lua 的新手,所以我现在正在学习运算符部分。在 Lua 中是否有与字符串一起使用的通配符? 我有 PHP 背景,我实际上是在尝试编写以下代码: --scan the directory's f
我在 countList 方法上遇到编译时错误。 public static void countList( List list, int count ){ for( int i =
我们需要在运行时检索多个类实例,而无需手动维护所有可用类型的列表。 可能的方法: 检索带有@xy注释的每种类型的实例 检索每种类型的实例实现接口(interface)iXY 检索每种类型的实例,命名如
我目前陷入了序言问题。 到目前为止我有: film(Title) :- movie(Title,_,_).(其中“movie(T,_,_,)”是对我的引用数据库) namesearch(Title,
我想从字符表达式(在 R 中)中删除一个“*”。在阅读帮助页面并尝试谷歌后,我无法充分理解 gsub 的复杂性。有人可以建议我该怎么做吗? 谢谢, 乔纳森。 最佳答案 您需要转义两次:一次针对 R,一
在我的 DOM 中,我有一个动态生成对话框的表。 DOM 中的对话框将具有以下形式的 ID: id="page:form:0:dlg" id="page:form:1:dlg" id="page:fo
我是 Java 新手,并且已经陷入这样一种情况,很明显我误解了它如何处理泛型,但是阅读教程和搜索 stackoverflow 并没有(至少到目前为止)让我清楚我怀疑我滥用了通配符。需要注意的是,我有
我想使用 jQuery 更改单击时图像的 src 属性。这是 HTML: View 2 在 img src 中,我想将“a”替换为“b”,但我的问题是我想忽略它前面的“1”,因为它也可能看起来像这样
我有一个 mysql 数据库,我的表是: Name | passcode ---------------------- hi* | 1111 ------------------
我想选择所有在星号所在位置具有确切 4 个“未知”字符的文档:(例如“****”可能是“2018”) foreach (string s in Directory.GetFiles(@"C:\User
我是一名优秀的程序员,十分优秀!