有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何比较和排序树集中的项目?

我有一门课叫《玩家与输赢》

public class Player
{
    private String          name ; 
    private int             ranking=0; 
    private int             wins = 0 ;
    private int             lossess = 0 ; 


    private boolean         isPrivate =false; 
    private int             experiance =0; 

public float getWinRatio()
{ 
     if( getExperiance() <= 0 )  return -0.0f ;  
     return  (getWins()/(float)getExperiance()) ;         
}

我有一个有10000个玩家对象的树集。我如何将它们相互比较,然后根据这个赢比函数对它们进行排序


共 (2) 个答案

  1. # 1 楼答案

    您需要在类中正确地声明方法,以便对其进行编译。但是一旦你这样做了,你就可以像这样把一个Comparator传递给TreeSet

      Set<Player> players = new TreeSet<>(Comparator.comparing(Player::getWinRatio));
    

    在这里,我只是将值添加到树集

          int[] numbs = { 5, 3, 1, 4, 2, 9, 7, 6, 8
          };
    
          // Integer has a natural ordering in ascending order so no
          // Comparator was needed 
          Set<Integer> set = new TreeSet<>();
          for (int n : numbs) {
             set.add(n);
          }
    

    瞧!它们被分类了

          System.out.println(set);
    

    如果你想按相反的顺序对它们进行排序,你可以这样分配树集

            Set<Integer> set = new TreeSet<>(Comparator.reverseOrder());
    

    对于你的胜利配给排序,你可以像这样颠倒顺序

     Set<Player> players =
                new TreeSet<>(Comparator.comparing(Player::getWinRatio).reversed());
    
  2. # 2 楼答案

    你需要用Comparator创建一个新的TreeSet,它根据winRatio比较玩家。然后,你只需要把玩家从原来的TreeSet复制到新的TreeSet。以下是参考代码:

    import java.util.Comparator;
    import java.util.TreeSet;
    
    class Player {
        private String name;
        private int ranking = 0;
        private int wins = 0;
        private int lossess = 0;
        private boolean isPrivate = false;
        private int experiance = 0;
    
        public Player(String name, int ranking, int wins, int lossess, boolean isPrivate, int experiance) {
            super();
            this.name = name;
            this.ranking = ranking;
            this.wins = wins;
            this.lossess = lossess;
            this.isPrivate = isPrivate;
            this.experiance = experiance;
        }
    
        String getName() {
            return name;
        }
    
        public int getExperiance() {
            return experiance;
        }
    
        public int getWins() {
            return wins;
        }
    
        public float getWinRatio() {
            if (getExperiance() <= 0)
                return -0.0f;
            return (getWins() / (float) getExperiance());
        }
    
        @Override
        public String toString() {
            return "Player [name=" + name + ", ranking=" + ranking + ", wins=" + wins + ", lossess=" + lossess
                    + ", isPrivate=" + isPrivate + ", experiance=" + experiance + "]";
        }
    }
    
    class DefaultComparator implements Comparator<Player> {
        @Override
        public int compare(Player p1, Player p2) {
            return p1.getName().compareTo(p2.getName());
        }
    }
    
    class PlayerWinRatioComparator implements Comparator<Player> {
        @Override
        public int compare(Player p1, Player p2) {
            return p1.getWinRatio() > p2.getWinRatio() ? 1 : (p1.getWinRatio() == p2.getWinRatio() ? 0 : -1);
        }
    }
    
    public class TreeSetExample {
        public static void main(String[] args) {
            TreeSet<Player> original = new TreeSet<Player>(new DefaultComparator());
            original.add(new Player("A", 5, 1, 2, true, 4));
            original.add(new Player("B", 4, 2, 1, false, 5));
            original.add(new Player("C", 2, 3, 2, true, 3));
            original.add(new Player("X", 3, 2, 3, false, 2));
            original.add(new Player("Y", 6, 4, 2, true, 1));
            original.add(new Player("Z", 1, 2, 4, true, 6));
    
            TreeSet<Player> copy = new TreeSet<Player>(new PlayerWinRatioComparator());
    
            // Copy the elements of original TreeSet to the copy TreeSet
            for (Player player : original) {
                copy.add(player);
            }
    
            //Display the elements copy TreeSet
            for (Player player : copy) {
                System.out.println(player);
            }
        }
    }
    

    输出:

    Player [name=A, ranking=5, wins=1, lossess=2, isPrivate=true, experiance=4]
    Player [name=Z, ranking=1, wins=2, lossess=4, isPrivate=true, experiance=6]
    Player [name=B, ranking=4, wins=2, lossess=1, isPrivate=false, experiance=5]
    Player [name=C, ranking=2, wins=3, lossess=2, isPrivate=true, experiance=3]
    Player [name=Y, ranking=6, wins=4, lossess=2, isPrivate=true, experiance=1]