Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java how to divide a treemap into two equal maps based on values

Okay I have this treemap that contains playerID and a players averageScore. I want to split it up into two other maps so each team has an even amount of players and the overall player score is around about the same(deviation of around +/-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);
   } 
like image 749
DiggidyDale Avatar asked Oct 30 '22 00:10

DiggidyDale


2 Answers

Try this out

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);

There is no reason to actually cast to a TreeMap because it doesn't provide any additional functionality over what a SortedMap does.

like image 187
Govinda Sakhare Avatar answered Nov 11 '22 00:11

Govinda Sakhare


The logic is there, you just need to finish the code to add each team to each map.

Make all the possible teams, compare their average with the previous best average. If the difference is below the previous, swap them.

At the end, you get the best difference between both.


An example of output

{false=[1=5.0, 4=6.5, 5=4.5, 8=7.6], true=[2=8.4, 3=2.1, 6=3.2, 7=9.8]}
// false = 23.6 in total             true = 23.5 in total
// As you see, the difference between both is the minimum possible (0.1)

public class Test {
    private Map<Integer, Double> tsm = new TreeMap<>();
    private Map<Integer, Double> team1 = new TreeMap<>();
    private Map<Integer, Double> team2 = new TreeMap<>();

    public void splitTeams() {
        double average = tsm.values()
                            .stream()
                            .mapToDouble(x->x)
                            .average()
                            .getAsDouble();
        int[] bestTeam = new int[4];
        double bestAverage = 10;
        double tmp;

        for (int i = 1 ; i <= 8 ; i++) {
            for (int j = 1 ; j <= 8 ; j++) {
                for (int k = 1 ; k <= 8 ; k++) {
                    for (int l = 1 ; l <= 8 ; l++) {
                        if (Stream.of(i, j, k, l).distinct().count() == 4) {
                            tmp = Stream.of(tsm.get(i), tsm.get(j), tsm.get(k), tsm.get(l))
                                        .mapToDouble(x -> x)
                                        .average()
                                        .getAsDouble();
                            if (Math.abs(average - tmp) < bestAverage) {
                                bestTeam[0] = i;
                                bestTeam[1] = j;
                                bestTeam[2] = k;
                                bestTeam[3] = l;
                                bestAverage = Math.abs(average - tmp);
                            }
                        }
                    }
                }
            }
        }

        List<Integer> team1 = Arrays.stream(bestTeam).boxed().collect(Collectors.toList());
        Map<Boolean, List<Entry<Integer, Double>>> both = tsm.entrySet()
                                                             .stream()
                                                             .collect(Collectors.partitioningBy(e -> team1.contains(e.getKey())));
        System.out.println(both);
    }

    public void createTeam() {
        tsm.put(1, 5.0);
        tsm.put(2, 8.4);
        tsm.put(3, 2.1);
        tsm.put(4, 6.5);
        tsm.put(5, 4.5);
        tsm.put(6, 3.2);
        tsm.put(7, 9.8);
        tsm.put(8, 7.6);
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.createTeam();
        t.splitTeams();
        System.out.println();
    }
}
like image 22
Yassin Hajaj Avatar answered Nov 11 '22 02:11

Yassin Hajaj