好的,我有一个包含玩家ID和玩家averageScore的树形图。我想将其分成另外两张 map ,以便每支球队的球员数量均等,并且总球员得分大致相同(偏差约为+/- 2)

private TreeMap<Integer, Double> teamScoreMap = new TreeMap<>();
private TreeMap<Integer, Double> team1 = new TreeMap<>();
private TreeMap<Integer, Double> team2 = new TreeMap<>();

public void createTeam()
   {
       teamScoreMap.put(001, 5.0);
       teamScoreMap.put(002, 8.4);
       teamScoreMap.put(003, 2.1);
       teamScoreMap.put(004, 6.5);
       teamScoreMap.put(005, 4.5);
       teamScoreMap.put(006, 3.2);
       teamScoreMap.put(007, 9.8);
       teamScoreMap.put(008, 7.6);
   }

最佳答案

试试看

TreeMap<Integer,Double> teamScoreMap = new TreeMap<Integer, Integer>(bigMap);
int size = teamScoreMap.size();
SortedMap<Integer, Double> team1 = teamScoreMap .subMap(0, size/2);
SortedMap<Integer, Double> team2 = teamScoreMap .subMap((size/2)+1,size);

没有必要实际转换为TreeMap,因为它没有提供SortedMap所做的任何其他功能。

09-12 11:18