簡體   English   中英

如何通過另一個ArrayList中的字段從ArrayList中刪除項目?

[英]How to remove items from ArrayList by fields from another ArrayList?

我有2個ArrayList:

List<ExcludedCalls> excludedCalls = ExcludedCallJpaDao.me().getExcludedCalls();
List<Calls> callsForSend = CallJpaDao.me().getCallsForSend();

public class ExcludedCalls  {
    private long id;
    private String callingNum;
...
}

public class Calls {
    private long id;
    private Date date;
    private Integer secdur;
    private String condcode;
    private String dialednum;
    private String callingnum;
    private Operators operators;
    private Integer status;
    private PollMessage pollMessage;
...
}

我需要從callsForSend刪除所有項目,其中callingnum包含excludedCalls

我嘗試了這個:

public List<Calls> getCallsForSend() {
        List<ExcludedCalls> excludedCalls = ExcludedCallJpaDao.me().getExcludedCalls();
        List<Calls> callsForSend = CallJpaDao.me().getCallsForSend();
        List<Calls> ex = new ArrayList<>();

        for (Calls call : callsForSend) {
            if (excludedCalls.contains(call.getCallingnum())) {
                ex.add(call);
            }
        }
        callsForSend.removeAll(ex);
        return callsForSend;
    }

但是我知道這是錯誤的。 列表具有不同的對象。 我可以從excludedCalls形成Set ,但是我不需要很多foreach。

我建議您使用帶有嵌套循環的Iterator<T> 比較各個對象中的callingNum ,如果相等callingNum其刪除。

Iterator<ExcludedCalls> excludedCallsIterator = excludedCalls.iterator();
Iterator<Calls> callsIterator = callsForSend.iterator();

while (callsIterator.hasNext()) {
  Calls calls = callsIterator.next();
  while (excludedCallsIterator.hasNext()) {
    ExcludedCalls excludedCalls1 = excludedCallsIterator.next();
    if (calls.getCallingnum().equals(excludedCalls1.getCallingNum())) {
      callsIterator.remove();  // remove the object from callsForSend if it matches the current excludedCalls's callingNum.
      break;
    }
  }
}

n java8您可以在流ex上使用過濾器:-

calls.stream().filter(call->!exclude.contains(call.getCallingnum())).collect(Collectors.toList());

對於Java ArrayList, contains()函數通過調用每個對象的equals()函數對列表進行線性搜索O(n)來找到匹配項。

如果您可以更改實現,以使對象共享一個公共接口,例如ICall ,則可以使用以下方法:

public List<Calls> getCallsForSend() {
    List<ExcludedCalls> excludedCalls = ExcludedCallJpaDao.me().getExcludedCalls();
    List<Calls> callsForSend = CallJpaDao.me().getCallsForSend();
    callsForSend.removeAll(excludedCalls);
    return callsForSend; 
}

如果您無法更改實現,那么自己執行迭代是最簡單的方法:

public List<Calls> getCallsForSend() {
    List<ExcludedCalls> excludedCalls = ExcludedCallJpaDao.me().getExcludedCalls();
    List<Calls> callsForSend = CallJpaDao.me().getCallsForSend();
    Iterator<ExcludedCalls> excludedCallsIterator = excludedCalls.iterator();
    Iterator<Calls> callsIterator = callsForSend.iterator();

    while (callsIterator.hasNext()) {
        Calls calls = callsIterator.next();
        while (excludedCallsIterator.hasNext()) {
            ExcludedCalls excludedCalls = excludedCallsIterator.next();
            if (excludedCalls.getCallingNum().equals(calls.getCallingnum())) {
                callsIterator.remove();
                break;
            }
        }
    }
    return callsForSend;
}

如果空間不是問題,則可以將“呼叫”添加到HashMap,然后刪除排除的呼叫列表中存在的呼叫:

public List<Calls> getCallsForSend() {
    List<ExcludedCalls> excludedCalls = ExcludedCallJpaDao.me().getExcludedCalls();
    List<Calls> callsForSend = CallJpaDao.me().getCallsForSend();
    Map<String, Calls> callsMap = new HashMap<>();
    for (Calls call : callsForSend) {
        callsMap.put(call.getCallingnum(), call);
    }

    for (ExcludedCalls excludedCall : excludedCalls) {
        callsMap.remove(excludedCall.getCallingNum());
    }

    return new ArrayList(callsMap.values());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM