我如何从 Groovy 中的列表中删除重复项

How Can i remove duplicate from a list in Groovy

您好,我有一个 JIRA 的 groovy 代码。 这里我需要对列表中的重复值进行排序或删除。

例如:

["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]]",]

上面是结果,从那里我需要像...167518 这样的排序 - 这个数字有两个值,但我只需要存储一个 167518,我不想存储多个值。或者简单地说,我需要以这种方式对列表进行排序,如果数字相同但有多个值,我只需要存储 1 个具有相同数字的值。

// 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.

可以像上面这样排序吗?我的意思是我们可以删除重复项,但是如何从值或字符串中删除数字?

如果我理解你的问题,你只需要留下一个相同编号的项目(编号是第一个字(我们称之为前导编号))?首先,您可以对列表进行排序。在结果中,具有相同 "leading number" 的字符串将被放置在相邻的单元格中。然后您可以遍历列表,从 i-1 和 i 元素中提取 "leading number"(例如通过正则表达式),如果它们相同,则只需从列表中删除元素 i。

听起来您想使用 groupBy 方法:

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]']

List result = list.groupBy{ String s ->
    String num
    s.eachMatch( /^(\d+).+$/ ){ num = it[ 1 ] }
    num
}.findResults{ String num, List<String> vals ->
    num ? vals.sort().first() : null
}

1st groupBy 被 "id" 使用正则表达式调用分组,结果你得到一个由 "id" 键控并用匹配字符串列表赋值的映射。

然后调用findResults来过滤掉不匹配的值,如'No CSI App ID',并按字母顺序对匹配列表进行排序,得到first()(或last())元素

结果如下:

[154515 Sawgrass, 170985 Mexico APIs for Payments, 153026 CitiCards Consumer and Business Account Online (authenticated pgs), 153890 GC Citibank Online - Singapore IPB v3, 144564 Citibank Online (CBOL) US, 171706 Quip, 167518 GC Eclipse [Signature]]

假设我们从这样的列表开始:

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]"]

然后我们可以从一个新列表和一组值开始:

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

然后我们遍历列表,检查当前值是否在 "previously seen" 个值的集合中:

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

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

    valuesAlreadySeen << value
}

然后我们可以排序 listUniqueValues 并打印:

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

得到

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

注意:这不是最有效的方法,"Grooviest" 也不是(inject 方法有更酷的方法)。但它是直截了当的,希望很容易理解。

您可以提取 ID 并将其放入 SortedMap 中。 例如

def data = 
[ "154515 Sawgrass"
, "171706 Quip"
, "167518 GC Eclipse [Teller]"
, "167518 GC Eclipse [Signature]]" ]

println(
    data.collectEntries(new TreeMap()) {
        [it.findAll(/\d+/).first().toLong(), it]
    }
)
// → [154515:154515 Sawgrass, 167518:167518 GC Eclipse [Signature]], 171706:171706 Quip]

原名单:

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]"]

首先,拆分每个元素并取数字部分:

def numbersList = []
list?.each {
    numbersList.add(it?.split(" ")[0]) 
}

应用唯一 属性:

def uniqueNumbers = numbersList.unique()

然后,对于每个唯一值,从原始列表中获取数据:

def result = []
uniqueNumbers?.each { number ->
    result.add(list.find { it.contains(number?.toString()) })
}

对最终结果进行排序:

result.sort(true)

我们得到:

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