import java.util.*;
public class FriendRecommend {
Map<String, List<Recommend>> totalMap = new Hashtable<String, List<Recommend>>();
public void getReconList(String UserId) {
if (totalMap.containsKey(UserId)) {
List<Recommend> recommends = totalMap.get(UserId);
// 打印Recommend //输出 recommend 内容
} else {
List<String> friends = getFriendsListForUser(UserId);
Map<String, Recommend> recommendMap = new HashMap<String, Recommend>();
for (String friend : friends) {
List<String> recomList = getPurchaseForUser(friend);
for (String prod : recomList) {
if (recommendMap.containsKey(prod)) {
Recommend recommend = recommendMap.get(prod);
recommend.setFrequency(recommend.getFrequency() + 1);
recommendMap.put(prod, recommend);
} else {
recommendMap.put(prod, new Recommend(prod, 1));
}
}
}
Iterator<String> iter = recommendMap.keySet().iterator();
List<Recommend> recommend = new ArrayList<FriendRecommend.Recommend>();
String product = iter.next();
Recommend recProd = recommendMap.get(product);
recommend.add(recProd);
Collections.sort(recommend, new sortByFrequency());
totalMap.put(UserId, recommend);
// 输出 recommend 内容
}
}
class Recommend {
public Recommend(String ProdId, int frequency) {
this.productID = ProdId;
this.frequency = frequency;
}
private int frequency;
private String productID;
public int getFrequency() {
return frequency;
}
public void setFrequency(int frequency) {
this.frequency = frequency;
}
public String getProductID() {
return productID;
}
public void setProductID(String productID) {
this.productID = productID;
}
}
private static final class sortByFrequency implements Comparator<Recommend> {
public int compare(Recommend o1, Recommend o2) {
// TODO Auto-generated method stub
return o2.getFrequency() - o1.getFrequency();
}
}
public static List<String> getPurchaseForUser(String CustomerId) {
return new ArrayList<String>();
}
public static List<String> getFriendsListForUser(String CustomerId) {
return new ArrayList<String>();
}
}