【问题标题】:Compare two different ArrayLists for matching element比较两个不同的 ArrayList 以匹配元素
【发布时间】:2015-10-05 07:58:18
【问题描述】:

我有两个数组列表,我想将请求的元素与允许的元素进行比较。

如果请求的元素存在于允许的元素中,它应该打印

"Allowed elements  <element1>, <element2>, <element3>"

如果请求的元素不在允许的元素中,则应打印

"Not allowed <element1>, <element2>, <element3>"

我的代码

public class testList {

    public static void main(String[] args) {

        ArrayList<String> Alist = new ArrayList<String>();
        ArrayList<String> Blist = new ArrayList<String>();

        // allowed elements
        Alist.add("NAME");
        Alist.add("SUBJECT");
        Alist.add("MARKS");

        // requested elements
        Blist.add("NAME");
        Blist.add("AGE");
        Blist.add("DOB");
        Blist.add("SUBJECT");
        Blist.add("MARKS");
        Blist.add("AVERAGE");
        Blist.add("MOBILE");
        Blist.add("EMAIL");

    }
}

结果应该是:

如果请求的 EMAIL 和 MOBILE 不在允许的元素中,则应打印“Not allowed EMAIL and MOBILE

如果要求 NAME、SUBJECT 和 MARKS 哪些是允许的元素,应打印“Allowed NAME, SUBJECT and MARKS

【问题讨论】:

标签: java arraylist collections compare


【解决方案1】:

这是解决您问题的非常简单的代码,试试这个并告诉我是否有:

StringBuilder allowBuilder = new StringBuilder("Allowed ");
StringBuilder notAllowBuilder = new StringBuilder("Not allowed ");

List<String> allowList = new ArrayList<String>();
List<String> notAllowList = new ArrayList<String>();

for (String blistItem : Blist) {
    if (Alist.contains(blistItem)) {
        allowList.add(blistItem);
    } else {
        notAllowList.add(blistItem);
    }
}

for (int i = 0; i < allowList.size(); i++) {
    if (i == 0) {
        allowBuilder.append(allowList.get(i));
    } else {
        if (i + 1 < allowList.size()) {
            allowBuilder.append(", ").append(allowList.get(i));
        } else {
            allowBuilder.append(" and ").append(allowList.get(i));
        }
    }
}

for (int i = 0; i < notAllowList.size(); i++) {
    if (i == 0) {
        notAllowBuilder.append(notAllowList.get(i));
    } else {
        if (i + 1 < notAllowList.size()) {
            notAllowBuilder.append(", ").append(notAllowList.get(i));
        } else {
            notAllowBuilder.append(" and ").append(notAllowList.get(i));
        }
    }
}

System.out.println(notAllowBuilder.toString());
System.out.println(allowBuilder.toString());

输出:

Not allowed AGE, DOB, AVERAGE, MOBILE and EMAIL
Allowed NAME, SUBJECT and MARKS

【讨论】:

  • 感谢您的帮助。如果匹配则打印允许的字段,如果不匹配则打印不允许的字段。在上述情况下,如果匹配,则打印“不允许”“允许的名称、主题和标记”
  • 您可以在打印输出前检查允许和不允许的 2 个列表的大小。
  • codeaholic:感谢 sn-p。我很有帮助。你摇滚:)
  • 很好,我很高兴,因为它可以帮助你
【解决方案2】:

试试这个:

    List<String> newBlist = new ArrayList<String>(Blist);

    newBlist.removeAll(Alist);

    StringBuilder response = new StringBuilder();        
    List<String> responseList;
    if (newBlist.isEmpty()) {
        response.append("Allowed ");
        responseList = Blist;
    } else {
        response.append("Not Allowed ");
        responseList = newBlist;
    }

    for (String str : responseList) {
        response.append(str).append(" ");
    }

    System.out.println(response.toString());

【讨论】:

    猜你喜欢
    • 2019-10-17
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    相关资源
    最近更新 更多