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

interface & Comparing Strings

H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
hii people

thats what i cant do... i want my compare method to compare Strings
i've got a class called, say.. House(!), which implements Houses.., within this class i wish to use this method

public int compareTo(String str){} //i know this is from class String

and so im struggling to work out the correct signiture for class Houses
what i currently have is

so what do i change Comparable to?

thanx in advance
[ March 25, 2005: Message edited by: H Melua ]
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
What you are saying in your interface declaration, is that you want Houses to be comparable. Now, your application logic might say that to compare Houses, you actually compare a string property of the Houses.

e.g.



That is, the actual comparison on two houses is a comparison of their addresses.

You could, if you desired, add your own method compareTo( String ), but as this is not declared in the Comparable interface, you wouldn't implement Comparable.

You could, I suppose, assume that the object passed to compareTo is a String, but this would break very quickly.
[ March 25, 2005: Message edited by: Horatio Westock ]
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
thanks mate

i know what ur saying but i dont want it to compare objects rather i want strings... my problem is not in class House, i want it to remain as it is, but i want class Houses to implement an interface that accepts compareTo (String), not object..

so rather than Comparable im looking for somthing else..

but in all cases, is there a better way of going about the whole thing?

thanx
Hannah
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Why not declare the compareTo( String ) in your Houses interface directly? Then classes implementing houses will have to implement it.

Can you give us a concrete example of what you are using it for? My brain hasn't warmed up yet this morning
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
lol im not any better, ive been on this since 4.30 in the morning!!

emm basically, when i add a house (String address), i want it to be in the order specified by the compareTo(String) method in the House class...

to make this more clear, there will be another class called HouseList that will hold the string of houses... but lets not worry about that 4 now lol!

thats all

bless u
Hannah
[ March 25, 2005: Message edited by: H Melua ]
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
OK, I think I understand now.

Basically what your require is for your collection of house instances to be ordered based upon their address.

Now, to do this, you have two strategies. Firstly, you can use a more generic collection, then sort the elements afterwards. Alternatively, you can use a collection that understands the ordering, so that when you insert an item, it is automatically inserted at the correct position.

Option 1
--------

You have two options. Either implement Comparable in your House type, then use the Collections.sort( your list ) method. This will sort your collection according to the order specified in the compareTo( Object ) method of your House implementation.

The second option is to create a Comparator class, perhaps HouseAddressComparator which does the same ordering logic. Then you can use the Collections.sort( your list, your comparator ) method.

The second option doesn't require you to change your House implementation.

Option 2
--------

Read about priority queues. You will have to either implement Comparable or create a Comparator as above.


You see, at the end of the day, you still need to implement compareTo( Object ) somewhere, even though you want to compare their string addresses. Even though you are comparing the addresses, it is the actual House instances that you are sorting.
[ March 25, 2005: Message edited by: Horatio Westock ]
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
ahaaaa what i understood is that u should be lecturing in our university!!! i dont know where they bring our useless lecturers from!?!

so going back to what u said earliar
Why not declare the compareTo( String ) in your Houses interface directly? Then classes implementing houses will have to implement it.


will that make it automatically order the instances? or will i have to make a call to it to do the sorting business?

im honestly so grateful..
Hannah
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
I think I've spotted the confusion here. The String class has two compareTo methods. One which compares lexographically. So read on, to see how we will use this.

What we are going to do is use the compareTo(String) method of the String class in our comparison of the houses.

You see, to use any of the sorting functionality in the Collections framework, you have to provide the ability to say "this object is greater than that object", "this object is equal to that object" and "this object is less than that object". The meaning of 'greater', 'equal' and 'less' depends upon what type of data you are talking about. By using the Comparable interface, collections like lists etc, don't need to know anything about the nature of your data. They only need to know that if they call "obj.compareTo( Object other )", it will return a result that indicates whether 'other' is greater than, equal to or less than 'obj'. The actual value is a positive int for greater, zero for equal and a negative int for less than.

Lets have a little example:



The code above creates a list of houses, then sorts them according to their natural order, then prints them out.

Now, for this natural order to work on the address, your House has to implement Comparable. That is, it has to implement the method compareTo( Object ). Don't worry, we will make sure that it this does a lexographical comparison of the address strings.

In our case, when we say house 1 is greater than house 2, what we mean is, the address of house 1 comes alphabetically before that of house 2.

So, in our House class we have, as before,



Got it? We are telling the Collection classes that if they compare two houses, then call our compare method, which then returns the result of comparing the address strings lexographically.

Remember the alternative? We can move the comparison in to another class called a Comparator. Lets look at an example:



Now to use this,



OK?

So, to sum up. In order to sort houses, you need to be able to compare them. You can do this by implementing the Comparable interface, and the compareTo(Object) method, or by creating a Comparator class, and implementing the compare(Object, Object) method.

In either case, you want to utilize the String.compareTo(String) method, to compare the addresses of the houses.

Phew!
[ March 25, 2005: Message edited by: Horatio Westock ]
H Melua
Ranch Hand

Joined: Jan 04, 2005
Posts: 172
lol awww so sweeet
thanks alot, thank u sooo much for that, i honestly cant express how extremely grateful iam.........

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface & Comparing Strings