java 8 收集器类型不匹配的问题:无法从 List<Object> 转换为 List<String>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34989759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
issue with java 8 collectors Type mismatch: cannot convert from List<Object> to List<String>
提问by Nomad
i was having working code with earlier version of java 8 which i was using to get unique values from list but since i upgraded to JDK 66 its giving me an error
我正在使用早期版本的 java 8 工作代码,我用它从列表中获取唯一值,但自从我升级到 JDK 66 后,它给了我一个错误
Type mismatch: cannot convert from List<Object>
to List<String>
类型不匹配:无法转换List<Object>
为List<String>
List<String> instList = new ArrayList<String>();
while (res.next()) {
instList.add(res.getString("INST").toString());
}
List<String> instListF = instList.stream().distinct().collect(Collectors.toList());
Where resis resultset i am getting from database, not sure what is wrong any idea?
当水库的结果集我是从数据库中获取,不知道什么是错的任何想法?
采纳答案by Ashish Kumar
Well I have also faced similar kind of error Type mismatch: cannot convert from Set<Object> to Set<String>
recently. Below is the code snippet-:
好吧,我最近也遇到了类似的错误Type mismatch: cannot convert from Set<Object> to Set<String>
。以下是代码片段-:
public static void main(String[] args) {
String[] arr = new String[]{"i", "came", "i", "saw", "i", "left"};
Set<String> set = Arrays.asList(arr).stream().collect(Collectors.toSet());
System.out.println(set.size() + " distinct words: " + set);
}
Here is the screen shot for reference-:
Now let me explain why was I getting this error? In my case code was displaying compile time error because there was mismatch in compiler versionin project properties. I had selected 1.7but it should be 1.8since this feature has been added in 1.8.
现在让我解释为什么我会收到这个错误?在我的情况下,代码显示编译时错误,因为项目属性中的编译器版本不匹配。我选择了1.7但它应该是1.8因为此功能已添加到1.8。
So please make a note of below points-:
所以请注意以下几点——
- Appropriate JDK has been selected in Java Build Path. e.g. JDK 1.8in this case.
- Appropriate compiler versionmust be selected under Java Compiler(as displayed in above screenshot) in project properties. e.g. 1.8
- 在Java Build Path 中选择了合适的 JDK 。例如JDK 1.8在这种情况下。
- 必须在项目属性中的Java 编译器(如上图所示)下选择适当的编译器版本。例如1.8
I hope this would help you.
我希望这会帮助你。
回答by Tagir Valeev
I checked the following complete example:
我检查了以下完整示例:
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;
public class Test {
public List<String> test(ResultSet res) throws SQLException {
List<String> instList = new ArrayList<String>();
while (res.next()) {
instList.add(res.getString("INST").toString());
}
List<String> instListF = instList.stream().distinct().collect(Collectors.toList());
return instListF;
}
}
It compiles perfectly with javac 8u25, 8u40, 8u60, 8u71 (note that 8u71 is the security update of 8u66, thus essentially the same). Try to clean your project and rebuild from scratch.
它与javac 8u25、8u40、8u60、8u71完美编译(注意8u71是8u66的安全更新,因此基本相同)。尝试清理您的项目并从头开始重建。
回答by Karidrgn
After checking for my compiler level (per Ashish above), I realized that I had no datatype on either List or Set. Once I added it worked.
在检查我的编译器级别(根据上面的 Ashish)之后,我意识到我在 List 或 Set 上都没有数据类型。一旦我添加它就起作用了。
List<Integer> number = Arrays.asList(2, 3, 4, 5, 3);
Set<Integer> square = number.stream()
.map(x -> x * x)
.collect(Collectors.toSet());