JAVA排序的问题
发布网友
发布时间:2022-05-12 07:25
我来回答
共2个回答
热心网友
时间:2023-01-25 04:23
有很多方法实现,我写了一个,你看看,可以按需求自己修改
package com.random;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
public class Client {
public static void main(String[] args) {
Set<Student> set = new HashSet<Student>();
Student s1 = new Student(101, "刘备", "1班", 89.5f, 75f);
Student s2 = new Student(106, "关羽", "1班", 78f, 69f);
Student s3 = new Student(105, "张飞", "1班", 60.5f, 55f);
Student s4 = new Student(104, "诸葛亮", "2班", 99.5f, 100f);
Student s5 = new Student(103, "马超", "2班", 88.5f, 77f);
Student s6 = new Student(102, "典韦", "3班", 88.5f, 88f);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
set.add(s5);
set.add(s6);
System.out.println("--------未排序-------");
for(Student s : set){
System.out.println("学号:" + s.getId() + " 姓名:" + s.getName() + " 班级:" + s.getClassName()
+ " java成绩:" + s.getJavaScore() + " sql成绩:" + s.getSqlScore()
+ " 总成绩:" + (s.getJavaScore() + s.getSqlScore()) + " \n");
}
System.out.println("--------按学号升序排列-------");
Set<Student> ascId = new TreeSet<Student>(getComparator("id", 0));
ascId.addAll(set);
for(Student s : ascId){
System.out.println("学号:" + s.getId() + " 姓名:" + s.getName() + " 班级:" + s.getClassName()
+ " java成绩:" + s.getJavaScore() + " sql成绩:" + s.getSqlScore()
+ " 总成绩:" + (s.getJavaScore() + s.getSqlScore()) + " \n");
}
System.out.println("--------按学号降序排列-------");
Set<Student> descId = new TreeSet<Student>(getComparator("id", 1));
descId.addAll(set);
for(Student s : descId){
System.out.println("学号:" + s.getId() + " 姓名:" + s.getName() + " 班级:" + s.getClassName()
+ " java成绩:" + s.getJavaScore() + " sql成绩:" + s.getSqlScore()
+ " 总成绩:" + (s.getJavaScore() + s.getSqlScore()) + " \n");
}
System.out.println("--------按java成绩降序排列-------");
Set<Student> descJava = new TreeSet<Student>(getComparator("javaScore", 1));
descJava.addAll(set);
for(Student s : descJava){
System.out.println("学号:" + s.getId() + " 姓名:" + s.getName() + " 班级:" + s.getClassName()
+ " java成绩:" + s.getJavaScore() + " sql成绩:" + s.getSqlScore()
+ " 总成绩:" + (s.getJavaScore() + s.getSqlScore()) + " \n");
}
System.out.println("--------按sql成绩降序排列-------");
Set<Student> descSQL = new TreeSet<Student>(getComparator("sqlScore", 1));
descSQL.addAll(set);
for(Student s : descSQL){
System.out.println("学号:" + s.getId() + " 姓名:" + s.getName() + " 班级:" + s.getClassName()
+ " java成绩:" + s.getJavaScore() + " sql成绩:" + s.getSqlScore()
+ " 总成绩:" + (s.getJavaScore() + s.getSqlScore()) + " \n");
}
System.out.println("--------按总分降序排列-------");
Set<Student> descAVG = new TreeSet<Student>(getComparator("avgScore", 1));
descAVG.addAll(set);
for(Student s : descAVG){
System.out.println("学号:" + s.getId() + " 姓名:" + s.getName() + " 班级:" + s.getClassName()
+ " java成绩:" + s.getJavaScore() + " sql成绩:" + s.getSqlScore()
+ " 总成绩:" + (s.getJavaScore() + s.getSqlScore()) + " \n");
}
System.out.println("--------各班的java成绩平均分-------");
Set<Entry<String, Float>> javaScore = groupByClass(set, "java").entrySet();
for(Entry<String, Float> score : javaScore){
System.out.println("班级:" + score.getKey() + " 平均成绩:" + score.getValue() + " \n");
}
System.out.println("--------各班的sql成绩平均分-------");
Set<Entry<String, Float>> sqlScore = groupByClass(set, "sql").entrySet();
for(Entry<String, Float> score : sqlScore){
System.out.println("班级:" + score.getKey() + " 平均成绩:" + score.getValue() + " \n");
}
}
//condition:按那个条件排序,order=0 升序,order为其他 降序
public static Comparator<Student> getComparator(String condition, final int order){
if("id".equals(condition)){
return new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
if(order == 0){
return (s1.getId() > s2.getId() ? 1 : -1);
}else{
return (s1.getId() > s2.getId() ? -1 : 1);
}
}
};
}else if("javaScore".equals(condition)){
return new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
if(order == 0){
return (s1.getJavaScore() > s2.getJavaScore() ? 1 : -1);
}else{
return (s1.getJavaScore() > s2.getJavaScore() ? -1 : 1);
}
}
};
}else if("sqlScore".equals(condition)){
return new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
if(order == 0){
return (s1.getSqlScore() > s2.getSqlScore() ? 1 : -1);
}else{
return (s1.getSqlScore() > s2.getSqlScore() ? -1 : 1);
}
}
};
}else{
return new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
if(order == 0){
return ((s1.getJavaScore() + s1.getSqlScore()) > (s2.getJavaScore() + s2.getSqlScore()) ? 1 : -1);
}else{
return ((s1.getJavaScore() + s1.getSqlScore()) > (s2.getJavaScore() + s2.getSqlScore()) ? -1 : 1);
}
}
};
}
}
public static Map<String, Float> groupByClass(Set<Student> st, String subject ){
Map<String, Float> map = new HashMap<String, Float>();
//java成绩平均分
if("java".equals(subject)){
for(Student s : st){
Float scort = map.get(s.getClassName());
if(scort != null){
map.put(s.getClassName(), (scort + s.getJavaScore())/2);
}else{
map.put(s.getClassName(), s.getJavaScore());
}
}
//sql成绩平均分
}else{
for(Student s : st){
Float scort = map.get(s.getClassName());
if(scort != null){
map.put(s.getClassName(), (scort + s.getSqlScore())/2);
}else{
map.put(s.getClassName(), s.getSqlScore());
}
}
}
return map;
}
}
class Student {
private long id; //学号
private String name; //姓名
private String className; //班级名称
private float javaScore;
private float sqlScore;
public Student(long id, String name, String className,
float javaScore, float sqlScore){
this.id = id;
this.name = name;
this.className = className;
this.javaScore = javaScore;
this.sqlScore = sqlScore;
}
public Student(){};
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public float getJavaScore() {
return javaScore;
}
public void setJavaScore(float javaScore) {
this.javaScore = javaScore;
}
public float getSqlScore() {
return sqlScore;
}
public void setSqlScore(float sqlScore) {
this.sqlScore = sqlScore;
}
}
追问Student1 s1 = new Student1(101, "刘备", "1班", 89.5f, 75f);
这里的信息是我们自己输入的,不是固定的,我在输入的时候后面会把前面的覆盖,您能把上面的改成手动输入的吗,谢谢!
热心网友
时间:2023-01-25 05:41
select top 5 学号, 姓名, 班级, java成绩, SQL成绩, java成绩 + SQL成绩 as 总分 from Student
order by 学号, java成绩, SQL成绩, 总分
select 班级, avg(java成绩) as java成绩平均分, avg(Sql成绩) as Sql成绩平均分 from Student
group by 班级
求采纳追问我是要用java做而不是用数据库,谢谢