How Can i remove duplicate from a list in Groovy

manas kumar :

Hi I have a groovy code for JIRA. Here I need to sort or remove duplicate value from the list.

Ex :

["154515 Sawgrass",
 "170985 Mexico APIs for Payments",
 "153026 CitiCards Consumer and Business Account Online (authenticated pgs), No CSI App ID",
 "153890 GC Citibank Online - Singapore IPB v3",
 "144564 Citibank Online (CBOL) US, Zack Dummy CSI #3",
 "171706 Quip",
 "167518 GC Eclipse [Teller]",
 "167518 GC Eclipse [Signature]]",]

the above is the result, from where I need a sorting like ... 167518 - with this number there are two values, but I need to store only one 167518, I dont want to store multi value. or else in simple word I need to sort the list in this way that if number is same but with multiple values, I need to store only 1 value with same number.

// From here Child Issue Details Starts....
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

String[] elements = impactedAppValues.split("\\s*[;]\\s*");
log.info "Printing elements with Issuee Value : --" + elements

List<String> fixedLenghtList = Arrays.asList(elements);

ArrayList<String> listOfString = new ArrayList<String>(fixedLenghtList);

log.info "Printing listOfString with Issuee Value : --" + listOfString


// From here parent Issue Details Starts....
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) as String
log.info "Printing parentCustomField value :-----" + parentCustomField

String[] elements1 = parentCustomField.split("\\s*[;]\\s*");
log.info "Printing elements for Parent Issuee Value : --" + elements1

List<String> fixedLenghtList1 = Arrays.asList(elements1);

ArrayList<String> listOfString1 = new ArrayList<String>(fixedLenghtList1);

log.info "Printing listOfString1 For Parent Issuee Value : --" + listOfString1

listOfString1.addAll(listOfString)
log.info "Printing listOfString1 Againnnnnnnnn For Combined Value of 1 & 2--" + listOfString1

listOfString1.unique()
log.info "Printing listOfString1 Unique Values of 1 & 2--" + listOfString1

parentCustomField = listOfString1 as String
log.info "Printing parentCustomField -----" + parentCustomField

here I have gave the code for what I have written till now.

Is this possible to to sort like the above? I mean we can remove the duplicates but how to removes the number from a value or String?

Michael Easter :

Assume we start with a list like this:

def list = ["154515 Sawgrass",
            "170985 Mexico APIs for Payments",
            "153026 CitiCards Consumer and Business Account Online (authenticated pgs) No CSI App ID",
            "153890 GC Citibank Online - Singapore IPB v3",
            "144564 Citibank Online (CBOL) US, Zack Dummy CSI #3",
            "171706 Quip",
            "167518 GC Eclipse [Teller]",
            "167518 GC Eclipse [Signature]"]

Then we can start with a new list and a set of values:

def listUniqueValues = []
def valuesAlreadySeen = new HashSet()

Then we iterate over the list, checking to see if the current value is in the set of "previously seen" values:

list.each { item ->
    def value = item.split(" ")[0]

    if (! valuesAlreadySeen.contains(value)) {
        listUniqueValues << item
    }

    valuesAlreadySeen << value
}

then we can sort listUniqueValues and print:

listUniqueValues.sort() { a,b -> a <=> b }.each { println it }

to get

144564 Citibank Online (CBOL) US, Zack Dummy CSI #3
153026 CitiCards Consumer and Business Account Online (authenticated pgs) No CSI App ID
153890 GC Citibank Online - Singapore IPB v3
154515 Sawgrass
167518 GC Eclipse [Teller]
170985 Mexico APIs for Payments
171706 Quip

NOTE: this isn't the most efficient method, nor the "Grooviest" (there is a cooler way with the inject method). But it is straight-forward and hopefully easy to understand.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=421610&siteId=1