// Demonstrates some fun analysis on Facebook data. // To use, first download the RestFB library from http://restfb.com/ // In Eclipse, right click on your project and go to Properties // Then, access Java Build Path > Libraries and click on the "Add External Jar" button // Select the library's jar file. // You'll also need a temporary access token from Facebook (see below) import java.util.*; import com.restfb.*; import com.restfb.types.*; public class FBPlay { public static void main(String[] args) { // get your own temporary access token from // http://developers.facebook.com/tools/explorer/ String token = ""; FacebookClient facebookClient = new DefaultFacebookClient(token); // quick test User zuck = facebookClient.fetchObject("zuck", User.class); System.out.println(zuck.getName()); List myFriends = facebookClient.fetchConnection("me/friends", User.class, Parameter.with("fields", "first_name, gender")) .getData(); System.out.println("Total friends: " + myFriends.size()); // map first names to their frequencies Map nameCounts = new TreeMap(); for (User friend : myFriends) { String name = friend.getFirstName(); if (!nameCounts.containsKey(name)) { nameCounts.put(friend.getFirstName(), 0); } nameCounts.put(name, nameCounts.get(name) + 1); } System.out.println(nameCounts); // map frequencies to sets of names (essentially, flip the key and value) Map> popularities = new TreeMap>(); for (String name : nameCounts.keySet()) { int times = nameCounts.get(name); if (!popularities.containsKey(times)) { popularities.put(times, new TreeSet()); } popularities.get(times).add(name); } System.out.println(popularities); } }