Java8使⽤lambda对List、Set进⾏排序
数据结构
public class InsurerClientDto {
private Long insurerClientId;
private String clientOidType;
private Long clientOid;
private String clientCode;
private String clientDesc;
private Long hqHdrId;
public InsurerClientDto() {
//Default constructor
}
public void setInsurerClientId(Long insurerClientId) {
this.insurerClientId = insurerClientId;
}
public Long getInsurerClientId() {
return insurerClientId;
sortedlist}
public void setClientOidType(String clientOidType) {
this.clientOidType = clientOidType;
}
public String getClientOidType() {
return clientOidType;
}
public void setClientOid(Long clientOid) {
this.clientOid = clientOid;
}
public Long getClientOid() {
return clientOid;
}
public String getClientCode() {
return clientCode;
}
public void setClientCode(String clientCode) {
this.clientCode = clientCode;
}
public void setClientDesc(String clientDesc) {
this.clientDesc = clientDesc;
}
public String getClientDesc() {
return clientDesc;
}
public Long getHqHdrId() { return hqHdrId; }
public void setHqHdrId(Long hqHdrId) { this.hqHdrId = hqHdrId; }
@Override
public String toString() {
return "InsurerClientDto [insurerClientId=" + insurerClientId + ", clientOidType=" + clientOidType
+ ", clientOid=" + clientOid + ", clientCode=" + clientCode + ", clientDesc=" + clientDesc
+ ", hqHdrId=" + hqHdrId + "]";
}
}
数据准备
List<InsurerClientDto> insurerClientDto = new ArrayList<>();
InsurerClientDto dto = new InsurerClientDto();
dto.setClientCode("KP03S");
insurerClientDto.add(dto);
dto = new InsurerClientDto();
dto.setClientCode("KP048");
insurerClientDto.add(dto);
dto = new InsurerClientDto();
dto.setClientCode("KP003");
insurerClientDto.add(dto);
3种⽅法
1、不能实现排序
Comparator<InsurerClientDto> clinentComparator = (v1, v2) -> v1.getClientCode()ClientCode()); insurerClientDto.stream().versed());
不能实现排序原因:
2、可以实现排序
Comparator<InsurerClientDto> clinentComparator = Comparatorparing(InsurerClientDto :: getClientCode, String.CASE_INSENSITIVE_ORDER);
insurerClientDto.sort(clinentComparator);
3、可以实现排序
insurerClientDto.sort((d1, d2) -> d1.getClientCode()ClientCode()));
//或(⽤法相同,上⾯省略类名⽽已)
insurerClientDto.sort((InsurerClientDto d1, InsurerClientDto d2) -> d1.getClientCode()ClientCode()));
原⽂链接:
参考链接:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。