I am attempting to filter down a map to a sub set of itself. To achieve this I have attempted to implement the following utils method:
/**
* Reduces the size of the map. Starts from the first entry in the map.
* @param map The map.
* @param size The size to reduce the map by.
* @return The reduced size map.
*/
public static <T> Map<T, T> subList(@NonNull Map<T, T> map, int size) {
val sub = new HashMap<T, T>();
for (Map.Entry<T, T> entry : map.entrySet()) {
if (sub.size() > size) {
break;
}
sub.put(entry.getKey(), entry.getValue());
}
return sub;
}
I am then attempting to use it like this:
@Override
public Set<FileTransfer> upload(@NonNull ConnectionConfiguration target, @NonNull Map<FileTransfer, File> fileExports) {
val batchSize = getBatchSize();
if (fileExports.size() > batchSize) {
fileExports = (Map<FileTransfer, File>) MapUtils.subList(fileExports, batchSize);
}
...
}
But I am getting the error:
reason: Incompatible equality constraint: FileTransfer and File
I had assumed that I could pass a Map<FileTransfer, File>
to the method because both types extend Object
. What am I doing wrong here?