在Java中同时对两个ArrayList进行排序

问题描述 投票:0回答:3

我有两个 ArrayList,一个包含团队名称(字符串),另一个包含各自团队的总得分(整数)。我想从最高分到最低分进行排序,同时保持分数与团队名称一致。

我对整个编程不够熟悉,不知道如何解决这个问题,而且我在这里找不到任何直接相关的内容

ArrayList<String> nameList = new ArrayList<String>();
ArrayList<Integer> pointsList = new ArrayList<Integer>();
for (int i = 0; i < competitionList.size(); i++)
{
    int pointTotal = 0;
    if (nameList.contains(competitionList.get(i).getTeam()))
    {
    }
    else
    {
        nameList.add(competitionList.get(i).getTeam());
        for (int a = 0; a < competitionList.size(); a++)
        {
            if (competitionList.get(a).getTeam().equals(competitionList.get(i).getTeam()))
            {
                pointTotal += competitionList.get(a).getPoints();
            }
        }
        pointsList.add(pointTotal);
    }

然后想在这里排序

java sorting arraylist
3个回答
2
投票

收集记录对象

定义一个 record 类来保存每个数组中的匹配值。

record Team ( String name , int points ) {}

列出这些对象。

List < Team > teams = new ArrayList <> () ;

循环输入数组,为每行实例化一个

Team
对象。

teams.add( new Team( nameArray[ index ] , pointsArray[ index ] ) ) ;

Comparator

排序

定义

Comparator
进行排序。

teams.sort( 
    Comparator
        .comparing( Team :: points )
        .reversed()
);

0
投票
import java.util.ArrayList;

public class Test {

static class Obj {

        String name;

        Integer point;

        @Override
        public String toString() {
            return "name: " + name + ", point: " + point;
        }
    }

    public static void main(String[] args) {

        ArrayList<Obj> objList = new ArrayList<>();

        Obj obj = new Obj();
        obj.name = "1";
        obj.point = 100;
        objList.add(obj);

        obj = new Obj();
        obj.name = "2";
        obj.point = 20;
        objList.add(obj);

        obj = new Obj();
        obj.name = "3";
        obj.point = 30;
        objList.add(obj);

        // 100,30,20
        objList.sort((a,b) -> b.point - a.point);
        // 20,30,100
        // objList.sort((a,b) -> a.point - b.point);
        System.out.println(objList);
    }
}

0
投票
  • 第1步:统计各队的总分

  • step2:按点数排序

public class Test {
    public static void main(String[] args) {
        List<CompetitionRecord> competitionList = new ArrayList<>();
        competitionList.add(new CompetitionRecord("teamA", 10));
        competitionList.add(new CompetitionRecord("teamB", 30));
        competitionList.add(new CompetitionRecord("teamB", 20));
        competitionList.add(new CompetitionRecord("teamC", 40));

        // tally the total points of each team
        Map<String, Integer> teamToPointsMap = new HashMap<>();
        for (CompetitionRecord competitionRecord : competitionList) {
            String team = competitionRecord.getTeam();
            Integer points = competitionRecord.getPoints();

            Integer oldPoints = teamToPointsMap.getOrDefault(team, 0);
            teamToPointsMap.put(team, oldPoints + points);
        }

        // sort by value(points)
        List<Map.Entry<String, Integer>> sortedList = teamToPointsMap.entrySet().stream()
                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
                .toList();
        
        // print result
        for (int i = 0; i < sortedList.size(); i++) {
            Map.Entry<String, Integer> entry = sortedList.get(i);
            System.out.println("i=" + i + ", team=" + entry.getKey() + ", points=" + entry.getValue());
        }
        /*
        output:
        i=0, team=teamB, points=50
        i=1, team=teamC, points=40
        i=2, team=teamA, points=10
         */
    }

    record CompetitionRecord(String team, Integer points) {
        public String getTeam() {
            return team;
        }

        public Integer getPoints() {
            return points;
        }
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.