• Post Reply Bookmark Topic Watch Topic
  • New Topic

how ro compare two arraylist objects  RSS feed

 
vineel kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends . i have a small doubt

let us think that i have 2 arraylists a1,a2;
in a1 i have five objects
1,2,3,4,5
and also in a2 i heve 2 objects
3,4

i wantt a method which can compare these two arraylists and print the comman objects in both

i.e 3,4
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked at the Javadoc for java.util.Collection, java.util.List, and java.util.ArrayList and thought about how the methods listed there could help you? There's a quite easy solution that involves contructing a new ArrayList, then calling one method on it. I'm going to move this to our Java in General (Beginner) forum, and then you c'mon back and show us what you can figure out.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can convert the ArrayList to a Set and then use the contains(Object obj) method.
 
vineel kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i want to make use of only arraylist yar
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manuel Leiria:
You can convert the ArrayList to a Set and then use the contains(Object obj) method.


That wouldnot be the way.

Vineel, so have you had time to look at the Javadocs? Maybe you need a stronger hint: you need one specific ArrayList constructor, then one method described on the page I've linked to. C'mon, it's not so hard...
 
vijay mahendra
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
check Comparator interface in util package .
that is better way to compare collections.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vijay mahendra:
hello,
check Comparator interface in util package .
that is better way to compare collections.


Actually, no, it's not. Comparator has absolutely no place here. Folks, please stop posting nonsense answers!
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[EJFH: Deleted the code for the exact solution I was hinting about ]
[ July 23, 2007: Message edited by: Ernest Friedman-Hill ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you help people figure out an answer on their own, then they learn something. If you give them the answer like this, then they copy, paste, and learn nothing. Do you know the old saying about "teaching a man to fish?" That's what JavaRanch is all about.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
If you help people figure out an answer on their own, then they learn something. If you give them the answer like this, then they copy, paste, and learn nothing. Do you know the old saying about "teaching a man to fish?" That's what JavaRanch is all about.


Thank you!
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
(...) so have you had time to look at the Javadocs? Maybe you need a stronger hint: you need one specific ArrayList constructor, then one method described on the page I've linked to. C'mon, it's not so hard...


I (not the OP) took the bait, only to regret it. Guess I found it harder than you expected because -

  • I kept skimming through the documentation, not really reading it.
  • I assumed the method you were referring to, would return an array list (somehow I lost the significance of your advice to use another constructor).
  • [list] I find the method poorly named. It might have better been named as . Just my opinion, of course.[/list]


    Thank you for such an answer. This will take me several days to forget as opposed to the 30-second half-life of reading a ready-made solution. :-)

    - Anand
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!