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?
Another approach to copying elements is using the addAll method: List<Integer> copy = new ArrayList<>(); copy. addAll(list); It's important to keep in mind whenever using this method that, as with the constructor, the contents of both lists will reference the same objects.
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);
In Java 8, we have the ability to convert an object to another type using a map() method of Stream object with a lambda expression. The map() method is an intermediate operation in a stream object, so we need a terminal method to complete the stream.
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-:
I hope this would help you.
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.
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.
List<Integer> number = Arrays.asList(2, 3, 4, 5, 3);
Set<Integer> square = number.stream()
.map(x -> x * x)
.collect(Collectors.toSet());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With