Search code examples
javatwitter4j

Twitter4j IDs to Array


I am attempting to compare two lists. One list contains everyone I am following on twitter, the other is everyone who follows me. I don't know how to do this since .getFollowersIDs and .getFriendsIDs are of type ID. I have looked this up, but I can't understand how to compare the results of this type. I tried treating it like they both were arrays, but Eclipse didn't like that.. http://twitter4j.org/javadoc/twitter4j/IDs.html

The type of the expression must be an array type but it resolved to IDs

package com.follow3d.rob;

import java.util.List;
import twitter4j.*;
import twitter4j.conf.*;

public class Follow3d {

    public static void main(String[] args) {

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey("xxxxxx")
                .setOAuthConsumerSecret("xxxxxx")
                .setOAuthAccessToken("xxxxxx")
                .setOAuthAccessTokenSecret("xxxxxx");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        try {
                long ID = twitter.getId();//Personal Twitter ID.
                IDs FOLLOWERS = twitter.getFollowersIDs(-1);//Numeric Array of every user that follows me. 
                IDs FOLLOWING = twitter.getFriendsIDs(-1);//Numeric Array of every user I am following. 
                while (FOLLOWING.hasNext() == true)
                {
                    int counter = 0;
                    if (FOLLOWING[counter] != FOLLOWERS[counter])//ERROR HERE.
                }
            } catch (TwitterException name) {
                System.out.println("You don't have internet connection.");
        }
    }
}

Solution

  • As explained in the documentation, FOLLOWERS and FOLLOWING are of type IDs (not array) and hence, we can't reference any element inside it by index.

    If we need to compare the followers and following user ids then, we need to use getIDs() method of IDs class (i.e. FOLLOWERS and FOLLOWING objects) and iterate through them. Also, instead of iterating using while loop (as shown in the example), we need to iterate FOLLOWING array for each element of FOLLOWERS array to see if an id exists or not.