ListUtil
tool class
so why do two unrelated classes, Comparator
and BiFunction
, conflict? And how should this conflict be resolved?
ListUtil
tool class
so why do two unrelated classes, Comparator
and BiFunction
, conflict? And how should this conflict be resolved?
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
getName ()
method partial code
filter
<ol>
<li>1 2 1+2FunctionalInterface
</li>
<li>1 1 1+1FunctionalInterface
</li>
</ol>
Comparator
BiFunction
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)
Previous: The markArea of echarts sets the regional background color
Next: How does cytoscape.js prevent individual nodes from dragging?
the requirement is to query the latest 3 transactions of each customer. The sql is similar to the following: select * from ( select , rank() over(partition by order by ) as ranking from ) where ranking > 3 how to use st...
topic description lambda groupingBy Advanced questions List < A > how to convert to List < B > sources of topics and their own ideas looked up a lot of materials and found that most of the Internet teaches how to convert List into Map < String,Li...