• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing objects  RSS feed

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Is it possible to compare 100 objects using comparator interface in java?

Thanks
Padma priya N.G.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, but two at a time.

Greetz,
Piet
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to compare 100 objects using comparator interface in java?

Could you elaborate on this statement? What exactly are you attempting to achieve?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a nice section about comparing objects in the Java Tutorials. Start there.
By “comparing 100 objects”, so you mean sorting a collection of 100 objects? If so, the answer is yes. You can go through the tutorial to the next section, or you can find an algorithms book and look for sorting algorithms.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I want to compare 100 objects of the same class.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Sorry,I want to sort a collection of 100 objects.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only sort collections which support the concept of an order, which means Lists. Go back to the tutorial link I posted and you will probably find sorting in the same “trail”. Try the section called algorithms.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I could see that the method int compare(Object e1,Object e2) compares two objects. But, I want to compare 100 objects from a collection. How can I do that?

Thanks
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two at a time.

You don't need to use the method yourself if you get another class to do the sorting.
If you have to write your own sorting method you use it to compare pairs of objects. You only compare two at a time. You would have to search for sorting algorithms or get a book.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!