Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arraylist in java

 
vijayapriya thirumurugan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use merge sort .
 
Muhammad Ijaz
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry Sagar, you are right. I shouldn't give the code...


Regards,

Pomy
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could be missing something, (it is friday morning after all) but surely this is a task for a Set?
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic