package invertedindex;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import misc.TextPairWritable;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class InvertedIndexComplexKeyReducer extends Reducer<TextPairWritable, DoubleWritable, Text, Text> {
	String lastTerm;
	List<String> postings;
	
	@Override
	public void setup(Context context) {
		lastTerm = null;
		postings = new ArrayList<String>();
	}
	
	@Override
	public void reduce(TextPairWritable key, Iterable<DoubleWritable> values, Context context)
			throws IOException, InterruptedException {
		String term = key.getFirst().toString();
		String page = key.getSecond().toString();
		
		if (lastTerm != null && !term.equals(lastTerm)) {
			context.write(new Text(lastTerm), new Text(StringUtils.join(postings, ";")));
			postings.clear();
		}
		
		for (DoubleWritable value : values) {
			postings.add(String.format("%s,%.4f", page, value.get()));
		}
		lastTerm = term;
	}
	
	@Override
	public void cleanup(Context context) throws IOException, InterruptedException {
		context.write(new Text(lastTerm), new Text(StringUtils.join(postings, ";")));
	}
}

@@ -49,134 +49,39 @@
 on;%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport misc.TextPairWritable;%0A%0Aimport org.apache.commons.lang.StringUtils;
+%0Aimport misc.TextPairWritable;%0A
 %0Aimp
@@ -309,16 +309,24 @@
 ext%3E %7B%0A%09
+private 
 String l
@@ -336,32 +336,69 @@
 Term
-;%0A%09List%3CString%3E postings
+ = null;%0A%09private StringBuilder builder = new StringBuilder()
 ;%0A%09%0A
@@ -471,41 +471,27 @@
 ;%0A%09%09
-postings = new ArrayList%3CString%3E(
+builder.setLength(0
 );%0A%09
@@ -599,17 +599,16 @@
 text)%0A%09%09
-%09
 throws I
@@ -635,25 +635,26 @@
 tedException
- 
+%0A%09
 %7B%0A%09%09String t
@@ -792,113 +792,72 @@
 %0A%09%09%09
-context.write(new Text(lastTerm), new Text(StringUtils.join(postings, %22;%22)));%0A%09%09%09postings.clear(
+write(context);%0A%09%09%09builder.setLength(0
 );%0A%09%09%7D%0A
+%0A
 %09%09
+lastTerm = term;
 %0A%09%09f
@@ -900,19 +900,21 @@
 %0A%09%09%09
-postings.ad
+builder.appen
 d(St
@@ -933,16 +933,17 @@
 %22%25s,%25.4f
+:
 %22, page,
@@ -962,35 +962,16 @@
 ));%0A%09%09%7D%0A
-%09%09lastTerm = term;%0A
 %09%7D%0A%09%0A%09@O
@@ -1059,16 +1059,162 @@
 ption %7B%0A
+%09%09write(context);%0A%09%7D%0A%09%0A%09private void write(Context context) throws IOException, InterruptedException %7B%0A%09%09builder.setLength(builder.length() - 1);%0A
 %09%09contex
@@ -1254,38 +1254,25 @@
 ext(
-StringUtils.join(postings, %22;%22
+builder.toString(
 )));

change from using ArrayList<String> to using StringBuilder for values