18

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?

12
  • so the .collect(Collectors.toList()) gives you a List<Object> but you want it to be List<String> Commented Jan 25, 2016 at 10:09
  • 1
    Do you still have compilation error if you remove the while loop completely? Commented Jan 25, 2016 at 10:26
  • 1
    @Nomad, of course I understand that the logic of your program will change. We are speaking about compilation error. Will the compilation error disappear or not? I'm not asking you to commit the changed code to production. Commented Jan 25, 2016 at 10:38
  • 1
    Is this a javac compilation error or is it an error from your IDE? This should work. Commented Jan 25, 2016 at 10:45
  • 1
    Then upgrade your Eclipse. This works. Commented Jan 25, 2016 at 10:50

4 Answers 4

17

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-: enter image description here

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.

enter image description here

So please make a note of below points-:

  1. Appropriate JDK has been selected in Java Build Path. e.g. JDK 1.8 in this case.
  2. 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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

1

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());

Comments

0

Note that if you get this error and for some reason you are stuck with Java <1.8 you can write the following:

... .collect(Collectors.<String>toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.