Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4106434
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:22:35+00:00 2026-05-20T21:22:35+00:00

for (Tweet tweet : tweets) { for(long forId : idFromArray){ long tweetId = tweet.getId();

  • 0
for (Tweet tweet : tweets) {                
    for(long forId : idFromArray){
        long tweetId = tweet.getId();
        if(forId != tweetId){
            String twitterString = tweet.getText();
            db.insertTwitter(twitterString, tweetId);
        }
    }
}

My code won’t run pass the first for{} loop, that’s why idFromArray is empty since I don’t add anything there until a tweet is has been added to the database.

And even if there is something in the array it loops the whole thing twice (DUH! Since I have two loops) which makes the database very bloated with the same tweets.

It is not a simple compare of the two tweets id and simply ignore the ones with the same id.

I’m pretty certain there is a really simple solution to this problem, but I still can’t wrap my head around it. Anybody?

UPDATE:

What I want is the code to ignore the the tweetId that already is in the database.
And just insert the tweets that is not in the database.

I don’t think I should have two for-loops, I think the second loop should be replaced with something? (or maybe I’m wrong?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:22:35+00:00Added an answer on May 20, 2026 at 9:22 pm

    If I understand correctly, what you want to do, in pseudo-code is the following:

    for (Tweet tweet : tweets) {
        if (!db.containsTweet(tweet.getId())) {
            db.insertTweet(tweet.getText(), tweet.getId());
        }
    }
    

    I assume your db class actually uses an sqlite database as a backend? What you could do is implement containsTweet directly and just query the database each time, but that seems less than perfect. The easiest solution if we go by your base code is to just keep a Set around that indexes the tweets. Since I can’t be sure what the equals() method of Tweet looks like, I’ll just store the identifiers in there. Then you get:

    Set<Integer> tweetIds = new HashSet<Integer>(); // or long, whatever
    for (Tweet tweet : tweets) {
        if (!tweetIds.contains(tweet.getId())) {
            db.insertTweet(tweet.getText(), tweet.getId());
            tweetIds.add(tweet.getId());
        }
    }
    

    It would probably be better to save a tiny bit of this work, by sorting the list of tweets to begin with and then just filtering out duplicate tweets. You could use:

    // if tweets is a List
    Collections.sort(tweets, new Comparator() {
        public int compare (Object t1, Object t2) {
            // might be the wrong way around
            return ((Tweet)t1).getId() - ((Tweet)t2).getId();
        }
    }
    

    Then process it

    Integer oldId;
    for (Tweet tweet : tweets) {
        if (oldId == null || oldId != tweet.getId()) {
            db.insertTweet(tweet.getText(), tweet.getId());
        }
        oldId = tweet.getId();
    }
    

    Yes, you could do this using a second for-loop, but you’ll run into performance problems much more quickly than with this approach (although what we’re doing here is trading time for memory performance, of course).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to cast a tweet id, which is a long, to a string.
I am getting at extremely fast rate, tweets from a long-lived connection to the
I am unable to tweet continuously in twitter - every three tweets I get:
HI all, can we post images with tweets in twittter through code in iphone????
I would like to use the API to return all tweets that match my
So I'm pulling down a user's tweet steam in JSON format via PHP. I'd
I'm using sed -e s/\*DIVIDER\*/$DIVIDER/g to replace *DIVIDER* with a user-specified string, which is
I use the following function in CodeIgniter, to get my latest tweet: function tweet($id)
This is really strange, I'm not sure what could be causing the code to
I have a bunch of tweets that are returned as plain text that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.