当前位置:网站首页>Collection framework generic LinkedList TreeSet
Collection framework generic LinkedList TreeSet
2022-06-27 08:46:00 【Ten thousand volt sun】
//package LinkedList;
import java.util.*;
public class TryLinkedlist {
public static void main(String args[ ]) {
List<Student> list = new LinkedList<Student>();
list.add(new Student(" Zhang San ",86));
list.add(new Student(" Li Si ",83));
list.add(new Student(" Zhaodalong ",89));
list.add(new Student(" Li Yunlong ",80));
Iterator<Student> iter=list.iterator();
int sum=0;
System.out.println(" Before ordering , Data in the linked list :");
while(iter.hasNext()){
Student stu=iter.next();
System.out.println(stu.name+ " achievement :"+stu.score);
}
System.out.println(" Sort ( By grade ) after , Data in the linked list :");
/* Perfect the code : Use Collection Class sort Method to sort and output —— Attention format */
Collections.sort(list,new Comparator<Student>(){
public int compare(Student o1, Student o2) {
return o1.score-o2.score;
}
});
Iterator<Student> iter2=list.iterator();
while(iter2.hasNext()){
Student stu=iter2.next();
sum+=stu.score;
System.out.println(stu.name+ " achievement :"+stu.score);
}
System.out.println(" Average score :"+sum/list.size());
/* Perfect the code : Use Linkedlist Class size Method average score —— Attention format */
}
}
class Student implements Comparable {
String name;
int score;
Student(String name,int score) {
this.name=name;
this.score=score;
}
public int compareTo(Object b) {
Student st=(Student)b;
return (this.score-st.score);
}
}
//package TreeSet;
import java.util.*;
public class TryTreeSet{
public static void main(String args[]) {
List<Student> list=new LinkedList<Student>();
int score []={
65,76,45,99,77,88,100,79};
String name[]={
" Zhang San "," Li Si "," peak season "," Gago "," Why "," Weeks and "," Zhao Li "," Set "};
for(int i=0;i<score.length;i++){
list.add(new Student(score[i],name[i]));
}
Iterator<Student> iter=list.iterator();
TreeSet<Student> mytree=new TreeSet<Student>();
while(iter.hasNext()){
Student stu=iter.next();
mytree.add(stu);
}
/* Perfect the code : take list The data in is input into the tree set */
Iterator<Student> te=mytree.iterator();
/* Perfect the code : Output tree set sorting results */
while(te.hasNext()){
Student stu=te.next();
System.out.println(stu.name+" "+stu.english);
}
}
}
class Student implements Comparable {
int english=0;
String name;
Student(int english,String name) {
this.name=name;
this.english=english;
}
public int compareTo(Object b) {
Student st=(Student)b;
return (this.english-st.english);
}
}
边栏推荐
猜你喜欢
快捷键 bug,可复现(貌似 bug 才是需要的功能 [滑稽.gif])

JVM层次上的对象的创建过程和内存布局

2022.06.26(LC_6100_统计放置房子的方式数)

That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade

ServletConfig and ServletContext

JVM常见的垃圾收集器

Understanding mvcc in MySQL transactions is super simple

Redis master-slave replication and sentinel mode

经典的一道面试题,涵盖4个热点知识

DataV轮播表组件dv-scroll-board宽度问题
随机推荐
win10为任意文件添加右键菜单
第十一章 信号(一)- 概念
(original) custom drawable
The largest rectangle in the bar graph of force buckle 84
Introduction to websocket protocol
Rough reading DS transunet: dual swing transformer u-net for medical image segmentation
Brief introduction to SSL encryption process
Rockermq message sending mode
Lvgl usage demo and instructions 2
MySQL锁详解
[original] typescript string UTF-8 encoding and decoding
Understanding mvcc in MySQL transactions is super simple
Fake constructor???
Internal class ~ lock ~ access modifier
ServletConfig与ServletContext
2022.6.26-----leetcode.710
vim 从嫌弃到依赖(19)——替换
CLassLoader
UE5神通--POI解决方案
一种太阳能电荷泵供电电路的方案设计