I have a problem with the following code (do not pay attention to the sense of it, it is simplified just to show the error):
package net.igorok;
import java.util.*;
public class Main {
public static void main(String[] args) {
EntryRecord<Integer, String> data_1 = new EntryRecord(0, "Xiaomi");
EntryRecord<Integer, String> data_2 = new EntryRecord(1, "Apple");
List<EntryRecord<Integer, String>> data = new ArrayList<>();
data.add(data_1);
data.add(data_2);
Operation operation = new Operation();
operation.transform(data);
}
}
.
package net.igorok;
public class EntryRecord<K,V> {
private K key;
private V value;
public EntryRecord(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
//public void setKey(K key), setValue(V value), equals(), hashcode()...
}
.
package net.igorok;
import java.util.Collection;
public interface OperationContract<V, R> {
Collection<R> transform(Collection<V> collection);
}
.
package net.igorok;
import java.util.*;
import java.util.stream.Collectors;
public class Operation<V, R> implements OperationContract<V, R> {
@Override
public Collection<R> transform(Collection<V> collection) {
Map<Integer, String> map = (Map<Integer, String>) collection.stream()
.collect(Collectors.toMap(EntryRecord::getKey, EntryRecord::getValue));
// Do not pay attention to return, it is just for example
return new ArrayList();
}
}
Inside this class, the "EntryRecord::getKey" and "EntryRecord::getValue" are marked by red color ("Non-static method cannot be referenced from a static context", but as I understand, this is an IntelliJ IDEA bug).
The message I received when try to compile is: .
/home/punisher/Dropbox/IdeaProjects/ToSOF/src/net/igorok/Operation.java:11:42 java: incompatible types: cannot infer type-variable(s) T,K,U
(argument mismatch; invalid method reference
method getKey in class net.igorok.EntryRecord<K,V> cannot be applied to given types
required: no arguments
found: java.lang.Object
reason: actual and formal argument lists differ in length)
.
I have already read some similar posts with similar problem, but I cannot understand, what I need to change inside the code. I understand, that this is because of Type Inference, but I am not strong in Generics and Type Inference.
Can you please tell me, what I can change in the code to make it work and why?
Thank you!