Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Comparable Interface  RSS feed

Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two ArrayList containing objects(POJO) of same type.I need to compare these tow ArrayLists on the basis of one attribute of the object.
Currently I am interating both lists and comparing a specific attribute.
I read about comparable interface and it did not seem to make much sense to me as there also I will have to iterate both lists and then call compareTo method.
Pls advice .

author and iconoclast
Posts: 24203
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Comparable provides a standard interface for comparing objects. It's very helpful in implementing standard sort routines, ordered containers, and such. In your particular situation, it wouldn't help anything.

Perhaps you could implement equals() in this POJO class so that it just compares this one attribute; then ArrayList's equals() method would do exactly what you want.
It means our mission is in jeapordy! Quick, read this tiny ad!
ScroogeXHTML - small and flexible RTF to HTML converter library
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!