Why Java TreeMap does not print all its inserted records

Pls will tell you what is going wrong here. I have a class Personthat I use as a key in TreeMap. I Comparablealso implemented it so that I TreeMapcould sort.

public class Person implements Comparable{

private String name;
private int age;
// getters and setters were omitted
@Override
public int compareTo(Object o) {
    return 0;
}
}

Now I created TreeMapand added values ​​to it like:

Map treeMap=new TreeMap<Person,Object>();

treeMap.put(new Person(), "String1");
treeMap.put(new Person(), "String2");
treeMap.put(new Person(), "String3");
treeMap.put(new Person(), "String4");

System.out.println(treeMap);

After direct printing with System.out.println(treeMap);Iam, only getting the last inserted value, i.e.

Output: {Person@4aa36c=String4}

I know that keys must be different, but a new operator always creates a new object, so I find it beautiful. But I am helpless to find out what is happening here.

+4
source share
4

, , , , Person , - . Person , , , . `Person ':

public Person(String n, int a){
   this.name = n;
   this.age = a;
}

:

 Person p1 = new Person("Bob Jones", 32);
 treeMap.put(p1.getName(), person);

, TreeMap compareTo, , . , Person , compareTo. compareTo 0 , :

@Override
public int compareTo(Object o) {
    / TODO Auto-generated method stub
    return 0;
}

(, TODO).

, (SSN) Person:

Long ssn;

public void setSsn(Long value){
    ssn = value;
}

public Long getSsn(){
    return ssn;
}

ssn:

@Override
public int compareTo(Object o) {
    if(o == null) return 1;
    Person op = (Person)o;
    return ssn.compareTo(op.getSsn());
}

- , , , , :

@Override
public int compareTo(Object o) {
    if(o == null) return 1;
    Person op = (Person)o;
    return (name + age).compareTo(op.getName() + op.getAge());
}
+2

toCompare. 0. , , treeMap , .

( @4castle) .

@Override
public int compareTo(Person o) {
    if (age != o.age) return age > o.age ? 1 : -1;
    return name.compareTo(o.name);
}

public class Person implements Comparable<Pesron>
+2

TreeMap compareTo Comparable ( equals Object), put Map. compareTo 0 Person, TreeMap .

, Person, TreeMap , Person ( compareTo).

TreeMap.put Java-8

    // split comparator and comparable paths
    Comparator<? super K> cpr = comparator;
    if (cpr != null) {
        do {
            parent = t;
            cmp = cpr.compare(key, t.key);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }
    else {
        if (key == null)
            throw new NullPointerException();
        @SuppressWarnings("unchecked")
            Comparable<? super K> k = (Comparable<? super K>) key;
        do {
            parent = t;
            cmp = k.compareTo(t.key);
            if (cmp < 0)
                t = t.left;
            else if (cmp > 0)
                t = t.right;
            else
                return t.setValue(value);
        } while (t != null);
    }

, , Comparator, , TreeMap compare , compareTo Comparable .

+2

, . Person , , Person , "" Person, compareTo (0 ).

put - , . put, - () , , .

treeMap.put("String1", new Person());
treeMap.put("String2", new Person());
treeMap.put("String3", new Person());
treeMap.put("String4", new Person());

Comparable compareTo Person.

If you want the object to Personbe a key , you need to make each of them unique by providing it with a unique variable with which you can use the method correctly compareTo. The best solution is to add the required identification number to each object Personor otherwise use nameit ageas a unique value for the object. Add this code to Person:

private int id;
private static int numPeople = 0;
public Person() {
    numPeople++;
    id = numPeople;
    name = "";
}
@Override
public int compareTo(Person o) {
    if (age != o.age) return age > o.age ? 1 : -1;
    if (!name.equals(o.name) return name.compareTo(o.name);
    return id > o.id ? 1 : -1;
}

from

public class Person implements Comparable<Person>
0
source

All Articles