Error in Java lambda automatic inference

< H1 > Java lambda automatic inference error < / H1 > < H2 > scene < / H2 >

ListUtil tool class

   

so why do two unrelated classes, Comparator and BiFunction , conflict? And how should this conflict be resolved?

Jun.27,2022

try writing into the method


1, this Lists.newArrayList method is encapsulated by yourself, isn't it without generics? Even if you use list < user > to receive, the original type is still obj

2, using lists in the toolkit, generic parameters, and native arraylist are all fine

3, run result
clipboard.png


I have tried. You can visit the getName () method

without type.
partial code

filter

<ol> <li>1 2 1+2FunctionalInterface </li> <li>1 1 1+1FunctionalInterface </li> </ol>

ComparatorBiFunction

int compare(T o1, T o2);

isn't this pattern BiFunction , just saying that the two are different FunctionalInterface , just like BinaryOperator and BiFunction

so when you call the filter method with the same type User , both methods actually match, so the third parameter cannot be inferred, only the Object type is the default

.

suppose you create a new type UserNew , which is similar to the User type. If you call the filter method with two different type parameters, you must have used the first one, so that you will not make a mistake

.
final List<User> users1 = Lists.newArrayList(new User("rx", 17), new User("haor", 15));
final List<User> users2 = Lists.newArrayList(new User("Ling", 15), new User("rx", 17));
final List<UserNew> users3 = Lists.newArrayList(new UserNew("Ling", 15), new UserNew("rx", 17));

// getName 
ListUtil.filter(users1, users2, (u1, u2) -> Objects.equals(u1.getName(), u2.getName()));
// getName 
ListUtil.filter(users1, users3, (u1, u2) -> Objects.equals(u1.getName(), u2.getName()));

of course, the subject will definitely say, "I also have scenarios in which method 1 is called with the same type, so you need to specify the third parameter type
, such as the example of the subject. Finally, you should write

."
// BiFunction
BiFunction<User, User, Boolean> biFunction = (u1, u2) -> Objects.equals(u1.getName(), u2.getName());
ListUtil.filter(users1, users2, biFunction);

the above is for reference only ((OO) OO)

Menu