cancel
Showing results for 
Search instead for 
Did you mean: 

Removing Duplicate elements in Value Node

Former Member
0 Kudos

Hello,

I have a Value node (FailedStudents) of type List. the attributes of this node are studentNumber & StudentName.

I am calling 5 bapis (StudentsFailedinMathBapi, StudentFailedScienceBapi, StudentFailedSocialBapi.. etc) & adding FailedStudentNodeelements (model node) from these bapi to the FailedStudentsNode (Value node).

i.e. adding elements from model nodes of different bapis to a value node. As elements are added from 5 bapis there will be many duplicate elements in the value node(FailedStudent).

If there are duplicate elements with same studentNumber & StudentName then the duplicate element should be deleted.

I want to remove the duplicate elements from the value node. How to remove the duplicate Elements from a Value node?. any configuration/property to set for node/best logic?

Regards

Maha

Edited by: Maha Hussain on Mar 31, 2009 8:52 AM

Accepted Solutions (0)

Answers (4)

Answers (4)

NarendraChandel
Contributor
0 Kudos

Hi Maha,

please ignore the previous post as i missed one line which i have added now.

Below is the method which you can directly call by passing the String array as parameter and in returns it will give you deduplicated string array.

String[] duplicate= {"Nar", "Raj", "Nar", "Raj", "Nar", "Nar", "Raj"};

public String[] getDeduplicatedData(String duplicate[]) {
		// Convert it to list as we need the list object to create a set object.
		// A set is a collection object that cannot have a duplicate values, so
		// by converting the array to a set the duplicate value will be removed.
                                List list = Arrays.asList(duplicate);		
		Set set = new HashSet(list);
		// Create an array to convert the Set back to array. The Set.toArray()
		// method copy the value in the set to the defined array.
		//
		String[] result = new String[set.size()];
		set.toArray(result);
		return result;
	}

hope this will help.

revert if you need more info.

Regards

Narendra

Edited by: Narendra Singh on Mar 31, 2009 11:30 AM

Former Member
0 Kudos

I resolved by using generic code. Thanks for replies

Former Member
0 Kudos

Hi,

You can try this code here.


  private void removeDuplicateElements(IWDNode node) {
	  List attributeNameList = new ArrayList();
	  Iterator nodeAttributes = node.getNodeInfo().iterateAttributes();
	  while (nodeAttributes.hasNext()) {
		  AttributeInfo attributeInfo = (AttributeInfo) nodeAttributes.next();
		  String attributeName = attributeInfo.getName();
		  attributeNameList.add(attributeName);
	  }
	  List sbAttributeList = new ArrayList();
	  for (int i = 0; i < node.size(); i++) {
		  IWDNodeElement nodeElement = node.getElementAt(i);
		  StringBuffer sbAttributeBuffer = new StringBuffer();
		  for (int j = 0; j < attributeNameList.size(); j++) {
			  String attributeName = (String) attributeNameList.get(j);
			  Object attributeValue = nodeElement.getAttributeValue(attributeName);
			  sbAttributeBuffer.append(attributeValue);
		  }
		  sbAttributeList.add(sbAttributeBuffer);
	  }
	  for (int i = 0; i < sbAttributeList.size(); i++) {
		  StringBuffer sbAttributeBuffer = (StringBuffer) sbAttributeList.get(i);
		  for (int j = i + 1; j < sbAttributeList.size(); j++) {
			  StringBuffer sbAttributeBufferCompare = (StringBuffer) sbAttributeList.get(j);
			  if (sbAttributeBuffer.toString().equals(sbAttributeBufferCompare.toString())) {
				  sbAttributeList.remove(j);
				  node.removeElement(node.getElementAt(j));
				  j--;	// RollBack Index..
			  }
		  }
	  }
  }

Feel free to modify it, I barely tested it.. It works at least. 😃

Regards,

Daniel

p330068
Active Contributor
0 Kudos

Hi Maha,

Please close the thread if your problem has resolved.

Thanks

Arun

NarendraChandel
Contributor
0 Kudos

Hi Maha,

Below is the method which you can directly call by passing the String array as parameter and in returns it will give you deduplicated string array.

String[] duplicate= {"Nar", "Raj", "Nar", "Raj", "Nar", "Nar", "Raj"};

public String[] getDeduplicatedData(String duplicate[]) {
		// Convert it to list as we need the list object to create a set object.
		// A set is a collection object that cannot have a duplicate values, so
		// by converting the array to a set the duplicate value will be removed.		List list = Arrays.asList(duplicate);
		Set set = new HashSet(list);
		// Create an array to convert the Set back to array. The Set.toArray()
		// method copy the value in the set to the defined array.
		//
		String[] result = new String[set.size()];
		set.toArray(result);
		return result;
	}

hope this will help.

revert if you need more info.

Regards

Narendra

NarendraChandel
Contributor
0 Kudos

HI Maha,

first take the output from Bapi to a array.

A set is a collection object that cannot have a duplicate values, so by converting the array to a set the duplicate value will be removed.

now convert the set back to array it will remove the dupmlicate elements.

please revert if you need more info.

Regards

Narendra

former_member185086
Active Contributor
0 Kudos

Hi

No such property/configuration is available in node.

You have to put this logic when u add the element from BAPI to value node (manually check one value with all existing in value node )

Best Regards

Satish Kumar

Former Member
0 Kudos

Please provide the best feasible logic to remove duplicate elements.

Thanks

Maha