Friday, 21 August 2015

Mahout: FastMap: Implementation of Map


FastMap is optimized implementation of Map interface. Once FastMap reches to maximum size (Integer.MAX_VALUE), then old entries will be removed when new ones are added.
import java.util.Set;

import org.apache.mahout.cf.taste.impl.common.FastMap;
import org.apache.mahout.cf.taste.impl.model.GenericPreference;
import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
import org.apache.mahout.cf.taste.model.Preference;
import org.apache.mahout.cf.taste.model.PreferenceArray;

public class FastMapEx {
 public static void main(String args[]) {
  FastMap<Integer, PreferenceArray> map = new FastMap<Integer, PreferenceArray>();

  PreferenceArray array1 = new GenericUserPreferenceArray(4);
  Preference pref = new GenericPreference(1, 4, 3);
  array1.set(0, pref);

  pref = new GenericPreference(1, 7, 2);
  array1.set(1, pref);

  pref = new GenericPreference(1, 8, 2);
  array1.set(2, pref);

  pref = new GenericPreference(1, 10, 1);
  array1.set(3, pref);

  PreferenceArray array2 = new GenericUserPreferenceArray(5);
  pref = new GenericPreference(2, 3, 2);
  array2.set(0, pref);

  pref = new GenericPreference(2, 4, 3);
  array2.set(1, pref);

  pref = new GenericPreference(2, 6, 3);
  array2.set(2, pref);

  pref = new GenericPreference(2, 7, 1);
  array2.set(3, pref);

  pref = new GenericPreference(2, 9, 1);
  array2.set(3, pref);

  map.put(1, array1);
  map.put(2, array2);

  Set<Integer> keySet = map.keySet();
  for (Integer key : keySet) {
   PreferenceArray array = map.get(key);

   for (int i = 0; i < array.length(); i++) {
    System.out.println(array.getUserID(i) + ","
      + array.getItemID(i) + "," + array.getValue(i));
   }
  }

 }
}


Output
1,4,3.0
1,7,2.0
1,8,2.0
1,10,1.0
2,3,2.0
2,4,3.0
2,6,3.0
2,9,1.0
2,0,0.0



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment