当前位置:网站首页>Case - grade sorting - TreeSet set storage
Case - grade sorting - TreeSet set storage
2022-06-13 05:05:00 【Jason_ LH1024】
package com.it03;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class TreeSetDemo {
public static void main(String[] args) {
// establish TreeSet A collection of objects , Sort by comparator sort
// Anonymous inner class
Set<Student> set = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
// return s2.getSum() - s1.getSum(); // Descending From high to low Main conditions
int num = s2.getSum() - s1.getSum();
int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
// The total score is the same, arranged by language
// Sort by first name minor criteria
int num3 = num2 == 0 ? s1.getName().compareTo(s2.getName()) : num2;
return num3;
}
});
// Create student objects
Student s1 = new Student(" Liu ", 99, 99);
Student s2 = new Student(" king ", 87, 89);
Student s3 = new Student(" Zhang ", 89, 100);
Student s4 = new Student(" Li ", 87, 87);
Student s5 = new Student("la", 88, 88);
Student s6 = new Student("hanhan", 88, 88);
// Add student objects to the collection
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
set.add(s6);
// Ergodic set
for (Student s : set) {
System.out.println(s.getName() + ", Chinese language and literature :" + s.getChinese() + ", mathematics :" + s.getMath() + ", Total score :" + s.getSum());
}
}
}
use Comparator The comparator , Anonymous inner class
边栏推荐
- Clause 33: decltype is used for auto & type formal parameters, with std:: forward
- Section 8 - Practical commissioning techniques
- [multithreading] thread pool core class -threadpoolexecutor
- JS to realize the conversion between string and array and an interview question
- 【转载】C语言内存和字符操作函数大全
- 135. distribute candy
- PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes
- shell变量学习笔记
- Section 3 - functions
- 2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
猜你喜欢
使用EasyDarwin+FFmpeg实现rtsp推流
Sampo Lock
About mission planning and improving execution
Luogu p1036 number selection
Time display of the 12th Blue Bridge Cup
Several methods of identifying equivalent circuit of circuit drawing
Advanced C - Section 2 - pointers
RTSP streaming using easydarwin+ffmpeg
The games that you've tasted
Optocoupler working principle function electric parameter application circuit
随机推荐
priority inversion problem
Explain the role of key attribute in V-for
C language learning log 2.6
2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation
RuoYi-Cloud启动教程(手把手图文)
Solution to sudden font change in word document editing
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
shell变量学习笔记
Mysql8.0.13 installation tutorial (with pictures)
Advanced C language - Section 1 - data storage
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
Chapter 14 introduction: memory operation API
Kaggle 时间序列教程
UNO
Time display of the 12th Blue Bridge Cup
Flex布局自适应失效的问题
Understanding inode
Nonstandard compiler attribute extension
lookup
Several methods of identifying equivalent circuit of circuit drawing