0

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!

Igor
  • 5
  • 4
  • 1
    Why do you cast to the [raw type](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) `ArrayList`? You don't need to cast at all. `Collection` already has a `stream` method that you can call. – Sweeper Aug 23 '21 at 03:50
  • Not only that, but you introduce a pointless failure if anyone passes in any other `Collection`, even though your signature claims to support it. – chrylis -cautiouslyoptimistic- Aug 23 '21 at 03:54
  • Thank you! Yes, it was a mistake concerning ArrayList. It has to be cast to(Map) for the whole expression, but the collection is without cast to ArrayList. This is because my working example was without any generics and the next my iterations with the code were on not working one. – Igor Aug 23 '21 at 04:08
  • Hello, Igor. Obviously in your implementation V is not a EntryRecord – Delf Aug 23 '21 at 04:09

1 Answers1

0
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()
           .map(e -> (EntryRecord<Integer, String>)e)
                .collect(Collectors.toMap(EntryRecord::getKey, EntryRecord::getValue));

        // Do not pay attention to return, it is just for example
        return new ArrayList();
    }
}

Would work for your case. But, in general, this is not a "good" code.

Delf
  • 64
  • 3
  • Great! Thank you! I do not have enough reputations to mark your answer as useful, but it works! Let me ask you, where it can be refactored to make it "good" in your vision? – Igor Aug 23 '21 at 04:19
  • I don’t know full your task with this code. So this is only my assumption. May be you can rewrite generics to get rid of cast from V to EntryRecord. – Delf Aug 24 '21 at 03:31