Saturday, 22 August 2015

Mahout: FastByIDMap

Like HashMap, FastByIDMap is hash-based, but there are some differences.

1. FastByIDMap uses linear probing technique to handle collisions.
2. Key is of type long
3. Once FastByIDMap reches to maximum size (Integer.MAX_VALUE), then old entries will be removed when new ones are added.
import org.apache.mahout.cf.taste.impl.common.FastByIDMap;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
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 FastByIDMapEx {
 public static void main(String args[]) {
  FastByIDMap<PreferenceArray> map = new FastByIDMap<PreferenceArray>(1);

  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);

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

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

  LongPrimitiveIterator iter = map.keySetIterator();

  while (iter.hasNext()) {
   long key = iter.next();
   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,9,1.0



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment