如何按升序对集合的元素进行排序?

时间:2015-02-16 19:23:36

标签: java sorting set

我将橄榄球元素添加到设定的团队中,但是我想按照我计算的点数递增它们...我已经在足球课程中使用了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());
}

}

2 个答案:

答案 0 :(得分:1)

您的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

答案 1 :(得分:0)

不是在Team上实现Comparable接口,而是可以为不同的排序实现Comparators,而不是为特定类型绑定一种。考虑:

public class OrderingTest {
    public static void main(String[] args) {
        Set<Team> setAlphaAscending = new TreeSet<>(new AscendingNameComparator());
        addTeamsToSet(setAlphaAscending);

        Set<Team> setAlphaDescending = new TreeSet<>(new DescendingNameComparator());
        addTeamsToSet(setAlphaDescending);

        Set<Team> setPointsOrdered = new TreeSet<>(new PointsComparator());
        addTeamsToSet(setPointsOrdered);

        System.out.println(setAlphaAscending);
        System.out.println(setAlphaDescending);
        System.out.println(setPointsOrdered);
    }

    private static void addTeamsToSet(Set<Team> teamSet) {
        teamSet.add(new Team("Austin", 3));
        teamSet.add(new Team("Bermuda", 10));
        teamSet.add(new Team("Colorado", 7));
        teamSet.add(new Team("Denver", 1));
    }
}

class Team {
    String name;
    Integer points;

    Team(String name, Integer points) {
        this.name = name;
        this.points = points;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPoints() {
        return points;
    }

    public void setPoints(Integer points) {
        this.points = points;
    }

    @Override
    public String toString() {
        return "Team{" +
                "name='" + name + '\'' +
                ", points=" + points +
                '}';
    }
}

// Output
// [Team{name='Austin', points=3}, Team{name='Bermuda', points=10}, Team{name='Colorado', points=7}, Team{name='Denver', points=1}]
// [Team{name='Denver', points=1}, Team{name='Colorado', points=7}, Team{name='Bermuda', points=10}, Team{name='Austin', points=3}]
// [Team{name='Denver', points=1}, Team{name='Austin', points=3}, Team{name='Colorado', points=7}, Team{name='Bermuda', points=10}]

请注意,团队未实施Comparable,但其排序由创建Comparator时提供的TreeSet操纵。您可以使用以下任何一种或多种定义来对集合进行排序:

/**
 * String comparator for ascending names
 */
class AscendingNameComparator implements Comparator<Team> {
    @Override
    public int compare(Team team1, Team team2) {
        return team1.getName().compareTo(team2.getName());
    }
}

/**
 * String comparator for descending names
 */
class DescendingNameComparator implements Comparator<Team> {
    @Override
    public int compare(Team team1, Team team2) {
        return team2.getName().compareTo(team1.getName());
    }
}

/**
 * Sort ascending on each teams points
 */
class PointsComparator implements Comparator<Team> {
    @Override
    public int compare(Team o1, Team o2) {
        return o1.getPoints().compareTo(o2.getPoints());
    }
}