2015-04-04 59 views
0

我想構建一個函數,比較2個對象列表並返回一個列表的差異。比較2列表並生成一個新的列表與差異

我將使用它來檢查從搜尋器檢索到的數據是否是新的,並將其與來自我的數據庫的數據進行比較。

public static List checkForNewTweets(List timeline, List database) { 
    List<TimelineTweet> newTweets = new ArrayList<>(); 

    List<TimelineTweet> timelineTweets = timeline; 
    List<TimelineTweet> databaseTweets = database; 

    for (TimelineTweet timelineTweet : timelineTweets) { 
     for (TimelineTweet databaseTweet : databaseTweets) { 
      if (!timelineTweet.equals(databaseTweet)) { 
       newTweets.add(timelineTweet); 
       break; 
      } 
     } 
    } 
    return newTweets; 
} 

這是不工作的,有沒有辦法爲此做一個遞歸函數?

+0

注意:這個函數已經存在於java Collection中;檢查'retainAll()' – GhostCat 2015-04-04 09:59:57

回答

0

您當前的邏輯將所有TimelineTweet小號可能添加到newTweets列表中,如果databaseTweets至少包含兩個元素,因爲它們中的至少一個不等於timelineTweets列表的給定元素。

您完成比較後,才所有databaseTweets可以將其添加到newTweets名單(因爲這是要知道,目前不會在任何databaseTweets匹配的唯一途徑):

for (TimelineTweet timelineTweet : timelineTweets) { 
    boolean found = false; 
    for (TimelineTweet databaseTweet : databaseTweets) { 
     if (timelineTweet.equals(databaseTweet)) {  
      found = true; 
      break; 
     } 
    } 
    if (!found) 
     newTweets.add(timelineTweet); 
} 
0

看着你的代碼我認爲只想從第一個列表中刪除第二個列表的元素並將其分配給第三個列表。

你爲什麼不使用timeline.removeAll(數據庫),然後newTweets.addAll(時間軸)

0

考慮的另一種方法。

public static <E> List<E> getDiff(List<E> list, List<E> list1){ 
    if(list.size() >= list1.size()) 
     return getDiffList(list, list1); 
    return getDiffList(list1, list); 
} 

private static <E> List<E> getDiffList(List<E> list, List<E> list1) { 
    List<E> newList = new ArrayList<>(list); 
    newList.removeAll(list1); 
    return newList; 
} 

請注意,這將報告不同的元素,任何重複將被忽略。

List<String> strings = Arrays.<String>asList("A", "B", "C"); 
List<String> strings1 = Arrays.<String>asList("A","B","D","C","B","Z","A"); 

結果列表將是[d,Z]

0

如果這是一種選擇,你應該有一個瞭解Java的設置類,因爲遏制便宜得多來計算這些類。我想你的TimelineTweet s是Comparable例如wrt時間戳,因此可以在TreeSet中使用,允許在log(n)中查找和插入。一個不同的選項是LinkedHashSet提供插入順序迭代和常量時間操作,或者如果你根本不關心排序,那麼就是HashSet。

如果不再次需要timeline,但只關心在新的鳴叫,你可以刪除所有的項目已經在database

public static TreeSet<TimelineTweet> checkForNewTweets(
     final TreeSet<TimelineTweet> timeline, final TreeSet<TimelineTweet> database) { 
    timeline.removeAll(database); 
    return timeline; 
} 

替代方法:複製timeline,然後可刪除database條目。

public static TreeSet<TimelineTweet> checkForNewTweets(
     TreeSet<TimelineTweet> timeline, TreeSet<TimelineTweet> database) { 
    final TreeSet<TimelineTweet> newTweets = new TreeSet<>(timeline); 
    newTweets.removeAll(database); 
    return timeline; 
} 

該操作適用於Java中的所有集合類,但是對於Set s更快。