This discussion is archived
8 Replies Latest reply: Jul 3, 2012 5:47 AM by EJP RSS

Java Collections

938022 Newbie
Currently Being Moderated
Hi friends,
this is irfan and
I want to Create a class called Employee with attributes name and salary.
and if i Pass employee objects as values to map and do the following.
a.Print all the entries.
b.Remove an entry.
c.Replace an entry.
d.Look up a value.
e.Iterate through all the entries.
I am not getting the actual data but Im getting the hashcode when i used put(key as integer ,Value as Employee Obj)
when i tried to print the details im getting hashcode value how to print each Employee obj as value of corresponding integer key from map put()
  • 1. Re: Java Collections
    796440 Guru
    Currently Being Moderated
    935019 wrote:
    I want to Create a class called Employee with attributes name and salary.
    and if i Pass employee objects as values to map and do the following.
    a.Print all the entries.
    b.Remove an entry.
    c.Replace an entry.
    d.Look up a value.
    e.Iterate through all the entries.
    None of this really tells us anything useful.
    I am not getting the actual data but Im getting the hashcode
    No, you are getting out exactly what you put in. But when you do System.out.println(someEmployee), you haven't told Java what it should print. Since didn't override the toString() method, in Employee, it's using the one inherited from Object, and since println() ends up calling toString(), that's what you're seeing.

    Google for java override toString examples if you don't know how to do it.
  • 2. Re: Java Collections
    942513 Newbie
    Currently Being Moderated
    An Elementry sample code for this is


    public class Emp {
         
         private String name;
         private Long sal;
         
         public Emp(String name, Long sal) {
              this.name = name;
              this.sal = sal;
         }
         
         public String getName() {
              return name;
         }
         public void setName(String name) {
              this.name = name;
         }
         public Long getSal() {
              return sal;
         }
         public void setSal(Long sal) {
              this.sal = sal;
         }
         
         @Override
         public String toString() {
              StringBuilder builder = new StringBuilder();
              builder.append(name + ", ");
              builder.append(sal.toString());
              return builder.toString();
         }

    }
  • 3. Re: Java Collections
    944148 Newbie
    Currently Being Moderated
    It is good practice to use the instance variable name in the toString method incase we have 2-3 int or same type of variables.
    @Override
    public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("name:" + name + ", ");
    builder.append("salary:" + sal.toString());
    return builder.toString();
    }


    @Trilok
    Curious to know why you have only used this reference in the setter methods and not in the getter methods.
  • 4. Re: Java Collections
    sabre150 Expert
    Currently Being Moderated
    somi wrote:
    Curious to know why you have only used this reference in the setter methods and not in the getter methods.
    Because of the name clash (argument and instance variable names) the setter method needs 'this' to reference the instance variable but the getter method has no name clash so 'this' is not needed.
  • 5. Re: Java Collections
    947164 Newbie
    Currently Being Moderated
    Hi,
    Please typecast your object with (Employee) class.if u try to print an abject it will always give you a hash code because object always stored in that way,name is a just a reference variable for them.

    Edited by: 944161 on Jul 3, 2012 3:14 AM
  • 6. Re: Java Collections
    EJP Guru
    Currently Being Moderated
    944161 wrote:
    Please typecast your object with (Employee) class.
    Why? How exactly does that solve the problem?
  • 7. Re: Java Collections
    947164 Newbie
    Currently Being Moderated
    Actually after typecasting you can access the reference variables.if you are expecting to display the object details by printing object than it is not possible.
    for ex : System.out.println(employee) will always print hashcode
  • 8. Re: Java Collections
    EJP Guru
    Currently Being Moderated
    Actually it won't, unless the class has been poorly designed with public variables. And whether or not a hashCode is printed depends entirely on the class. And none of this inaccurate irrelevance answers the question.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points