Top 10 Twitter Trending Topics using JAVA twitter4j API

Hi friends,

Today we are going to find out the Top 10 Worldwide Trending Topics on Twitter with the help of twitter4j Java API.

The step by step process can be depicted in below picture. It shows the step by step algorithm which you can use in order to show top 10 Worldwide Trending Twitter Topics.

Twitter Trends
Twitter Trends


Now, what do these trending topics mean?

The simple answer is it shows what people are talking about on twitter. It kind of gives you some idea about the topics that are getting discussed over “mentioned” places. In our case, this mentioned place is the entire world i.e. we are going to retrieve the trending topics across the world.

For that we are using the following code.

package com.milind.twitter.trends;
import twitter4j.Trend;
import twitter4j.Trends;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
* @author Milind
public class TwitterTrendingTopics {
public static void main(String[] args) throws IOException, TwitterException {
ConfigurationBuilder cb = new ConfigurationBuilder();
TwitterFactory tf = new TwitterFactory(;
Twitter twitter = tf.getInstance();
Trends trends = twitter.getPlaceTrends(1);
int count = 0;
for (Trend trend : trends.getTrends()) {
if (count < 10) {

There are few things that we should know about the above code. Those are as follows.

  • Consumer Key, Consumer Secret, Access Token and Access Token Secret are the parameters we get after creating our own twitter app at
  • getPlaceTrends() method in line number 25 expects the Yahoo Place Id. In this case, we are passing it as 1 which means we want to find the trends worldwide. Similarly, you can pass any Place Id to get the trending topics for that particular place.
  • The counter that we are using in line number 28 is for getting the top 10 trending topics.

Once I ran above code, I got the following output.It shows the trending topics at that particular time.

Program Output
Program Output

And for verification, I went to my twitter account and saw the latest trending topics on Following is the screenshot of the trending topics on

Actual Trends
Actual Trends

As you can see from above two screenshots, both my program and is giving the same results.

This completes our twitter4j implementation of trending topics.

Thanks for having a read.

Published by milindjagre

I founded my blog four years ago and am currently working as a Data Scientist Analyst at the Ford Motor Company. I graduated from the University of Connecticut pursuing Master of Science in Business Analytics and Project Management. I am working hard and learning a lot of new things in the field of Data Science. I am a strong believer of constant and directional efforts keeping the teamwork at the highest priority. Please reach out to me at for further information. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: