直接上代码
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
Temp temp1 = new Temp(11L, 1, "11111");
Temp temp2 = new Temp(22L, 2, "22222");
Temp temp3 = new Temp(22L, 2, "22222");
Temp temp4 = new Temp(33L, 3, "33333");
List<Temp> old = new ArrayList<>();
old.add(temp1);
old.add(temp2);
List<Temp> newL = new ArrayList<>();
newL.add(temp3);
newL.add(temp4);
List<Integer> collectId = newL.stream().map(Temp::getId).filter(id -> !old.stream().map(Temp::getId)
.collect(Collectors.toList()).contains(id)).collect(Collectors.toList());
List<Long> collectMainId = newL.stream().filter(it -> !old.stream().map(Temp::getId).collect(Collectors.toList())
.contains(it.getId())).map(Temp::getMainId).collect(Collectors.toList());
System.out.println(collectId);
System.out.println(collectMainId);
}
}
@Data
class Temp {
Long mainId;
Integer id;
String item;
public Temp(Long mainId, Integer id, String item) {
this.mainId = mainId;
this.id = id;
this.item = item;
}
}