美文网首页
Java 列表新增序号字段

Java 列表新增序号字段

作者: 蓝笔头 | 来源:发表于2021-07-08 19:11 被阅读0次

    定义测试所需的类

    public class RankDemo {
    
        public static void main(String[] args) {
            List<UserScore> userScores = initUserScore(10);
            userScores.stream().forEach(System.out::println);
        }
    
        private static List<UserScore> initUserScore(int count) {
            Random random = new Random();
            int top = 100;
            return IntStream.range(0, count).boxed()
                .map(idx -> new UserScore("haha_" + idx, random.nextInt(top)))
                .sorted(Comparator.comparingInt(UserScore::getScore).reversed())
                .collect(Collectors.toList());
        }
    
        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public static class UserScore {
            private String name;
            private int score;
        }
    
        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        @Builder
        public static class UserRank {
            private String name;
            private int score;
            private int rank;
        }
    }
    

    (方法一)使用 for 循环

    1)通过 for 循环新增序号

        public static List<UserRank> useFor(List<UserScore> userScores) {
            List<UserRank> result = new ArrayList<>();
            for (int i = 0; i < userScores.size(); ++ i) {
                UserScore userScore = userScores.get(i);
                result.add(UserRank.builder()
                    .rank(i + 1)
                    .name(userScore.getName())
                    .score(userScore.getScore())
                    .build());
            }
            return result;
        }
    

    2)测试代码:

        public static void main(String[] args) {
            List<UserScore> userScores = initUserScore(10);
            List<UserRank> userRanks = useFor(userScores);
            userRanks.stream().forEach(System.out::println);
        }
    

    控制台输出:

    RankDemo.UserRank(name=haha_1, score=97, rank=1)
    RankDemo.UserRank(name=haha_5, score=94, rank=2)
    RankDemo.UserRank(name=haha_3, score=90, rank=3)
    RankDemo.UserRank(name=haha_6, score=86, rank=4)
    RankDemo.UserRank(name=haha_7, score=86, rank=5)
    RankDemo.UserRank(name=haha_9, score=79, rank=6)
    RankDemo.UserRank(name=haha_0, score=67, rank=7)
    RankDemo.UserRank(name=haha_4, score=62, rank=8)
    RankDemo.UserRank(name=haha_8, score=24, rank=9)
    RankDemo.UserRank(name=haha_2, score=1, rank=10)
    

    (方法二)使用 stream 循环

    1)通过 stream() 循环和 AtomicInteger 新增序号

        public static List<UserRank> useStream(List<UserScore> userScores) {
            AtomicInteger rankIndex = new AtomicInteger(1);
            return userScores.stream()
                .map(userScore -> UserRank.builder()
                    .rank(rankIndex.getAndIncrement())
                    .name(userScore.getName())
                    .score(userScore.getScore())
                    .build())
                .collect(Collectors.toList());
        }
    

    2)测试代码:

        public static void main(String[] args) {
            List<UserScore> userScores = initUserScore(10);
            List<UserRank> userRanks = useStream(userScores);
            userRanks.stream().forEach(System.out::println);
        }
    

    控制台输出:

    RankDemo.UserRank(name=haha_4, score=83, rank=1)
    RankDemo.UserRank(name=haha_9, score=75, rank=2)
    RankDemo.UserRank(name=haha_0, score=71, rank=3)
    RankDemo.UserRank(name=haha_1, score=50, rank=4)
    RankDemo.UserRank(name=haha_6, score=48, rank=5)
    RankDemo.UserRank(name=haha_5, score=32, rank=6)
    RankDemo.UserRank(name=haha_2, score=21, rank=7)
    RankDemo.UserRank(name=haha_7, score=14, rank=8)
    RankDemo.UserRank(name=haha_8, score=4, rank=9)
    RankDemo.UserRank(name=haha_3, score=0, rank=10)
    

    相关文章

      网友评论

          本文标题:Java 列表新增序号字段

          本文链接:https://www.haomeiwen.com/subject/dkbtpltx.html