Pls tell what is wrong is happening here. I have a Person
class which I'm using as a key in TreeMap. I have implemented Comparable
also so that TreeMap
can do sorting.
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 TreeMap
and added values in it as:
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 printing directly using System.out.println(treeMap);
Iam only getting the last inserted value ie
Output:{Person@4aa36c=String4}
I know keys should be different but new operator always create a new object so I think its fine. But Iam helpless to figure out what is wrong going on here.