No instance s of type variable s t exist so that void conforms to t
No instance s of type variable s t exist so that void conforms to t
so I’ve build these two classes: 1. Genre which implements Comparable 2. GenreManager which takes a Collection of genres and creates an internal copy of it. Later in GenreManager, I will need to add new Genres by getting a name as an input, and I need to assign this Genre the next free id number, which is basically the next smallest positive number after the smallest used id.
I am trying to use Collections.sort() to sort my list but I am getting the following error: «no instance(s) of type variable(s) T exist so that Collection conforms to List.» and I am not sure what this is referring to. I’ve tried ready a bunch of posts about this on here but couldn’t figure out the solution. Here is part of the code:
I am trying to do the sorting in the constructor above. Can someone tell me how to go around this?
PS: I can’t change any of the method header or class headers.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
As per request, here’s a compilation of my comments to answer the question:
The immediate problem is that Collections.sort(List ) takes a List parameter and not just a Collection because collections in general don’t have to be sortable (e.g. hash sets aren’t). Additionally the method returns void and sorts the passed list in place, i.e. the way you call it won’t compile.
Taking all this into consideration your code might be changed to something like this:
This can be solved to use a set instead of a list. However, a HashSet would depend on how equals() and hashCode() define equality, which doesn’t seem to match your requirements. That could be solved by using a wrapper object that implements both methods as needed.
A better approach might be to use a TreeSet though. TreeSet uses comparisons to determine order and equality (if the compare result is 0) and thus would allow you to either let your Genre class implement Comparable as you did or provide a separate Comparator (e.g. if you need multiple different definitions of equality).
If you just want to eliminate duplicates, your code could then look like this:
If you want to know what duplicates are in the collection you could do it like this:
Java generics incompatible types (no instance(s) of type variable(s) exist)
The question title may seem to be same as some other post but the content is different. So please don’t mark it duplicate.
Problem:
I have the below class:
But I have an compilation error saying:
Can anyone please explain why this is happening and how to solve it?
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You are streaming entries from the map:
Then filtering out some of them:
now you need to map the entries to List, for example:
and stream the contents of all these lists as one stream:
Finally collect the values into a list:
For example, if you wish to collect all the keys having a value that passes the filter into a List :
You don’t need to iterate over the entrySet since you are not using the key at all. You can get the values of the Map and filter that. Looks like this