Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to separate or remove duplicate elements from an array String or list

J Manas Kumar
Contributor
February 13, 2020

Hi Everyone

 

I am trying to add fields from child custom field to parent custom field.

I am able to merge it, but i am not able to update parent issue custom field.

below i am first fetching child issue's field details, and then converting it into array.

sam ething i am doing for parent too.

i am able to merge it in 2 ways, but cant remove the duplicates using toUnique() or unique() method.

below i have the code.

any help will be appreciated.

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Level ;
import org.apache.log4j.Logger ;
import com.atlassian.jira.issue.MutableIssue ;
import com.atlassian.jira.issue.fields.config.FieldConfig ;
import com.atlassian.jira.issue.customfields.option.Options ;
import org.apache.commons.collections.ListUtils;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.io.*;
import java.util.HashSet;
import java.util.Set;
import com.google.common.collect.ObjectArrays;
import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;

 


log.setLevel(Level.DEBUG) ;
def issue = event.issue as Issue
def issueManager = ComponentAccessor.getIssueManager() ;
def customField = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_10602")
log.debug("Printing Custom Field : --" + customField)
def impactedAppValues = customField.getValueFromIssue(issue) as String
log.info "Printing Custom Field with Issuee Value : --" + impactedAppValues

// here m printing the above log --->>Printing Custom Field with Issuee Value : --No CSI App ID; 170985 Mexico APIs for Payments; 153026 CitiCards Consumer and Business Account Online (authenticated pgs);

 


def modifyIF = impactedAppValues.replace(";",",")

// replacing ; with , --- No CSI App ID, 170985 Mexico APIs for Payments, 153026 CitiCards Consumer and Business Account Online (authenticated pgs)


String[] strList1 = Arrays.asList(modifyIF)
log.info "Printing modifyIF Value : --" + modifyIF
log.info "Printing strList1 Value : --" + strList1
//def theList1 = Arrays.asList(modifyIF)
List theList1 = Arrays.asList(modifyIF)
log.info "Printing theList1 Value : --" + theList1

// here you can see the array --- >> [No CSI App ID, 170985 Mexico APIs for Payments, 153026 CitiCards Consumer and Business Account Online (authenticated pgs),

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Parent Link")
def parentValue = issue.getCustomFieldValue(cf)
log.info "Printing parentValue value :-----" + parentValue


def planviewProjectSide = ComponentAccessor.getComponent(CustomFieldManager).getCustomFieldObject("customfield_10602");
Issue PlanviewIssue = issueManager.getIssueObject("" + parentValue);
def parentCustomField = planviewProjectSide.getValueFromIssue(PlanviewIssue)
log.info "Printing parentCustomField value :-----" + parentCustomField
def modifyParentIA = parentCustomField.replace(";",",")
List theList2 = Arrays.asList(modifyParentIA)
log.info "Printing theList2 value :-----" + theList2
String[] strList2 = Arrays.asList(modifyParentIA)
log.info "Printing strList2 Value : --" + strList2
//List<String> theList2 = Arrays.asList(modifyParentIA)

//def merged = new ArrayList(theList1);
def merged = new ArrayList();
merged = ListUtils.union(theList1, theList2);
String[] merged1 = ListUtils.union(theList1, theList2);
//log.debug("Merged List using Apache Commons Collections: " + merged)
//log.debug("Merged1 List using Apache Commons Collections: " + merged1)

String[] combined = ArrayUtils.addAll(strList1, strList2)
def combined1 = ArrayUtils.addAll(strList1, strList2);
log.debug("combined List using combined array Collections: " + combined)
log.debug("combined1 List using combined1 array Collections: " + combined1)

//def unique = merged.toUnique()
String[] unique1 = merged1.toUnique()
//log.debug("No Duplicates List using unique array Collections: " + unique)
log.debug("No Duplicates using unique1 array Collections: " + unique1)


String[] unqComb = combined.toUnique()
//def unqComb1 = combined1.toUnique()
log.debug("No Duplicates List using unqComb array Collections: " + unqComb)
//log.debug("No Duplicates List using unqComb1 array Collections: " + unqComb1)

 

 

 

 

 

 

 

 

 

 

Regards

Manas

0 answers

Suggest an answer

Log in or Sign up to answer