import java.util.ArrayList; import java.io.IOException; import java.util.Collection; import java.util.Iterator; import java.util.TreeMap; import java.lang.*; import org.apache.commons.lang.StringUtils; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; public void reduce(final Text key, final Iterable values, final Context context) throws IOException, InterruptedException { ArrayList commonFriends = new ArrayList(); ArrayList allcommonFriends = new ArrayList(); for (Text word : values) { //java.lang.String[] friends = word.toString().split(" ");//convert string to string array //commonFriends.add(friends); allcommonFriends.add(word.toString()); } //if(values.size()!=2) System.out.println("Error"); StringTokenizer list1 = new StringTokenizer((String[]) allcommonFriends[0].toArray(); StringTokenizer list2 = new StringTokenizer((String[]) allcommonFriends[1].toArray(); while (list1.hasMoreTokens()) { friend1=list1.nextToken(); while (list2.hasMoreTokens()) { friend2=list2.nextToken(); if(friend1 == friend2) commonFriends(friend1); } } } Collection allValues= commonFriends.values(); java.lang.String entireText = StringUtils.join(allValues, ','); friendList.set(entireText); context.write(key, friendList); }