簡體   English   中英

如何將集合中的元素按升序排序?

[英]How do I sort the elements of a set in ascending order?

我將足球元素添加到設置的團隊中,但是我想按已計算的升序對它們進行排序...我已經在足球課中使用了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

除了在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}]

注意,Team沒有實現Comparable ,但是其順序由創建TreeSet時提供的Comparator操縱。 您可以使用以下定義的任何一項或多項來對集合進行排序:

/**
 * 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());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM