2

Hello I'm beginner when it comes to Java 8 so please be patient for me :)

I have a method that returns custom list of objects. What I need to do: I have got a list of disabledPaymentTypesStrings - and I don't know how many elements it has got. How can I change my code in order to not write every condition like !paymentType.getName().equalsIgnoreCase(disabledPaymentTypesStrings.get(1))? I would like to have somehow my whole list "disabledPaymentTypesStrings" placed here as a condition but I have no idea how to do that. Please give me some hints or advices :)

private List<PaymentType> listOfPaymentTypesForChangePayment(OrderPaymentTypeParameters paymentTypeParameters) { List<String> disabledPaymentTypesStrings = newArrayList(Splitter.on(COMMA).split(systemUtils.getChangePaymentTypeDisabled())); return paymentTypeSelector.availablePaymentTypesForChangePayment(paymentTypeParameters).stream() .filter(paymentType -> !paymentType.getName().equalsIgnoreCase(disabledPaymentTypesStrings.get(0)) && !paymentType.getName().equalsIgnoreCase(disabledPaymentTypesStrings.get(1)) && !paymentType.getName().equalsIgnoreCase(disabledPaymentTypesStrings.get(2))) .collect(toList()); } 
1
  • 4
    Consider representing your payment types as enums rather than strings. Use a set instead of a list. Commented Aug 20, 2018 at 10:37

3 Answers 3

3

A stream approach could consist in the filter() to stream the List of String and keep PaymentType elements where paymentType.getName() don't match with any elements of the List of String :

return paymentTypeSelector.availablePaymentTypesForChangePayment(paymentTypeParameters) .stream() .filter(paymentType -> disabledPaymentTypesStrings.stream() .allMatch(ref -> !ref.equalsIgnoreCase(paymentType.getName()))) .collect(toList()); 

But you could also compare Strings by using the same case. For example lowercase. It will simplify the filtering.

You can convert the reference list elements to lowercase :

List<String> disabledPaymentTypesStrings = newArrayList(Splitter.on(COMMA).split(systemUtils.getChangePaymentTypeDisabled())) .stream() .map(String::toLowerCase) .collect(toList()); 

And you can so use List.contains() in the filter() :

return paymentTypeSelector.availablePaymentTypesForChangePayment(paymentTypeParameters) .stream() .filter(paymentType -> !disabledPaymentTypesStrings.contains(paymentType.getName().toLowerCase())) .collect(toList()); 

Note that for big lists, using a Set would be more efficient.

Sign up to request clarification or add additional context in comments.

1 Comment

A note for the sake of completness: Converting disabledPaymentTypesStrings to a List and calling contains on it implies that disabledPaymentTypesStrings does have 3 elements. Regarding the code in the question there could be more than 3 elements but only the first 3 are compared. And a second note: Like @RealSkeptic suggests in his comment, using a Set is more suitable when calling contains.
0

Use contains(). But you have to think about case sensitivity ignoring

private List<PaymentType> listOfPaymentTypesForChangePayment(OrderPaymentTypeParameters paymentTypeParameters) { List<String> disabledPaymentTypesStrings = newArrayList(Splitter.on(COMMA).split(systemUtils.getChangePaymentTypeDisabled())); return paymentTypeSelector.availablePaymentTypesForChangePayment(paymentTypeParameters).stream() .filter(paymentType -> !disabledPaymentTypesStrings.contains(paymentType) .collect(toList()); } 

Comments

0

Both the steps need to have values in common case(either in uppercase or in lowercase, I preferred lowercase)

List<String> disabledPaymentTypesStringsLowerCase = newArrayList(Splitter.on(COMMA).split(systemUtils.getChangePaymentTypeDisabled())) .stream() .map(String::toLowerCase) .collect(toList()); return paymentTypeSelector.availablePaymentTypesForChangePayment(paymentTypeParameters) .stream() .map(paymentType -> paymentType.getName()) .map(String::toLowerCase) .filter(disabledPaymentTypesStrings::contains) .collect(toList()); 

This code can further be refactored if paymentType class is known, assuming class of paymentType is PaymentType code would look like below,

return paymentTypeSelector.availablePaymentTypesForChangePayment(paymentTypeParameters) .stream() .map(PaymentType::getName) .map(String::toLowerCase) .filter(disabledPaymentTypesStrings::contains) .collect(toList()); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.