I recently started working in Java streams. I was trying to get the String values out of the result set of one of my SQL queries. The result set selects just a String/Varchar column from the DB.
So I did:
List<String> list = query.getResultList().stream().map(Object::toString).collect(Collectors.toList());
or:
List<String> list = = query.getResultList().stream().map(String::valueOf).collect(Collectors.toList());
I believe the map takes a function to convert data from one type to another. In this case, from Object to String and then collect them in a String list.
But the above code shows compile time error: Cannot convert from Object to List of string.
Please suggest me the correct way of doing this and explain what is wrong with my understanding.