Issue
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
Type mismatch: cannot convert from List<Object>
to 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 res is resultset i am getting from database, not sure what is wrong any idea?
Solution
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-:
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 version in project properties. I had selected 1.7 but it should be 1.8 since this feature has been added in 1.8.
So please make a note of below points-:
- Appropriate JDK has been selected in Java Build Path. e.g. JDK 1.8 in this case.
- Appropriate compiler version must be selected under Java Compiler (as displayed in above screenshot) in project properties. e.g. 1.8
I hope this would help you.
Answered By - Ashish Kumar
Answer Checked By - David Marino (JavaFixing Volunteer)