private ArrayList<DepthBean> getDifferent2(ArrayList<DepthBean> prelist, ArrayList<DepthBean> curlist) {
ArrayList<DepthBean> diff = new ArrayList<>();
TreeMap<Double, DepthBean> map = new TreeMap<>(new Comparator<Double>() {
@Override
public int compare(Double o1, Double o2) {
return o2.compareTo(o1);
}
});
for (int i = 0; i < prelist.size(); i++) {
map.put(prelist.get(i).key, prelist.get(i));
Log.i("排序前", " key=" + prelist.get(i).key + " value=" + prelist.get(i).value);
}
for (int i = 0; i < curlist.size(); i++) {
if (map.containsKey(curlist.get(i).key)) {
if (curlist.get(i).value == 0) {
//如果有value=0则删除map对应的key
map.remove(curlist.get(i).key);
} else {
//覆盖
map.put(curlist.get(i).key, curlist.get(i));
}
} else {
//不相同
if (curlist.get(i).value == 0) {
map.remove(curlist.get(i).key);
} else {
map.put(curlist.get(i).key, curlist.get(i));
}
}
}
for (Map.Entry entry : map.entrySet()) {
DepthBean depthBean = (DepthBean) entry.getValue();
//只保存前5条数据
if (diff.size() < 5) {
diff.add(depthBean);
} else {
break;
}
}
Collections.sort(diff, new Comparator<DepthBean>() {
@Override
public int compare(DepthBean o1, DepthBean o2) {
Double d1 = o1.key;
Double d2 = o2.key;
return d2.compareTo(d1);
}
});
for (int i = 0; i < diff.size(); i++) {
Log.e("排序后", " key=" + diff.get(i).key + " value=" + diff.get(i).value);
}
return diff;
}
public class DepthBean {
public double key, value;
public DepthBean(double key, double value) {
this.key = key;
this.value = value;
}
}
问题描述: 有个功能,1列表数据对比2列表数据,
1 如果1列表.key = 2列表.key && 2列表.value =0 则删除1列表的key
2 如果没有相同的则添加到一列表
3 最后并排序。