-2

Why this code is not compile for me?

I try to convert List to Map using stream and toMap option

List<CountryToPaymentMethodsDisplayRules>
   paymentMethodDisplayRulesByCountryList = 
       gatway.getCountryPaymentMethodsDisplayRulesByCountry();

Map<PaymentMethod, CountryToPaymentMethodsDisplayRules>
   countryToPaymentMethodsDisplayRulesMap = paymentMethodDisplayRulesByCountryList
       .stream()
       .collect(Collectors.toMap(type -> type.getCountryToPaymentMethodsDisplayRules().getPaymentMethod(),
                                 type -> type));

public interface PaymentMethod extends Serializable {
}

public enum PaymentMethodType implements PaymentMethod, Serializable {
}

public interface CountryToPaymentMethodsDisplayRules {
    public PaymentMethod getPaymentMethod();
}

public class CountryToPaymentMethodsDisplayRulesEntity implements CountryToPaymentMethodsDisplayRules, PersistentEntity<Long>, Serializable {
    @Type(type = "com.plimus.core.payment.PaymentMethodTypeUserType")
    @Column(name = "PAYMENT_TYPE")
    private PaymentMethod paymentMethod;
}

What is wrong here?

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Yaron Tzur
  • 108
  • 1
  • 1
  • 6
  • 2
    Well you've provided some code (which I've reformatted for you - please read the formatting help and avoid posting until the preview shows reasonable formatting) but you haven't said in what way it doesn't work... Ideally, rewrite this as a [mcve]. – Jon Skeet Mar 17 '17 at 11:06
  • 2
    I can not find method getCountryToPaymentMethodsDisplayRules. Could you provide some information about this method? – Roma Khomyshyn Mar 17 '17 at 11:07
  • 3
    Indeed, what *is* wrong here? Does it produce an exception? Does it produce an empty map? Does it run indefinitely? We may never know! – M. Prokhorov Mar 17 '17 at 11:13

2 Answers2

7

You just need to provide the Collections.toMap() method with a method reference and an identity. Try this:

 Map<PaymentMethod, CountryToPaymentMethodsDisplayRules>
    countryToPaymentMethodsDisplayRulesMap = paymentMethodDisplayRulesByCountryList
    .stream()
    .collect(Collectors.toMap(CountryToPaymentMethodsDisplayRules::getPaymentMethod,x->x);
CraigR8806
  • 1,584
  • 13
  • 21
0

Found the problem thanks

Map<PaymentMethod, CountryToPaymentMethodsDisplayRules>
countryToPaymentMethodsDisplayRulesMap = paymentMethodDisplayRulesByCountryList
   .stream()
   .collect(Collectors.toMap(type -> type.getPaymentMethod(),
                             type -> type));
Yaron Tzur
  • 108
  • 1
  • 1
  • 6
  • 4
    Do you really think that the finding, that calling a method that does not exist can be fixed by removing the method call, has any worth for future readers? – Holger Mar 17 '17 at 15:03