如何高效找出对象发生变化的属性
【摘要】 直接上需求,看需求分析代码如图,假设选项A固定,可增删,不可改变,选项B可自定义但不可重复,并且横向对应A:出行,B:去公园,value:1,以此类推如果当前内容发生修改时如何找出B发生改变或删除的iterm。前端以Json形式传入,转成对象后比对。思路一因为value是不允许重复的,将新的List传入以value为k,name为v,放入map,遍历原list,如果map中去get此对象,获...
直接上需求,看需求分析代码
如图,假设选项A固定,可增删,不可改变,选项B可自定义但不可重复,并且横向对应A:出行,B:去公园,value:1,以此类推
如果当前内容发生修改时如何找出B发生改变或删除的iterm。
前端以Json形式传入,转成对象后比对。
思路一
因为value是不允许重复的,将新的List传入以value为k,name为v,放入map,遍历原list,如果map中去get此对象,获取不到标明是此对象删除,否则比对那么是否发生改变,发生改变的去放入下行集合。
private void selectDiffOptions(Passenger Vo,List<Passenger> vos) {
List<Passenger> oldList = JSON.parseArray(vo, Passenger.class);
Map<String, String> map = vos.stream().collect(Collectors.toMap(p -> p.getUserId(), p -> p.getType()));
List<Passenger> diffList=new ArrayList<>();
oldList.forEach(vo ->
{
Object o = map.get(vo.getValue());
if (o == null) {
diffList.add(vo);
} else {
if (!vo.getLabel().equals(map.get(vo.getValue()))) {
Passenger optionsDto = vos.stream().filter(po -> po.getName().equals(map.get(vo.getValue()))).findFirst().get();
diffList.add(optionsDto);
}
}
}
);
System.out.println(JSONObject.toJSONString(diffList));
}
思路二
迭代原集合,和新集合,如果name不同,则是修改的,且此时将这个元素删除,以便于下次遍历更快,如果遍历到底都没有发现此元素,则表明被删除
public class ListDemo {
static class Passenger {
public String type, name, value;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
boolean eq = obj instanceof Passenger;
if (!eq) {
throw new RuntimeException();
}
Passenger passenger = (Passenger) obj;
return passenger.value.equals(this.value);
}
public Passenger(String type, String name, String value) {
this.type = type;
this.name = name;
this.value = value;
}
@Override
public String toString() {
return new StringJoiner(", ", Passenger.class.getSimpleName() + "[", "]")
.add("type='" + type + "'")
.add("name='" + name + "'")
.add("value='" + value + "'")
.toString();
}
}
public static void main2(String[] args) {
List<Passenger> src = getSrcList(), newData = getNewList();
List<Passenger> result = new ArrayList<>();
Iterator<Passenger> srcIterator = src.iterator();
while (srcIterator.hasNext()) {
Passenger srcItem = srcIterator.next();
Iterator<Passenger> newIterator = newData.iterator();
int index = 1,max = newData.size();
while (newIterator.hasNext()) {
Passenger next = newIterator.next();
if (next.value == srcItem.value && !next.name.equals(srcItem.name)) {
result.add(next);
newIterator.remove();
break;
}
index++;
}
if(index == max){
result.add(srcItem);
}
}
System.out.println(result);
}
public static void main(String[] args) {
List<Passenger> src = getSrcList(), newData = getNewList();
List<Passenger> addList = new ArrayList<>(), delList = new ArrayList<>();
int max = Math.max(src.size(), newData.size());
for (int i = 0; i < max; i++) {
if (i < src.size() && !newData.contains(src.get(i))) {
delList.add(src.get(i));
}
if (i < newData.size() && !src.contains(newData.get(i))) {
addList.add(newData.get(i));
}
}
src.remove(delList);
newData.remove(addList);
Iterator<Passenger> srcIterator = src.iterator();
List<Passenger> result = new ArrayList<>();
while (srcIterator.hasNext()) {
Passenger srcItem = srcIterator.next();
Iterator<Passenger> newIterator = newData.iterator();
while (newIterator.hasNext()) {
Passenger next = newIterator.next();
if (next.value == srcItem.value && !next.name.equals(srcItem.name)) {
result.add(next);
newIterator.remove();
}
}
}
result.addAll(delList);
System.out.println(result);
System.out.println(addList);
}
private static List<Passenger> getNewList() {
return new ArrayList<>(Arrays.asList(new Passenger("Type_1", "Name_1", "1"), new Passenger("Type_2", "Name_2", "2"),new Passenger("Type_4", "Name_4", "4")));
}
private static List<Passenger> getSrcList() {
return new ArrayList<>(Arrays.asList(new Passenger("Type_1", "Name_1", "1"), new Passenger("Type_2", "Name_3", "2"),new Passenger("Type_3", "Name_3", "3")));
}
}
因为业务遍历次数不会多,所以不考虑效率问题
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)