Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

How to transform this lambda code to method reference in Java?

I have a method reference question in Java.

I want to transform this lambda code:

Collections.sort(trends, (map1, map2) -> (int) ((long) map2.get("pageview") - (long) map1.get("pageview")));

How to use Integer::compareTo?

question from:https://stackoverflow.com/questions/65914070/how-to-transform-this-lambda-code-to-method-reference-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In order to user method reference "Integer::compareTo" try this:

Collections.sort(trends, Comparator.comparing( map -> -map.get("pageview").intValue(),
                Integer::compareTo));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...