File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Hashmap duplicate/sorting issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hashmap duplicate/sorting issue" Watch "Hashmap duplicate/sorting issue" New topic
Author

Hashmap duplicate/sorting issue

Chris Avala
Greenhorn

Joined: Oct 28, 2013
Posts: 4
Hey, Just recently learned hashmap. I'm adding employee information. With sort: It ascends by last name. if they have the same last name, then it ascends by first name. If both employee's have first and last name then it must ascends by ID number. My code does not ascend by ID number, rather it omits the employee who's ID number is greater than the other one. I'm not sure if I have to work on my equals/CompareTo method, or my main method case.

Here's my code for my employee class. So objective is if two employee's have the same first and last name, then it must sort by ID, but my code omits the Employee who's ID is greater. Any guidance on what code I need to rework on will help.



Here is my other code that compiles the cases, and this case is the one that prints out Employee's information

Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Chris Avala wrote:Hey, Just recently learned hashmap. I'm adding employee information. With sort: It ascends by last name. if they have the same last name, then it ascends by first name. If both employee's have first and last name then it must ascends by ID number. My code does not ascend by ID number, rather it omits the employee who's ID number is greater than the other one. I'm not sure if I have to work on my equals/CompareTo method, or my main method case.



Welcome to the Ranch.

Actually I'm slightly confused, cause your subject line says a HashMap and your method printLastNameAscending takes two TreeMaps.
You are probably creating a HashMap and then converting it into a TreeMap or creating only TreeMaps or creating a HashMap as well as a TreeMap or ..... ( I could go on ...)
The point is I do not know. So how about you post the source code of your main method also so we know what is actually happening in there?

Another thing is - do you know what the printLastNameAscending is doing? Why are you passing two TreeMaps to this method? Ok, you must be trying to figure how to get your method to print the employees whose last and/or first names are different, and then whose last and the first names are same and the employee ids are different, are you? This is not how we do it but let's park that aside for now. Let's get the other things in place and then we'll work on this part. But it's a good thing to provide comments about what a method is supposed to do.

Now coming back to your question on whether it is the equals ( equals and hashCode ) method(s) or the compareTo method that is being used in your Map, the answer is it depends on the Map implementation you are creating. But first post your main method, so I can tell you how you can figure which method is being used by the specific Map implementation you have in your code and the operations you do on your Map. Actually you can figure that on your own by commenting out bits of your programs and running it but you may need to comment more than one thing at a time. We can work on that but you need to show me what your main method looks like.

Chan.

Another question I have is - Are employee ids of the employees unique?
Chris Avala
Greenhorn

Joined: Oct 28, 2013
Posts: 4
Thanks for your reply. Here is my main file. As for why I put two treemaps on my descendlingLastname case, my code only outputted Last Name & First Name. It did not output the ID numbers as well, so Implemented another tree map to get ID Number in the output as well.



Also here is an update compareTo method. Thanks for your reply, I'll post all information you need. So close to finishing this dang project. Also looking forward to being in this community board as well. Lastly, yeah, if two employees have the same first and last name, then they should have their own unique ID.


Chris Avala
Greenhorn

Joined: Oct 28, 2013
Posts: 4
Hey, I also changed my equals method because it was using "==" rather than equals when using Strings. I also changed my main method 5th case, the ascending/sort. Still does not work, when I enter the 2nd employee's same first/last name as the 1st employee, it overwrites that person along with their ID & Performance rating. So any help appricated. Codes change:



Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1049
    
  15
Chris Avala wrote:Hey, I also changed my equals method because it was using "==" rather than equals when using Strings.


That's a good change. Good that you found it on your own. IMO, we use == for equality test only in the case of primitives and enums or when we really need to test if two variables are refering to the same object or if we are testing two null values for equality.

I also changed my main method 5th case, the ascending/sort. Still does not work, when I enter the 2nd employee's same first/last name as the 1st employee, it overwrites that person along with their ID & Performance rating. So any help appricated. Codes change:


Actually I suggest you go through the API docs of a HashMap/TreeMap/HashSet/TreeSet to understand what they are and which methods they require you to override/implement and/or why.
Here is a helpful post - http://www.coderanch.com/t/449203/java/java/TreeSet-compareTo

Regardless, I think first we need fix a couple of other things in your code. For starters, what you have in the Employee class looks kind of ok, albeit some changes are required.

To test if a method is used at all in your code, you could write a System.out.println("<some text>"); statement in that method and see if gets printed.

Also could you state the requirements clearly? What is the program supposed to do? Do not skip details, i.e describe each of those menu options.
Also can two Employees have the same employee id regardless of what their name is? Note that this is independent of how you are to print the values or what those menu options are to do.
Printing the data as per an ordering scheme is not a thing we need worry about too much at this point.

We write equals method using those fields only that can determine if two objects are equal/unequal ( field/fields that can uniquely locate an Employee). If it is the employee id, it should only be the employee id. This is again independent of what data option 5 is to print.
We need to also make sure that our equals method is capable of handling any kind of input. So nulls may require a special consideration.
So the question here is which fields determine if two Employees are equal. The requirement description should tell you that. If two employees cannot have same employee id, it should only be the employee id.
The hashCode should also be computed based on those fields only which determine if two objects are equal/unequal. If it is the employee id, it should only be the employee id.

We'll talk about compareTo and toString once you state the requirements clearly.
Also I don't like the idea of using pre generic Java code.

I've not looked at EmployeeTest in details, but here are a couple of quick things I noticed.

Instead of coding as follows

it is always a better idea to code as follows.


Also I see you have no HashMap in the code. So why does the subject have a HashMap? Those TreeMaps don't look good but that is for later.

I haven't taken a look at EmployeeTest class in detail.

Chan.

Also I suggest you go through the following article --
http://www.coderanch.com/how-to/java/UserInput
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8196
    
  23

Chris Avala wrote:Here's my code for my employee class.

Chris,

1. Please DontWriteLongLines. It makes your thread very hard to read, and it's actually bad coding practice. I've broken yours up this time, but for future reference, please remember:
80 characters max.   (the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.
Oh, and you're usually better off using spaces for indentation, rather than TABs.

2. You could save yourself a lot of casting, and probably prevent "silly" mistakes by using generics. Employees are presumably only comparable with other Employees, so you can tell the compiler that by writing:
public class Employee implements Comparable<Employee> { ...
and then your compareTo() method becomes:
public int compareTo(Employee o) { ...

3. I don't think we've yet clarified whether or not your Employee ID's are unique. If they are, then that, and that alone should determine whether two Employees are "equal". Unfortunately, unless they're coming from a database, unique IDs are tricky, since you generally need some way to enforce that uniqueness. Java provides two classes: UID (java.rmi.server.UID) and UUID (java.util.UUID) for creating unique IDs, but you can also generate them from a sequence number (quite tough to get right) and/or a timestamp.
Warning: The Java "unique IDs" tend to be quite large.

Another alternative is to forget about implementing equals() and hashCode() completely, and generate an "ID" using System.identityHashCode(). Unfortunately, the value is then not guaranteed to be unique and will change from session to session. It does, however, ensure that two Employees are only "equal" if they are the same object, which sounds like what you actually want.

The fact is that "people" are notoriously difficult to identify, since none of the normal things we use to identify ourselves (name, sex, birthday) actually guarantees uniqueness. Even combinations can sometimes throw up duplicates, especially in cases of names like "John Smith".

4. If (3) is true, then your compareTo() method and your equals() method are no longer "consistent" with each other. Now there's nothing particularly terrible about that, but you should bear it in mind.

5. An alternative to making an object Comparable is to create a Comparator (java.util.Comparator), which can also be used with a TreeMap. This separates the class from its order, and you can create many of them for a single class, eg: for ordering by name, or age, or salary.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Chris Avala
Greenhorn

Joined: Oct 28, 2013
Posts: 4
Thanks for your replies. I have managed to finish this project. Had to change my equals method as well to match up with the compareTo and scrap my main file and change it up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashmap duplicate/sorting issue