Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8067631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:23:18+00:00 2026-06-05T12:23:18+00:00

public class BigD{ public static void main(String[] args) { List<Employee> emps = new ArrayList<Employee>();

  • 0
public class BigD{
    public static void main(String[] args) {


        List<Employee> emps = new ArrayList<Employee>();
        emps.add(new Employee("Bal", "5"));
        emps.add(new Employee("Kiri", "7"));
        emps.add(new Employee("Pad", "2"));

        Map tree = new TreeMap();
        for(int i=0; i<emps.size(); i++) {
            Employee emp = null;
            emp = emps.get(i);
            System.out.println("hashcode : " + emp.hashCode());
           tree.put(emp, emp.getFirstNM()); // why is not keeping all three elements here ?
        }
        System.out.println(tree.size()); //why does it print the size as "1"
    }
}

class Employee implements Comparable {
    private String firstNM;
    private String lastNM;

    Employee(String firstNM, String lastNM) {
        this.firstNM = firstNM;
        this.lastNM = lastNM;
    }

    public String getFirstNM() {
        return firstNM;
    }
    public void setFirstNM(String firstNM) {
        this.firstNM = firstNM;
    }
    public String getLastNM() {
        return lastNM;
    }
    public void setLastNM(String lastNM) {
        this.lastNM = lastNM;
    }

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

Please let me know
why the treemap “tree” has just one element which is of employee object “Pad”, even though all three Employee objects which I am adding are having different hashcodes.
Is it because of I am not overriding equals/hashcode? if yes – why should I override when all are returning different hashcodes
Your thoughts would be appreciated.
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:23:19+00:00Added an answer on June 5, 2026 at 12:23 pm

    The problem is that when you put objects into the treemap, the treeMap uses employee.comparedTo method to see if you are inputting a new key. Since defined the comparedTo method to return 0 everytime (which means the two objects are the same), the treeMap will think that you are pairing different Employee value with the same key object every time you call treeMap.put. If you change the comparedTo to return -1, or appropriate value reflecting the order of the objects, then the treeMap.size() will return 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Employee { public static void main(String[] args) { int j=3; staples[] stemp
public class InterruptedInput { public static void main(String[] args) { InputThread th=new InputThread(); //worker
public class Start { public static void main(String[] args) { JFrame j = new
public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
public class Demo { public static void main(String args[]) { double d = 12.345;
public class Test{ public void Test(String name){}; public static void main() { Test t=new
public class Test { public static void main(String[] args) throws Exception { String s1
public class Main { public static void main (String args[]) { int nums[]= {2,
public class Main { public static void main(String[] args) throws InterruptedException { ClassA a
public class Test { public static void main(String[] args) { } } class Outer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.