获取迭代ArrayList值的排名

时间:2016-08-19 06:23:28

标签: java

我在像{50,25,50,30,10}这样的ArrayList中有五个值。我想在每个值中设置排名,所以请告诉我如何才能做到这一点。输出就像

50->1st rank
50->1st rank
30->2nd rank
20->3rd rank
10->4th rank

2 个答案:

答案 0 :(得分:1)

您应该检查thisthis

使用java 1.8中提供的lambda expression很容易。

以下是我从上面的参考链接

获得的代码
List<Player> players = new ArrayList<Player>() {{
    add(new Player(1L, "a", 5));
    add(new Player(2L, "b", 7));
    add(new Player(3L, "c", 8));
    add(new Player(4L, "d", 9));
    add(new Player(5L, "e", 3));
    add(new Player(6L, "f", 8));
 }};
 int[] score = {Integer.MIN_VALUE};
 int[] no = {0};
 int[] rank = {0};
 List<Ranking> ranking = players.stream()
     .sorted((a, b) -> b.getScores() - a.getScores())
     .map(p -> {
         ++no[0];
         if (score[0] != p.getScores()) rank[0] = no[0];
         return new Ranking(rank[0], score[0] = p.getScores());
     })
     // .distinct() // if you want to remove duplicate rankings.
     .collect(Collectors.toList());
 System.out.println(ranking);

// result:
// rank=1, score=9
// rank=2, score=8
// rank=2, score=8
// rank=4, score=7
// rank=5, score=5
// rank=6, score=3

答案 1 :(得分:0)

请尝试此代码。

    package snakePack;

    import java.util.ArrayList;

    public class MainSnake {

        public static void main(String[] args) {

            Snake blackMambo = new Snake("blackMambo",2,0);
            Snake rattle = new Snake("rattle",9,0);
            Snake green = new Snake("green",6,0);
            Snake cobra = new Snake("cobra",78,0);
            Snake kingCobra = new Snake("kingCobra",5,0);
            Snake whiteCobra = new Snake("whiteCobra",5,0);
            Snake python = new Snake("python",5,0);
            Snake yellow = new Snake("yellow",5,0);
            Snake blackCobra = new Snake("blackCobra",1000,0);
            Snake desertCobra = new Snake("desertCobra",5,0);

            ArrayList<Snake> list = new ArrayList<Snake>();
            list.add(blackMambo);list.add(rattle);list.add(green);list.add(cobra);list.add(kingCobra);
            list.add(whiteCobra);list.add(python);list.add(yellow);list.add(blackCobra);list.add(desertCobra);

            ArrayList<Integer> mongoosebigSnakes = new ArrayList<Integer>();

            Integer tempHelperFrog = 0;
            Integer rankMe = 0;
            for (int grossHopper = 0; grossHopper < list.size(); grossHopper++) {
                Integer strongSnake = 0;
                for (int spider = 0; spider < list.size(); spider++) {
                    if (list.get(spider).getSnakePoisionRate() > strongSnake) {
                        boolean bool = false;
                        if (mongoosebigSnakes.size() != 0) {
                            for (int dragonFly = 0; dragonFly < mongoosebigSnakes.size(); dragonFly++) {
                                if (mongoosebigSnakes.get(dragonFly).intValue() == list.get(spider).getSnakePoisionRate().intValue()) {
                                    bool = true;
                                }
                            }
                            if (bool != true) {
                                bool = false;
                                strongSnake = list.get(spider).getSnakePoisionRate();
                            }
                        } else {
                            if (tempHelperFrog != list.get(spider).getSnakePoisionRate()) {
                                strongSnake = list.get(spider).getSnakePoisionRate();
                            }
                        }

                    }
                }
                tempHelperFrog = strongSnake;
                mongoosebigSnakes.add(strongSnake);
                ++rankMe;
                for (int x = 0; x < list.size(); x++) {
                    if (strongSnake == list.get(x).getSnakePoisionRate()) {
                        list.get(x).setSnakeRank(rankMe);
                    }
                }
            }

            System.out.println(" Hey guys get ready to see who has strong poison >>>");
            Integer ratHelperCounter = 0;
            for (Snake snake : list) {
                System.out.println(++ratHelperCounter + " :" + snake.toString());
            }
            System.out.println("it's awesome" + " huge me !!!! ");
        }

    }

Bean类:

package snakePack;

public class Snake {

    String snakeName;
    Integer snakePoisionRate;
    Integer snakeRank;

    public Snake(String string, int i, int j) {
        this.snakeName = "";
        this.snakePoisionRate = i;
        this.snakeRank = j;
    }

    public String getSnakeName() {
        return snakeName;
    }
    public void setSnakeName(String snakeName) {
        this.snakeName = snakeName;
    }
    public Integer getSnakePoisionRate() {
        return snakePoisionRate;
    }
    public void setSnakePoisionRate(Integer snakePoisionRate) {
        this.snakePoisionRate = snakePoisionRate;
    }
    public Integer getSnakeRank() {
        return snakeRank;
    }
    public void setSnakeRank(Integer snakeRank) {
        this.snakeRank = snakeRank;
    }

    @Override
    public String toString() {
        return "Snake [snakeName=" + snakeName + ", snakePoisionRate="
                + snakePoisionRate + ", snakeRank=" + snakeRank + "]";
    }       
}