Marcus Fihlon
03/19/2017, 9:46 AMimport com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.toList;
public class SortCollectJava {
private final Map<Long, Question> questions = new ConcurrentHashMap<>();
List<Question> readAll() {
return ImmutableList.copyOf(
questions.values().stream()
.sorted(comparingLong(Question::getQuestionId))
.collect(toList()));
}
}
My Kotlin code looks like this:
import com.google.common.collect.ImmutableList
import java.util.concurrent.ConcurrentHashMap
import java.util.Comparator.comparingLong
import java.util.stream.Collectors.toList
class SortCollectKotlin {
private val questions = ConcurrentHashMap<Long, Question>()
internal fun readAll(): List<Question> {
return ImmutableList.copyOf(
questions.values.stream()
.sorted(comparingLong<Question>({ it.questionId }))
.collect<List<Question>, Any>(toList<Question>()))
}
}
I have a compile error at the toList
call:
Type mismatch: inferred type is Collector<Question!, *, (Mutable)List<Question!>!>! but Collector<in Question!, Any!, List<Question>!>! was expected
I’m new to Kotlin but as far as I understand this code should work. Maybe I do not understand the meaning of “!”. How should this code look like?