我将足球元素添加到设置的团队中,但是我想按已计算的升序对它们进行排序...我已经在足球课中使用了compareTo方法。我想知道最有效的方法是按升序对集合进行排序。

这是一些测试代码

 Georgia Southern(Sun Belt)|9|0|0|0|0|0|0|1
 Louisana-Lafayette(Sun Belt)|36|0|0|0|0|4|0|1
 Appalachian State(Sun Belt)|7|0|0|0|0|0|0|0
 Texas State(Sun Belt)|17|0|0|0|0|0|0|0
 Arakansas State(Sun Belt)|35|0|0|0|0|2|2|3
 South Alabama(Sun Belt)|14|0|0|0|0|0|1|0


import java.util.*;
import java.util.Collection;
import java.io.*;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Schedule
{
  public static void main (String args[])throws IOException
 {
    Scanner sc=new Scanner(new File("Footballteams.txt"));
    Set<Football>teams=new TreeSet<Football>();

    for(int i=0;i<128;i++)
    {
        String team=sc.nextLine();
        Integer points=0;
        String[]a=team.split("\\|",9);
        String name=a[0];
        int wins=Integer.parseInt(a[1]);
        points+=wins;
        int finalRecord14=Integer.parseInt(a[2]);
        int finalRecord13=Integer.parseInt(a[3]);
        int finalRecord12=Integer.parseInt(a[4]);
        int finalRecord11=Integer.parseInt(a[5]);
        int bowlVictories=Integer.parseInt(a[6]);
        points=points+(bowlVictories*10);
        int bowlLosses=Integer.parseInt(a[7]);
        points=points-(bowlLosses*5);
        int ConferenceChamp=Integer.parseInt(a[8]);
        points=points+(ConferenceChamp*10);
        Football x=new Football(name,wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamp,points);
        teams.add(x);


    }

    for(Football x:teams)
        System.out.println(x.name);











}


}

 public class Football implements Comparable<Football>
{
    public String name;
   public int wins,finalRecord14,finalRecord13,finalRecord12,finalRecord11,bowlVictories,bowlLosses,ConferenceChamps;
   public Integer points;
   public Football(String name,int wins,int finalRecord14,int finalRecord13,int finalRecord12,int finalRecord11,int bowlVictories,int bowlLosses,int ConferenceChamp,Integer points)
   {
       this.name=name;
       this.wins=wins;
       this.finalRecord14=finalRecord14;
       this.finalRecord13=finalRecord13;
       this.finalRecord12=finalRecord12;
       this.finalRecord11=finalRecord11;
       this.bowlVictories=bowlVictories;
       this.bowlLosses=bowlLosses;
       this.ConferenceChamps=ConferenceChamp;
       this.points=points;
}
public String getName()
{
    return name;
}
public int getWins()
{
    return wins;
}
public int getfinalRecord14()
{
    return finalRecord14;
}
public int getfinalRecord13()
{
    return finalRecord13;
}
public int getfinalRecord12()
{
    return finalRecord12;
}
public int getfinalRecord11()
{
    return finalRecord11;
}
public int getBowlVictories()
{
    return bowlVictories;
}
public int getBowlLosses()
{
    return bowlLosses;
}
public int getConferenceChamps()
{
    return ConferenceChamps;
}
public Integer getPoints()
{
    return points;
}
public int compareTo(Football o)
{

    return getPoints().compareTo(o.getPoints());
}


}

最佳答案

您的Football类应实现Comparable接口并覆盖其compareTo方法。

public class Football implements Comparable<Football> {
    protected String name;
    protected Integer points;

    public Football(String name, Integer points) {
        setName(name);
        setPoints(points);
    }

    // getters & setters

    @Override
    public int compareTo(Football o) {
        return getPoints().compareTo(o.getPoints());
    }
}


Schedule模拟:

import java.util.Set;
import java.util.TreeSet;

public class Schedule {
    public static void main(String[] args) {
        Set<Football> teams = new TreeSet<>();

        teams.add(new Football("Team A", 3));
        teams.add(new Football("Team B", 1));
        teams.add(new Football("Team C", 2));

        for (Football x : teams) {
            System.out.println(x.getName());
        }
    }
}


根据团队的得分输出:

Team B
Team C
Team A

关于java - 如何将集合中的元素按升序排序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28548842/

10-10 05:53