| Author |
arraylist in java
|
vijayapriya thirumurugan
Greenhorn
Joined: Apr 03, 2009
Posts: 16
|
|
hello sir,
I have hashtable consist of 5 key with their corresponding pair values in the form of arraylist. My problem is i want to get common elements between two arraylists. that is common elements between 1 and 2 , common elements between 1,3 , common elements between 1and 4 , common elements between 1and 5. how can do this is. can you give code for this. i'm waiting. can you explain this please.
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
vijayapriya thirumurugan wrote:can you give code for this.
Not here.
Two ideas:
1. Iterate over one list, check whether the elements contains in the second list, if yes, get that.
2. Use standard Set#reatinAll() API.
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
|
use merge sort .
|
 |
Muhammad Ijaz
Ranch Hand
Joined: Mar 03, 2009
Posts: 34
|
|
Following is the hint on how to use Set and retainAll method to resolve your problem
Hope this will help you to get your desired result
Regards,
Pomy
|
IBM Certified WebSphere Commerce Application Developer
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Muhammad Ijaz wrote:Following is the hint on how to use Set and retainAll method to resolve your problem
Muhammad ,
Many of us couldn't control ourselves from sharing what we got or our knowledge, but giving code in that way is not a good way. At least let them try, let them come up with their own code/solutions.
seetharaman venkatasamy wrote:use merge sort .
Not exactly a merge sort, but while merging, just checked out the equality for the elements between two sorted halves and add the common element into a result/final list. [Looks like it get finally resulted as my first idea ]
|
 |
Muhammad Ijaz
Ranch Hand
Joined: Mar 03, 2009
Posts: 34
|
|
I am sorry Sagar, you are right. I shouldn't give the code...
Regards,
Pomy
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Muhammad Ijaz wrote:I am sorry
NoNeedToSaySorry
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Muhammad Ijaz wrote:I am sorry Sagar, you are right. I shouldn't give the code...
I thought about deleting it earlier, but left it as not being exactly the same as the final answer.
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Campbell Ritchie wrote:
Muhammad Ijaz wrote:I am sorry Sagar, you are right. I shouldn't give the code...
I thought about deleting it earlier, but left it as not being exactly the same as the final answer.
Same thought here, initially, but the reason to ping is simple, let the user think and use Java API
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Sagar Rohankar wrote:checked out the equality for the elements between two sorted halves and add the common element into a result/final list. [Looks like it get finally resulted as my first idea  ]
it is called Merge Sort
|
 |
Gavin Tranter
Ranch Hand
Joined: Jan 01, 2007
Posts: 333
|
|
|
I could be missing something, (it is friday morning after all) but surely this is a task for a Set?
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
seetharaman venkatasamy wrote:
Sagar Rohankar wrote:checked out the equality for the elements between two sorted halves and add the common element into a result/final list. [Looks like it get finally resulted as my first idea  ]
It is called Merge Sort
I knew one thing, a sort is different from set intersection.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Sorry for not noticing before, but there is a misconception about merge sort. Merge sort is an algorithm which sorts something (not a Set, which has no concept of order) by dividing it into smaller and smaller parts, then merging the parts together.
It is not a way of merging two separate collections.
|
 |
 |
|
|
subject: arraylist in java
|
|
|