2014-12-08 127 views
-1
List<Status> tweets = result.getTweets(); 

for (Status tweet : tweets) { 
    if(!(tweet.getUser().getScreenName().toString().equals("gh") || tweet.getUser().getScreenName().toString().equals("gh"))){ 
     List<Status> Stats= tw.getHomeTimeline();   
     for (Status StatList : Stats) { 
      if(->here i want to compare HomeTimelin list of tweets with getTweets list<-){      
       try { 
        System.out.println(tweet.getUser().getScreenName()); 
        Thread.sleep(2000); 
       } 
       catch(InterruptedException ex) { 
        Thread.currentThread().interrupt(); 
       }   
      } 
      else{ 
       System.out.println("retweeted"); 
      } 
     } 
    } 
    else{ 
     System.out.println("derp blocked"); 
    } 

在第二个if语句中,我想比较hometimeline列表项和gettweet列表,如果hometime行列表没有gettweet项目,我希望它返回转,如果主场线有它,我希望它返回false。我需要帮助在Java中比较两个列表

回答

0

你可以做类似如下:

List<Status> tweets = result.getTweets(); 
    List<Status> Stats= tw.getHomeTimeline(); 
    for (Status tweet : tweets) { 

     if(!(tweet.getUser().getScreenName().toString().equals("gh") || tweet.getUser().getScreenName().toString().equals("gh"))){ 

      boolean found = false; 
      for (Status StatList : Stats) { 

       if(StatList.getTweetText().equals(tweet.getTweetText())){ 
        found = true; 
        break; 
        try { 
         System.out.println(tweet.getUser().getScreenName()); 

         Thread.sleep(2000); 
        } catch(InterruptedException ex) { 
         Thread.currentThread().interrupt(); 
        } 
       }else{ 
        // nothing here 
        // System.out.println("retweeted"); 
       } 
      } 
      if (found) // you can move this and the initialisation(boolean found = false) out side the upper if statement 
       System.out.println("false"); 
      else 
       System.out.println("turn"); 

     }else{ 
      System.out.println("derp blocked"); 
     } 
    } 
0

您可以使用retainAll来比较两个列表。如下所示:

tweets.retainAll(Stats); // or whatever the names are: 

另外作为一个附注:变量名应该始终以小​​写字母开头!