如何对一个哈希表中的列表进行排序?

3
我有以下列表:
Map<String, Integer> map = new HashMap<String, Integer>();
Map<String,  Map<Integer, Integer>> map2 = new HashMap<String, Map<Integer, Integer>>();

在我的映射列表中使用以下代码进行一些计算后,
map.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue()
    .reversed()).limit(1000).forEach(System.out::println);

它返回一个基于整数和名称排序的列表:
team1=1511
team4=1106
team2=805
team3=792

这意味着team1以1211的时间获得第一名,team4以1106的时间获得第二名,以此类推。

现在,在我的map2中,我希望能够按照车手的时间拥有相同的列表,但应该像这样排序:

team1=1511
team4=1106
team1=1010
team2=905
team2=892
team3=750
team3=740
team4=600

这意味着团队1以1511的时间获得第一名,然后是团队4以1106的时间,接着又是团队1以1010的时间等等。

(基本上,你有一个车队,每个车队至少有2名司机,在进行一些计算之后,你希望能够看到谁赢得了比赛)

对于地图2,我使用以下方法进行排序和返回:

 map2.entrySet().stream().sorted(Map.Entry.<String, Map<Integer, Integer>>comparingByKey()
    .reversed()).limit(1000).forEach(System.out::println);

但这样做并不能返回正确的结果。 我想知道是否可以使用哈希表来实现,或者甚至使用ArrayList?

你需要做什么来计算地图2中团队的时间? - Grzegorz Górkiewicz
@GrzegorzGórkiewicz 我已经用不同的方法进行了计算,我只想按照我上面解释的方式进行排序。 - Shervin Shemrani
new HashMap<String, Map<Integer, Integer>>();new HashMap<String, Integer>(); 不是相同的数据结构... 这就是为什么我不知道你把那些时间放在哪里 ;) - Grzegorz Górkiewicz
@GrzegorzGórkiewicz map2是一个列表,字符串是车队名称,另一个包含2个整数的列表基本上是该车队的驾驶员列表,这是在不同的方法中填充的。 - Shervin Shemrani
一个映射不是一个列表,如果你的意思是映射,请使用“map”这个词,而不是“list”。 - john16384
2个回答

0

另一种方法是...为什么不使用一个单独的类呢?

public class Standing implements Comparable<Standing> {

    private String team;
    private Integer time;

    public Standing(String team, Integer time) {
        this.team = team;
        this.time = time;
    }

    public String getTeam() {
        return this.team;
    }

    public Integer getTime() {
        return this.time;
    }

    @Override
    public String toString() {
        return this.team + "=" + this.time;
    }

    public int compareTo(Standing standing) {
        return (this.time).compareTo(standing.getTime());
    }
}

这里我会使用:

Map<String,  ArrayList<Integer>> map2 = new HashMap<String, ArrayList<Integer>();

这样:

TreeSet<Standing> standingSet = new TreeSet<Standing>();
for (String team : map2.keySet()) {
    standingSet.add(new Standing(team, map2.get(team).get(0)));
    standingSet.add(new Standing(team, map2.get(team).get(1)));
}

for (Standing standing : standingSet) {
    System.out.println(standing);
}

它不起作用,我得到了一个错误,因为Map<String,Map<Integer,Integer>> map2 = new HashMap<String,ArrayList<Integer,Integer>>(); - Shervin Shemrani
应该改为 Map<String, ArrayList<Integer, Integer>> map2 = new HashMap<String, ArrayList<Integer, Integer>>(); - Grzegorz Górkiewicz
不是真的再次出现这个错误:此行有多个标记
  • ArrayList<E> 的类型参数数量不正确;它不能使用 <Integer, Integer> 作为参数
  • ArrayList<E> 的类型参数数量不正确;它不能使用 <Integer, Integer> 作为参数
- Shervin Shemrani
1
虽然还不完全能用,但是看了你的解决方案后我有了一些想法。 - Shervin Shemrani
1
实际上,我已经成功运行了你的代码,谢谢! - Shervin Shemrani
显示剩余2条评论

0
假设map2包含:
team1: {
    1: 1506,
    2: 1010
},
team4: {
    1: 1106,
    2: 600
}

如果您想对其进行排序,可以执行以下操作:

List<DriverResult> results = map2.entrySet().stream()
    .flatMap(e -> e.getValue().entrySet().stream()
        .map(e2 -> new DriverResult(e.getKey(), e2.getKey(), e2.getValue()))
    )
    .sorted(Comparator.comparing(DriverResult::getTime))
    .collect(Collectors.toList());

还有这个辅助类:

public class DriverResult {
  private String team;
  private int number;
  private int time;

  public DriverResult(String team, int number, int time) {
    this.team = team;
    this.time = time;
    this.number = number;
  }

  public int getTime() {
    return time;
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接