In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how Java8 uses Lambda expressions to sort List collections". In daily operations, I believe many people have doubts about how Java8 uses Lambda expressions to sort List collections. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to use Lambda expressions to sort List collections". Next, please follow the editor to study!
Lambda uses a functional interface Comparator that comes with JDK8.
Prepare an Apple class
Public class Apple {private int weight; private String color; public Apple () {} public Apple (int weight) {this.weight = weight;} public Apple (int weight, String color) {this.weight = weight; this.color = color;} setters (); getters (); toString ();}
Step 1:
Public class AppleComparator implements Comparator {@ Override public int compare (Apple o1, Apple O2) {return o1.getWeight ()-o2.getWeight ();}}
Step 2: prepare a List collection
ArrayList inventory = Lists.newArrayList (new Apple (10, "red"), new Apple (5, "red"), new Apple (1, "green"), new Apple (15, "green"), new Apple (2, "red"))
Step 3: sort sequentially in three ways
/ * * ordering * / 1, passing code, functional programming inventory.sort (new AppleComparator ()); System.out.println (inventory); / / 2, anonymous inner class inventory.sort (new Comparator () {@ Override public int compare (Apple o1, Apple O2) {return o1.getWeight ()-o2.getWeight ();}}) / / 3. Use the Lambda expression inventory.sort ((a, b)-> a.getWeight ()-b.getWeight ()); / / 4, comparingComparator comparing = comparing ((Apple a)-> a.getWeight ()) using Comparator; inventory.sort (comparing ((Apple a)-> a.getWeight (); / / or equivalent to inventory.sort (comparing (Apple::getWeight))
Step 4: sort in reverse order
/ * * sort in reverse order * / 1, sort inventory.sort in reverse order by weight (comparing (Apple::getWeight). Reversed ())
Step 5: if two apples weigh the same weight, you have to find another condition to sort them.
/ / 2. What if the weight of two apples is the same? Then find another condition to sort inventory.sort (comparing (Apple::getWeight). Reversed (). ThenComparing (Apple::getColor))
At this point, the study on "how Java8 uses Lambda expressions to sort List collections" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.