我有一个班级:
public class Address {
private String country;
private String state;
private String city;
}
还有一个Person对象列表。人类看起来像:
public class Person {
private String country;
private String state;
private String city;
//other fields
}
我需要过滤Person
个对象并获得最合适的对象。 Address
对象可以至少有一个非空字段。 Person
对象可以没有,部分或全部提到的字段已初始化。
以下是可能的输入示例之一:
Three Person objects:
a. PersonA: country = 'A'
b. PersonB: country = 'A', state = 'B'
c. PersonC: country = 'A', state = 'B', city = 'C'
Address object:
a. Address: country = 'A', state = 'B'
过滤后的预期结果是PersonB。如果只有PersonA和PersonC对象,那么PersonA更可取。
我想展示我是如何尝试这样做的,但事实上它是纯粹的强力算法而我不喜欢它。算法复杂度随着新增字段而增加。我还考虑过使用谓词的番石榴过滤器,但不知道谓词应该是什么。
除了暴力之外,还有什么比这种过滤更好的算法?
答案 0 :(得分:1)
据我所知,蛮力你的意思是检查所有实体的所有字段。好吧,如果你不能重构你的课程,那是不可能的,但有一个简单的技巧会有所帮助。它使用state
模式。
您可以在两个类中添加标记notNulls
:
public class Address {
private int notNulls = 0;
private String country;
private String state;
private String city;
}
public class Person {
private int notNulls = 0;
private String country;
private String state;
private String city;
//other fields
}
我将向您展示一个setter的可能实现,其余的类似:
public void setCountry(String s) {
if (country == null {
if (s != null) {
country = s;
notNulls++;
}
} else {
if (s == null) {
country == null;
notNulls--;
} else {
country = s;
}
}
}
public boolean isValid() {
return notNulls != 0;
}
现在您可以简单地遍历对象。
答案 1 :(得分:1)
为了避免蛮力,你需要按地址索引你的人。对于一个好的搜索,你肯定需要一个国家(猜测它或以某种方式默认它,否则结果会不太准确)。
索引将是一个数字,一个国家的前三位数字,一个州的后三个数字和一个城市的后四个数字。在这种情况下,你可以存储213个国家(only 206 as of 2016),最多999个州和9999个城市。
它使我们能够使用hashCode和TreeSet来索引您的Person实例,并以O(log(n))方式部分地通过地址查找它们而不触及它们的字段。将在TreeSet构造中触及字段,并且您需要添加一些额外的逻辑来修改Person以保持索引完整。
从国家/地区
开始,每个部分的指数均按顺序计算 import java.util.HashMap;
import java.util.Map;
public class PartialAddressSearch {
private final static Map<String, AddressPartHolder> COUNTRY_MAP = new HashMap<>(200);
private static class AddressPartHolder {
int id;
Map<String, AddressPartHolder> subPartMap;
public AddressPartHolder(int id, Map<String, AddressPartHolder> subPartMap) {
this.id = id;
this.subPartMap = subPartMap;
}
}
public static int getCountryStateCityHashCode(String country, String state, String city) {
if (country != null && country.length() != 0) {
int result = 0;
AddressPartHolder countryHolder = COUNTRY_MAP.get(country);
if (countryHolder == null) {
countryHolder = new AddressPartHolder(COUNTRY_MAP.size() + 1, new HashMap<>());
COUNTRY_MAP.put(country, countryHolder);
}
result += countryHolder.id * 10000000;
if (state != null) {
AddressPartHolder stateHolder = countryHolder.subPartMap.get(state);
if (stateHolder == null) {
stateHolder = new AddressPartHolder(countryHolder.subPartMap.size() + 1, new HashMap<>());
countryHolder.subPartMap.put(state, stateHolder);
}
result += stateHolder.id * 10000;
if (city != null && city.length() != 0) {
AddressPartHolder cityHolder = stateHolder.subPartMap.get(city);
if (cityHolder == null) {
cityHolder = new AddressPartHolder(stateHolder.subPartMap.size() + 1, null);
stateHolder.subPartMap.put(city, cityHolder);
}
result += cityHolder.id;
}
}
return result;
} else {
throw new IllegalArgumentException("Non-empty country is expected");
}
}
对于您的Person和Address类,您可以根据int的自然顺序定义hashCode和compareTo:
public class Person implements Comparable {
private String country;
private String state;
private String city;
@Override
public boolean equals(Object o) {
//it's important but I removed it for readability
}
@Override
public int hashCode() {
return getCountryStateCityHashCode(country, state, city);
}
@Override
public int compareTo(Object o) {
//could be further improved by storing hashcode in a field to avoid re-calculation on sorting
return hashCode() - o.hashCode();
}
}
public class Address implements Comparable {
private String country;
private String state;
private String city;
@Override
public boolean equals(Object o) {
//removed for readability
}
@Override
public int hashCode() {
return getCountryStateCityHashCode(country, state, city);
}
@Override
public int compareTo(Object o) {
//could be further improved by storing hashcode in a field to avoid re-calculation on sorting
return hashCode() - o.hashCode();
}
}
public class AddressPersonAdapter extends Person {
private final Address delegate;
public AddressPersonAdapter(Address delegate) {
this.delegate = delegate;
}
@Override
public boolean equals(Object o) {
return delegate.equals(o);
}
@Override
public int hashCode() {
return delegate.hashCode();
}
}
之后,您的过滤代码将缩小为填充索引并计算部分地址的楼层:
TreeSet<Person> personSetByAddress = new TreeSet<>();
Person personA = new Person();
personA.setCountry("A");
personSetByAddress.add(personA);
Person personB = new Person();
personB.setCountry("A");
personB.setState("B");
personSetByAddress.add(personB);
Person personC = new Person();
personC.setCountry("A");
personC.setState("B");
personC.setCity("C");
personSetByAddress.add(personC);
Address addressAB = new Address();
addressAB.setCountry("A");
addressAB.setState("B");
System.out.println(personSetByAddress.floor(new AddressPersonAdapter(addressAB)));
Yields:
Person{hashCode=10010000, country='A', state='B', city='null'}
如果你没有PersonB:
TreeSet<Person> personSetByAddress = new TreeSet<>();
Person personA = new Person();
personA.setCountry("A");
personSetByAddress.add(personA);
Person personC = new Person();
personC.setCountry("A");
personC.setState("B");
personC.setCity("C");
personSetByAddress.add(personC);
Address addressAB = new Address();
addressAB.setCountry("A");
addressAB.setState("B");
System.out.println(personSetByAddress.floor(new AddressPersonAdapter(addressAB)));
Yields:
Person{hashCode=10000000, country='A', state='null', city='null'}
修改强>
需要额外验证的角落情况是在同一国家内没有更大(或更小,如果我们需要上限)元素。 E.g:
TreeSet<Person> personSetByAddress = new TreeSet<>();
Person personA = new Person();
personA.setCountry("D");
personSetByAddress.add(personA);
Person personC = new Person();
personC.setCountry("A");
personC.setState("B");
personC.setCity("C");
personSetByAddress.add(personC);
Address addressAB = new Address();
addressAB.setCountry("A");
addressAB.setState("B");
System.out.println(personSetByAddress.floor(new AddressPersonAdapter(addressAB)));
Yields:
Person{hashCode=10000000, country='D', state='null', city='null'}
即。我们去了最近的国家。要解决这个问题,我们需要检查国家/地区的数字是否仍然相同。我们可以通过对TreeSet进行子类化并在那里添加此检查来实现:
//we need this class to allow flooring just by id
public class IntegerPersonAdapter extends Person {
private Integer id;
public IntegerPersonAdapter(Integer id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
return id.equals(o);
}
@Override
public int hashCode() {
return id.hashCode();
}
@Override
public int compareTo(Object o) {
return id.hashCode() - o.hashCode();
}
@Override
public String toString() {
return id.toString();
}
}
public class StrictCountryTreeSet extends TreeSet<Person> {
@Override
public Person floor(Person e) {
Person candidate = super.floor(e);
if (candidate != null) {
//we check if the country is the same
int candidateCode = candidate.hashCode();
int eCode = e.hashCode();
if (candidateCode == eCode) {
return candidate;
} else {
int countryCandidate = candidateCode / 10000000;
if (countryCandidate == (eCode / 10000000)) {
//we check if the state is the same
int stateCandidate = candidateCode / 10000;
if (stateCandidate == (eCode / 10000)) {
//we check if is a state
if (candidateCode % 10 == 0) {
return candidate;
} else { //since it's not exact match we haven't found a city - we need to get someone just from state
return this.floor(new IntegerPersonAdapter(stateCandidate * 10000));
}
} else if (stateCandidate % 10 == 0) { //we check if it's a country already
return candidate;
} else {
return this.floor(new IntegerPersonAdapter(countryCandidate * 10000000));
}
}
}
}
return null;
}
现在,在我们初始化null
后,我们的测试用例会产生StrictCountryTreeSet
:
TreeSet<Person> personSetByAddress = new StrictCountryTreeSet();
Person personA = new Person();
personA.setCountry("D");
personSetByAddress.add(personA);
Person personC = new Person();
personC.setCountry("A");
personC.setState("B");
personC.setCity("C");
personSetByAddress.add(personC);
Address addressAB = new Address();
addressAB.setCountry("A");
addressAB.setState("B");
System.out.println(personSetByAddress.floor(new AddressPersonAdapter(addressAB)));
Yields:
null
对不同状态的测试也会产生null
:
TreeSet<Person> personSetByAddress = new StrictCountryTreeSet();
Person personD = new Person();
personD.setCountry("D");
personSetByAddress.add(personD);
Person personE = new Person();
personE.setCountry("A");
personE.setState("E");
personSetByAddress.add(personE);
Person personC = new Person();
personC.setCountry("A");
personC.setState("B");
personC.setCity("C");
personSetByAddress.add(personC);
Address addressA = new Address();
addressA.setCountry("A");
Address addressAB = new Address();
addressAB.setCountry("A");
addressAB.setState("B");
Address addressABC = new Address();
addressABC.setCountry("A");
addressABC.setState("B");
addressABC.setCity("C");
System.out.println(personSetByAddress.floor(new AddressPersonAdapter(addressAB)));
Yields:
null
请注意,在这种情况下,您需要将hashCode结果存储在Address和Person clasess中,以避免重新计算。