@@ -0,0 +1,2732 @@
+/* %0A* @Author: Zeyuan Shang%0A* @Date:   2016-01-22 23:09:19%0A* @Last Modified by:   Zeyuan Shang%0A* @Last Modified time: 2016-01-22 23:09:26%0A*/%0A%0A//author shafaet, shafaet@hackerrank.com%0Aimport java.util.*;%0Aimport org.json.simple.JSONObject;%0A%0Aclass MapReduce%0A%7B%0A   private LinkedHashMap intermediate;%0A   private JSONObject finalResult = new JSONObject();%0A   private int resultCount;%0A   MapReduce()%0A   %7B%0A      resultCount=0;%0A      finalResult = new JSONObject();%0A      intermediate=new LinkedHashMap();%0A   %7D%0A   %0A   JSONObject execute(JSONObject inputdata)%0A   %7B%0A      for(int i=0;i%3Cinputdata.size();i++)%0A      %7B%0A         LinkedHashMap record=(LinkedHashMap)inputdata.get(i);%0A         mapper(record);%0A      %7D%0A      %0A      Iterator it = intermediate.entrySet().iterator();%0A      while (it.hasNext()) %7B%0A         Map.Entry pair = (Map.Entry)it.next();%0A         reducer((int)pair.getKey(), (HashSet)pair.getValue());%0A         it.remove();%0A      %7D%0A      return finalResult;%0A      %0A      %0A   %7D%0A   private void emit(LinkedHashMap obj)%0A   %7B%0A      finalResult.put(resultCount++,obj);%0A   %7D%0A   private void reducer(int key, HashSet value)%0A   %7B%0A      LinkedHashMap obj=new LinkedHashMap();%0A      obj.put(%22key%22,key);%0A      emit(obj);%0A      %0A   %7D%0A   private void mapper(LinkedHashMap record)%0A   %7B%0A      String value=(String)record.get(%22value%22);%0A      int key=(int)record.get(%22key%22);%0A      emitIntermediate(key, String.valueOf(key)); //Complete this line%0A      %0A   %7D%0A   private void emitIntermediate(int key, String value)%0A   %7B%0A      if(!intermediate.containsKey(key))%0A         intermediate.put(key,new HashSet());%0A%0A      HashSet temp=(HashSet)intermediate.get(key);%0A      temp.add(value);%0A      intermediate.put(key,temp);%0A   %7D%0A%7D%0Apublic class Solution%0A%7B%0A   %0A   public static void main(String %5B%5Dargh)%0A   %7B%0A      JSONObject inputdata= new JSONObject();%0A      Scanner sc=new Scanner(System.in);%0A      int Nr=sc.nextInt();%0A      int Ns=sc.nextInt();%0A      int %5B%5DR=new int%5BNr%5D;%0A      int %5B%5DS=new int%5BNs%5D;%0A      int c=0;%0A      for(int i=0;i%3CNr;i++)%0A      %7B%0A         %0A         Map obj=new LinkedHashMap();%0A         R%5Bi%5D=sc.nextInt();%0A         obj.put(%22value%22,%22R%22);%0A         obj.put(%22key%22,R%5Bi%5D);%0A         inputdata.put(c,obj);%0A         c++;%0A      %7D%0A      %0A      for(int i=0;i%3CNs;i++)%0A      %7B%0A         Map obj=new LinkedHashMap();%0A         S%5Bi%5D=sc.nextInt();%0A         obj.put(%22value%22,%22S%22);%0A         obj.put(%22key%22,S%5Bi%5D);%0A         inputdata.put(c,obj);%0A         c++;%0A      %7D%0A      %0A      %0A      MapReduce mapred= new MapReduce();%0A      JSONObject result=mapred.execute(inputdata); %0A   %0A      for(int i=0;i%3Cresult.size();i++)%0A      %7B%0A         LinkedHashMap record=(LinkedHashMap)result.get(i);%0A         System.out.println(record.get(%22key%22));%0A%0A      %7D%0A   %0A   %7D%0A   %0A%7D%0A

add relational-mapreduce-patterns-4-unions