0

I have implemented following classes:

public class Team - has a SortedMap<String, Player> as field. public class Player implements Comparator - has the method int compare(Object o1, Object o2)

Now, I want to have the Map always sorted when putting in String, Player.

How can I realize that, because the Comparator must work with the value Vand not with the key K. However, there is only a constructor with a Comparator of the key.

Thanks!

public class Player implements Comparator 

@Override
public int compare(Object o1, Object o2) {
    if(o1.getClass() != o2.getClass()) return 0;
    if(((int)((Player)o1).getName().charAt(0)) == ((int)((Player)o2).getName().charAt(0))) {
        if(((Player)o1).getNumber() == ((Player)o2).getNumber()) {
            return 0;
        } else if(((Player)o1).getNumber() < ((Player)o2).getNumber()) return -1;
        else return 1;
    } else if(((int)((Player)o1).getName().charAt(0)) < ((int)((Player)o2).getName().charAt(0))) {
        return -1;
    } else return 1;
}

The Team class:

public class Team {

    private SortedMap<String,Player> team;

    /**
     * This is the default constructor without parameters. It initializes the team Map
     */
    public Team() {
        team = new TreeMap(new Player(null, 0, 0));
    }

    /**
     * This method adds a new Player to the team-Map
     * @param player is the player to add to the team-Map
     */
    public void put(Player player) {
        if(player != null) {
           String name = player.getName();
           team.put(name, player);
        }
    }
Iskuskov Alexander
  • 4,077
  • 3
  • 23
  • 38
mittermor
  • 51
  • 7
  • 1
    `public class Player implements Comparator ` do yourself a favour, declare it as `public class Player implements Comparator`. Actually, it shouldn't be a `Comparator`, but rather `Comparable`. – Andy Turner Jan 11 '20 at 19:54
  • 1
    A SortedMap is sorted by its keys, not by its values. There's no way around it. It wouldn't make sense to sort by value. What's the concrete problem you're trying to solve? Why do you ue a map in the first place (two players never have the same name??), and what is the ordering used for? – JB Nizet Jan 11 '20 at 19:56
  • our exercise: In the exercise „Soccer Team“ store the players in a sorted map. Players should be compared with their name, or with their number as a second sorting criterion. – mittermor Jan 11 '20 at 20:10
  • What *specifically* do your instructions state regarding which type should be the map's key and which type should be the map's value? Why not use Player as the key for your Map? And what is the map to be used for? – Hovercraft Full Of Eels Jan 11 '20 at 20:15
  • Map soccerteam - where String is the name and Player the Player object {goals: ..., country: ..., ... } – mittermor Jan 11 '20 at 20:19
  • 1
    I would understand a SortedSet, but a SortedMap makes no sense to me. Ask your teacher for clarifications: what should be the keys, what should be the values, why use a Map. – JB Nizet Jan 11 '20 at 20:21

0 Answers0