cancel
Showing results for 
Search instead for 
Did you mean: 

Check the duplicates in context in mapping

deva_gembali2
Participant
0 Kudos

Dear experts,

  Interface : IDOC --> JDA

before sending to JDA, there is another mapping to check duplicates in the same context.

As per screen,an item should check the duplicates in the context of the articles and trigger out the same but output comes in reverse order.

based on this logic, output is coming in reverse order, so inserting wrong values in JDBC table. please advise me code.

ArrayList aList = new ArrayList();

int j = 1;

for(int n=0; n<var1.length; n++)

   j = n;

aList.add(var1[j]);

result.addValue(var2[j]);

for(int i=j-1; i>-1; i--){

if(aList.contains(var1[i])){

    continue;

}

else{

aList.add(var1[i]);

result.addValue(var2[i]);

}

}

Accepted Solutions (1)

Accepted Solutions (1)

former_member190293
Active Contributor
0 Kudos

Hi, Deva!

You can use this code for removing duplicates from "var1" input queue.

LinkedHashSet<String> varList = new LinkedHashSet<String>(Arrays.asList(var1));

for (Iterator<String> iterator = varList.iterator(); iterator.hasNext();)

result.addValue(iterator.next().toString());

Regards, Evgeniy.

former_member182412
Active Contributor
0 Kudos

Hi Evegeniy,

We can simply use for each loop instead of iterator.


public void removeDuplicates(String[] var1, ResultList result, Container container) {

  Set<String> set = new LinkedHashSet<String>(Arrays.asList(var1));

  for (String value : set) {

  result.addValue(value);

  }

  }

Regards,

Praveen.

former_member190293
Active Contributor
0 Kudos

Hi Praveen!

Yes, this seems to be more optimal way.

I'm not much experienced in Java yet.

Thank you for your suggestion.

Regards, Evgeniy.

Answers (4)

Answers (4)

Ryan-Crosby
Active Contributor
0 Kudos

Hi Deva,

Here is some information on some of the easiest stuff you can do regarding set operations - Set Interface

I'm not clear what you are doing with the duplicates but the method retainAll gives the intersection.  However, if you are doing something that is specific to indices which may be the case since you mentioned context then I would suggest continuing with the index approach and refining the UDF code which you have included.

Regards,

Ryan Crosby

former_member190293
Active Contributor
0 Kudos

Hi Ryan!

It's the same approach I've mentioned above . "LinkedHashSet" object stores input array contents removing duplicates and preserving original elements order.

Regards, Evgeniy.

Ryan-Crosby
Active Contributor
0 Kudos

Hi Evgeniy,

Yes, I am familiar with the concept but more unclear because the original post doesn't give enough details of what is expected for duplicates - specifically does it have to be only duplicates at any certain index (context) or duplicates across the whole set.  For instance, if the two collections are A, B, C, D & A, B, C, c, D does that mean that D will not be marked as a duplicate?  The post mentions context so in the case of the first set D is at index 3 and in the case of the second set it's at index 4 which would end up being different contexts altogether.  As the saying goes the devil is in the detail but at the moment I only see halos. 😛

The LinkedHashSet has its own determination of the "intersection" with retainAll so I would merely use that instead of iterating the set on my own.

deva_gembali2
Participant
0 Kudos

HI Ryan,

yes, your expectations are correct. In IDOC structure, context may not be in right index .

so, your code might not required index right ?

Former Member
0 Kudos

Hi Deva,

you can use the below UDF to remove the duplicate value from the context.

Hope this will help you.

Thanks

Sagarika

suman_sourabh
Participant
0 Kudos

Hi Deva,

As per the screenshot if you want to remove the duplicate values from the field 'ITEM' then you can use the below UDF:

UDF type: Context

input: ITEM as a string

----------------------------------

ArrayList al = new Arraylist();

for(int i =0;i<ITEM.length();i++){

if(!al.contains(ITEM[i])){

al.add(ITEM[i]);

}

}

for(int j=0; j<al.size();j++){

result.addValue(al.get(j).toString());

}

-----------------------------------------------
Hope this will help you to remove the duplicates from the context.

Regards,
Suman

former_member186851
Active Contributor
0 Kudos

Hello Deva,

Your requirement is to remove the duplicate values?