需求:每隔几秒请求数据,并在地图展示。每次返回数据要和上一次数据进行比较。\n
要求:1.两次相同的留在地图,并进行点的移动;2.现在数据与上一次数据不一样的点要显示在地图上;3.上一次与现在数据不一样的点要在地图上消失。
其实自己写也可以,但是有现成的就是用现成的,如下:
1.定义一个 小车 的类,重写 equals 和 hashCode
public class CarModel {
private String plate;
private String driver;
public String getPlate() {
return plate;
}
public void setPlate(String plate) {
this.plate = plate;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public CarModel(String plate) {
this.plate = plate;
}
@Override
public boolean equals(Object obj) {
CarModel model = (CarModel) obj;
return model.getPlate().equals(this.getPlate());
}
@Override
public int hashCode() {
return this.getPlate().hashCode();
}
@Override
public String toString() {
return this.getPlate();
}
}
测试:
public class MyClass {
public static void main(String[] args) {
Set<CarModel> list = new HashSet<>();
CarModel carModel = new CarModel("1");
CarModel carModel1 = new CarModel("2");
CarModel carModel2 = new CarModel("3");
CarModel carModel3 = new CarModel("4");
CarModel carModel4 = new CarModel("5");
list.add(carModel);
list.add(carModel1);
list.add(carModel2);
list.add(carModel3);
list.add(carModel4);
Set<CarModel> list2 = new HashSet<>();
CarModel carModel5 = new CarModel("5");
CarModel carModel6 = new CarModel("7");
CarModel carModel7 = new CarModel("1");
CarModel carModel8 = new CarModel("3");
CarModel carModel9 = new CarModel("9");
list2.add(carModel5);
list2.add(carModel6);
list2.add(carModel7);
list2.add(carModel8);
list2.add(carModel9);
Set<CarModel> jiaoList = new HashSet<>();
jiaoList.addAll(list);
jiaoList.retainAll(list2);
System.out.println("jiao ji :" + Arrays.toString(jiaoList.toArray()));
Set<CarModel> chaModels = new HashSet<>();
chaModels.clear();
chaModels.addAll(list);
chaModels.removeAll(jiaoList);
System.out.println("cha ji :" + Arrays.toString(chaModels.toArray()));
Set<CarModel> bingModels = new HashSet<>();
bingModels.clear();
bingModels.addAll(list);
bingModels.addAll(list2);
System.out.println("bing ji:" + bingModels);
}
}
结果: