TreeMap potrzebna wiedza teoretyczna z wyświetlania mapy object,object

0

Tak jak w temacie, nie za bardzo czaję czemu wyświetlana jest wartość key z pierwszego PUT'a ale już value z druga PUT'a....

Oto kod (kod czysty ?):

import java.util.Map;
import java.util.TreeMap;

class ClassA implements Comparable{
    private int phoneNumber;
    public ClassA(int newPhoneNumber) {
        phoneNumber = newPhoneNumber;
    }

    public String toString(){
        return "" + phoneNumber;
    }

    public static void main(String[] args) {
        TreeMap<ClassA, Object> treeMap = new TreeMap<> ();

        ClassB bob = new ClassB ("bob", "boss", 12345);
        ClassA phoneBob = new ClassA (bob.phoneNumber);

        ClassC bobAddress = new ClassC ("jamaica", 567890);
        ClassA phoneBobAddress = new ClassA (bobAddress.phoneNumber);

        treeMap.put (phoneBob,bob);
        treeMap.put (phoneBobAddress,bobAddress);

//        printTreeMap(treeMap);
        for(Map.Entry<ClassA, Object> entry : treeMap.entrySet ()){
            System.out.println (entry.getKey () + "/" + entry.getValue () );
        }
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }
}

class ClassB{
    private String name, surname;
    int phoneNumber;

    ClassB(String name, String surname, Integer phoneNumber){
        this.name = name;
        this.surname = surname;
        this.phoneNumber = phoneNumber;
    }
    public String toString(){
        return this.name + ", " + this.surname;
    }

}

class ClassC{
    private String address;
    int phoneNumber;

    ClassC( String address, Integer phoneNumber){
        this.address = address;
        this.phoneNumber = phoneNumber;
    }
    public String toString(){
        return this.address;
    }
}

output:

12345/jamaica

0

Masz metodę compareTo, która zawsze zwraca zero, a to znaczy, że jak dodasz kolejny obiekt do mapy, to on będzie przypisany do tego klucza, który już tam jest. Tylko wartość się zmieni.

0
chodnik napisał(a):

Masz metodę compareTo, która zawsze zwraca zero, a to znaczy, że jak dodasz kolejny obiekt do mapy, to on będzie przypisany do tego klucza, który już tam jest. Tylko wartość się zmieni.

Ale to chyba służy do porównywania np. key i sortować je...

0

Nie. @chodnik ma rację. compareTo służy też do porównywania czy klucze są identyczne. Wystarczy zobaczyć do dokumentacji i w ciągu sekund można zauważyć taki oto akapit:

Note that the ordering maintained by a tree map, like any sorted map, and whether or not an explicit comparator is provided, must be consistent with equals if this sorted map is to correctly implement the Map interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Map interface is defined in terms of the equals operation, but a sorted map performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the sorted map, equal. The behavior of a sorted map is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Map interface.

1 użytkowników online, w tym zalogowanych: 0, gości: 1