• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

how ro compare two arraylist objects

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but i want to make use of only arraylist yar
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
     
    If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic