当前位置:网站首页>Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.
Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.
2022-06-30 02:39:00 【aigo-2021】
call Collections.sort() Method , Compare the two Person object ( First by age , The same age by name ), Use Lambda The expression is passed as an argument .
public class Person {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// rewrite toString Method
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Person> list = Arrays.asList(new Person(" Xiao Liang ", 22),
new Person(" Li Si ", 23), new Person(" Xiao Ming ", 20),
new Person(" Zhang San ", 20));
//sort Method passes in a comparator Comparator<? super T> c
Collections.sort(list,(a, b) ->{
int num = Integer.compare(a.age, b.age);
int num2 = num==0?a.name.compareTo(b.name):num;
return num2; // Be sure to write when there are multiline expressions return
});
for (Person employee : employeeList) {
System.out.println(employee.toString());
}
}
}Running results :

边栏推荐
- CMake教程系列-02-使用cmake代碼生成二進制
- SSL证书格式转化的两种方法
- FDA ESG regulation: digital certificate must be used to ensure communication security
- 五个最便宜的通配符SSL证书品牌
- Merge sort
- What files does a CA digital certificate contain? How to view SSL certificate information?
- 2022 the action of protecting the net is imminent. Things about protecting the net
- LeetCode 3. 无重复字符的最长子串
- 迅为恩智浦iTOP-IMX6开发平台
- Differences among digicert, SECTIONO and globalsign code signing certificates
猜你喜欢

Two methods of SSL certificate format conversion

What is the difference between a layer 3 switch and a layer 2 switch

AutoJS代碼能加密嗎?YES,AutoJS加密技巧展示

Jupyter notebook displays a collection of K-line graphs

1380. lucky numbers in matrices

论文回顾:Playful Palette: An Interactive Parametric Color Mixer for Artists

打造创客教育中精湛技艺

JMeter obtains cookies across thread groups or JMeter thread groups share cookies

走进江苏作家诗人胭脂茉莉|世界读书日

选择排序
随机推荐
How to use SMS to deliver service information to customers? The guide is here!
LeetCode 3. 无重复字符的最长子串
Entering Jiangsu writers and poets carmine Jasmine World Book Day
Jupyter notebook displays a collection of K-line graphs
[on] [DSTG] dynamic spatiotemporalgraph revolutionary neural networks for traffic data impact
Steam elements hidden in science and Technology Education
Global and Chinese markets for light cargo conveyors 2022-2028: Research Report on technology, participants, trends, market size and share
[NPM] solve the problem of error reporting when installing typeorm with NPM
Matlab code running tutorial (how to run the downloaded code)
[dry goods sharing] the latest WHQL logo certification application process
Le Code autojs peut - il être chiffré? Oui, présentation des techniques de chiffrement autojs
走进江苏作家诗人胭脂茉莉|世界读书日
What is a dangling pointer- What is a dangling pointer?
Global and Chinese market of subscription revenue management software 2022-2028: Research Report on technology, participants, trends, market size and share
DHU programming exercise
DHU programming exercise
Global and Chinese market for defense network security 2022-2028: Research Report on technology, participants, trends, market size and share
Network neuroscience——网络神经科学综述
2022护网行动在即,关于护网的那些事儿
002 color classification