# 请教一个算法问题

``````List<String> str = new ArrayList<>();

``````class Bean {
private String year;
private String cnt;
//  get set.....
}

List<String> str2 = new ArrayList<>();

``List<String> str3 = new ArrayList<>();``

``````// bean的year为2016 cnt为100
// bean的year为2017 cnt为100
// bean的year为2018 cnt为0``````

### 回答：

``````    List<String> str1 = new ArrayList<>();

List<Bean> str2 = new ArrayList<>();

List<String> str3 = new ArrayList<>();

List<String> temp = new ArrayList<>();

str2.stream().forEach(bean -> {
});

str1.stream().forEach(str -> {
if(temp.contains(str)){
}else{
}
});

System.out.println(str3.toString());``````

### 回答：

``````import java.util.ArrayList;
import java.util.List;

public static void main(String[] args) {
List<String> str = new ArrayList<>();
List<Bean> str2 = new ArrayList<>();
// bean的year为2016 cnt为100
// bean的year为2017  cnt为150
List<Bean> str3 = new ArrayList<>();
deal(str, str2, str3);
System.out.println(str3);
}

private static void deal(List<String> str, List<Bean> str2, List<Bean> str3) {
//存储交集
List<String> years = new ArrayList<>();
for (Bean temp : str2) {
if (str.contains(temp.getYear())) {
} else {
temp.setCnt("0");
}
}
for (String s : str) {
if (!years.contains(s)) {
}
}
}
}
class Bean {
private String year;
private String cnt;

public Bean() {
}

public Bean(String year, String cnt) {
this.year = year;
this.cnt = cnt;
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Bean{");
sb.append("year='").append(year).append('\'');
sb.append(", cnt='").append(cnt).append('\'');
sb.append('}');
return sb.toString();
}

public String getYear() {
return year;
}

public void setYear(String year) {
this.year = year;
}

public String getCnt() {
return cnt;
}

public void setCnt(String cnt) {
this.cnt = cnt;
}
}
``````