• 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

arraylist in java

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use merge sort .
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Ijaz wrote:I am sorry


NoNeedToSaySorry
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic