• Post Reply Bookmark Topic Watch Topic
  • New Topic

SSN Comparator issues  RSS feed

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




My code looks as above. I am getting compilation error at line 8 as Cannot invoke compareTo(int) on the primitive type int

I know int is not Object and i need to use wrapper class but not sure where and how

With LastName as it is String object i got correct output as below


Integer wrapSsn= new Integer( ssn );//ssn is the primitive int
Above casting from primitive to Integer wrapper object where should i use it.
please advise
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. SSNComparator should implement Comparator<Customer> instead. If you do this, the lines that cast from Object to Customer can go away.

2. You can use the static Integer.compare() method make the SSNComparator.compare method a one-liner:

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. SSNComparator should implement Comparator<Customer> instead. If you do this, the lines that cast from Object to Customer can go away.


i tried as below

Still same issue is there with one additonal

Name clash: The method compare(Object, Object) of type LastNameComparator has the same erasure as compare(T, T) of type Comparator<T> but does not override it
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why would you name this LastNameComparator when it's comparing SSNs?

2. Why would you keep lines 4 to 9? you should delete them.

3. Why return 0 instead of Integer.compare(ssn1, ssn2)?

 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




i got below output
Smith John 12345

not sure why other two did not come

how below two lines are different with SSN not with LastName comparison

public class SsnComparator implements Comparator<Customer> {//with generic declaration


public class SsnComparator implements Comparator {//without generic telling specific Customer type Comparator
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was working as below



But i still want to write code similar to LastNameComparator as below


Where should i cast using Integer wrapper class so that i can use compareTo() method inside compare().
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:It was working as below



But i still want to write code similar to LastNameComparator as below


Where should i cast using Integer wrapper class so that i can use compareTo() method inside compare().

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai rama krishna wrote:
But i still want to write code similar to LastNameComparator as below


Why on earth would you want to write more code than is necessary and is less safe to boot? This version can accept any type of object and will cause a ClassCastException if either of them is not an instance of Customer. The version with generics forces you to pass in Customer instances instead. The variables c1, c2, and index are just noise that can be eliminated. There is no good reason for keeping them around.

And what is the purpose of returning -index? Are you trying to reverse the logic so you can sort in reverse order? If so, don't do that. Use Collections.reverseOrder(Comparator) instead.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a section in the Java Tutorials which explains why -index is a bad idea.
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My challenge is with Ssn not LastName which was already working.

I want to sort on Ssn similar to above somethong like below




i am not able to succeed as Ssn is primitive so i have to convert to object to achieve it
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you feel a need to use compareTo() when
will do?
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just to experiment, code how converting from int to Integer concept works in this context
 
sai rama krishna
Ranch Hand
Posts: 536
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i got it



when i wrote as above i got as below

Smith John 12345
Edison Thomas 78989
Jackson Laura 456546


Please correct me if i am wrong.

Also why i got only one output if i use Integer.compare() as below

package test;
import java.util.*;
public class SsnComparator implements Comparator<Customer> {




Smith John 12345



also which approach is better approach1 or approach 2 as below



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!