首先,您需要通过monthOfBirth + country 将人类分类到桶中。这样做应该很便宜 - 只需遍历它们,将每一个弹出到适当的存储桶中。
请注意,附加字符串是解决此问题的“hacky”方式。 “正确”的方法是使用正确的 hashCode 方法创建一个关键对象:
public class MonthCountryKey {
String monthOfBirth;
String country;
// <snip> constructor, setters
@Override public int hashCode() {
return Arrays.hashCode(new Object[] {
monthOfBirth,
country,
});
}
@Override public boolean equals(Object o) {
...
}
}
见:What is a best practice of writing hash function in java?
Map<MonthCountryKey,List<Human>> buckets = new HashMap<List<Human>>;
while(Human human = humanSource.get()) {
MonthCountryKey key = new MonthCountryKey(human.getMonthOfBirth(), human.getCountry());
List list = buckets.get(key);
if(list == null) {
list = new ArrayList<Human>();
buckets.put(key,list);
}
list.add(human);
}
请注意,还有其他种类的 Set。例如,new TreeSet(monthCountryHumanComparator) -- 使用 Apache BeanUtils new TreeSet(new BeanComparator("monthOfBirth.country"))!
如果真的有 很多 人,则可能值得将存储桶存储在数据库中 - SQL 或其他,如您所见。您只需要能够通过存储桶和列表索引号合理快速地获取它们。
然后你可以依次对每个bucket应用一个爱好匹配算法,大大减少了蛮力搜索的规模。
我无法避免将存储桶中的每个人与同一存储桶中的每个其他人进行比较,但您可以做一些工作来降低比较成本。
考虑将爱好编码为整数;每个爱好一点点。一个长的给你多达64个爱好。如果您需要更多,您将需要更多整数或 BigInteger(对两种方法进行基准测试)。当您通过人类工作并遇到新的爱好时,您可以建立兴趣爱好的位位置字典。比较两组爱好然后是廉价的二进制 '&' 后跟 Long.bitCount()。
为了说明,第一个人类有爱好[ "cooking", "cinema" ]
所以右边的位是“烹饪”,左边的下一位是“电影院”,这个人类的编码爱好是二进制 {60 个零}00011 == 3
下一个人喜欢[ "cooking", "fishing" ]
所以fishing 被添加到字典中,这个人的编码爱好是{60 个零}0101 = 5
public long encodeHobbies(List<String> hobbies, BitPositionDictionary dict) {
long encoded = 0;
for(String hobby : hobbies) {
int pos = dict.getPosition(hobby); // if not found, allocates new
encoded &= (1 << pos)
}
return encoded;
}
...与...
public class BitPositionDictionary {
private Map<String,Integer> positions = new HashMap<String,Integer>();
private int nextPosition;
public int getPosition(String s) {
Integer i = positions.get(s);
if(i == null) {
i = nextPosition;
positions.put(i,s);
nextPosition++;
}
return i;
}
}
二进制 & 他们得到 {60 zeroes}0001; Long.bitCount(1) == 1。这两个人有一个共同爱好。
要处理您的第三个人:[“钓鱼”、“俱乐部”、“国际象棋”],您的成本是:
- 添加到爱好->位位置字典并编码为整数
- 与迄今为止创建的所有二进制编码的爱好字符串进行比较
您会希望将二进制编码的爱好存储在访问成本非常低廉的地方。我很想只使用一个长数组,并带有相应的人类索引:
long[] hobbies = new long[numHumans];
int size = 0;
for(int i = 0; i<numHumans; i++) {
hobby = encodeHobbies(humans.get(i).getHobbies(),
bitPositionDictionary);
for(int j = 0; j<size; j++) {
if(enoughBitsInCommon(hobbies[j], hobby)) {
// just record somewhere cheap for later processing
handleMatch(i,j);
}
}
hobbies[size++] = hobby;
}
随着...
// Clearly this could be extended to encodings of more than one long
static boolean enoughBitsInCommon(long x, long y) {
int numHobbiesX = Long.bitCount(x);
int hobbiesInCommon = Long.bitCount(x & y);
// used 128 in the hope that compiler will optimise!
return ((hobbiesInCommon * 128) / numHobbiesX ) > MATCH_THRESHOLD;
}
这样,如果爱好类型足够少,可以长期保存,1GB数组可以保存1.68亿组爱好:)
它应该非常快;我认为 RAM 访问时间是这里的瓶颈。但这是一个蛮力搜索,并且继续是 O(n2)
如果您谈论的是真正庞大的数据集,我怀疑这种方法适用于使用 MapReduce 或其他方式进行分布式处理。
附加说明:您可以使用 BitSet 代替 long(s),并获得更多表现力;也许以牺牲一些性能为代价。再次,基准测试。
long x,y;
...
int numMatches = Long.bitCount(x & y);
... becomes
BitSet x,y;
...
int numMatches = x.and(y).cardinality();
两个字符串不同的位置数称为汉明距离,在 cstheory.so 上有一个回答的问题关于搜索具有接近汉明距离的对:https://cstheory.stackexchange.com/questions/18516/find-all-pairs-of-values-that-are-close-under-hamming-distance --根据我对已接受答案的理解,这是一种可以找到“非常高比例”的匹配项的方法,而不是全部,我猜这确实需要蛮力搜索。