1、去重
ListmyList = listAll.stream().distinct().collect(Collectors.toList());
2、根据单个对象属性去重
list.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet(Comparator.comparing(UserCar::getCarNo))), ArrayList::new));
或者
list.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o->o.getCarNo()))), ArrayList::new));
3、根据多个对象属性去重
list.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o->o.getDetailID()+ ";" + o.receiptCode()))),ArrayList::new));
4、根据单个对象属性合并
list.stream().collect(Collectors.toMap(BillsNums::getId, a -> a, (o1,o2)-> {
o1.setNums(o1.getNums() + o2.getNums());
o1.setSums(o1.getSums() + o2.getSums());
return o1;
})).values().stream().collect(Collectors.toList());
5、根据多个对象属性合并
list.parallelStream().collect(Collectors.groupingBy(o -> (o.geName() + o.getAge()), Collectors.toList())).forEach((id, transfer) -> { transfer.stream().reduce((a, b) -> new Student(a.getName(), a.getAge(), a.getScore() + b.getScore())).ifPresent(studentList::add);});
或使用Map:需重写hashCode()、equals()方法
原因移步另一篇文章:Java中将对象当成map的key存在的问题与解决方案
//新建类,作为map的key
static class Temp {
private String period;//会计期间
private String dept;//部门
private String fund;//资金用途
private String currency;//币别
public String getPeriod() {
return period;
}
public void setPeriod(String period) {
this.period = period;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getFund() {
return fund;
}
public void setFund(String fund) {
this.fund = fund;
}
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
//重写hashcode方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((period == null) ? 0 : period.hashCode());
result = prime * result + ((dept == null) ? 0 : dept.hashCode());
result = prime * result + ((fund == null) ? 0 : fund.hashCode());
result = prime * result + ((currency == null) ? 0 : currency.hashCode());
return result;
}
//重写equals方法
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Temp other = (Temp) obj;
if (period == null) {
if (other.period != null)
return false;
} else if (!period.equals(other.period))
return false;
if (dept == null) {
if (other.dept != null)
return false;
} else if (!dept.equals(other.dept))
return false;
if (fund == null) {
if (other.fund != null)
return false;
} else if (!fund.equals(other.fund))
return false;
if (currency == null) {
if (other.currency != null)
return false;
} else if (!currency.equals(other.currency))
return false;
return true;
}
}
@Override
public void method () {
List list = new ArrayList<>();
Map map = new HashMap<>();
for (DynamicObject entryItem : entry) {
Temp temp = new Temp();
temp.setPeriod(dateStr);
temp.setCurrency(currency);
temp.setDept(dept);
temp.setFund(fund);
if (map.containsKey(temp)) {
//累加实付金额
entryItem.set(KEY_PAY_SFJE,map.get(temp).getBigDecimal(KEY_PAY_SFJE).add(entryItem.getBigDecimal(KEY_PAY_SFJE)));
}
map.put(temp, entryItem);
}
for (Map.Entry entry : map.entrySet()) {
list.add(entry.getValue());
}
}
6、根据对象属性值获取对象
Optionalfirst = list.stream().filter(user -> Objects.equals(user.getId(),1)).findFirst(); //判空 if(first.isPresent()) { System.out.println(first.get()); }



