数据结构
public classInsurerClientDto {privateLong insurerClientId;privateString clientOidType;privateLong clientOid;privateString clientCode;privateString clientDesc;privateLong hqHdrId;publicInsurerClientDto() {//Default constructor
}public voidsetInsurerClientId(Long insurerClientId) {this.insurerClientId =insurerClientId;
}publicLong getInsurerClientId() {returninsurerClientId;
}public voidsetClientOidType(String clientOidType) {this.clientOidType =clientOidType;
}publicString getClientOidType() {returnclientOidType;
}public voidsetClientOid(Long clientOid) {this.clientOid =clientOid;
}publicLong getClientOid() {returnclientOid;
}publicString getClientCode() {returnclientCode;
}public voidsetClientCode(String clientCode) {this.clientCode =clientCode;
}public voidsetClientDesc(String clientDesc) {this.clientDesc =clientDesc;
}publicString getClientDesc() {returnclientDesc;
}public Long getHqHdrId() { returnhqHdrId; }public void setHqHdrId(Long hqHdrId) { this.hqHdrId =hqHdrId; }
@OverridepublicString toString() {return "InsurerClientDto [insurerClientId=" + insurerClientId + ", clientOidType=" +clientOidType+ ", clientOid=" + clientOid + ", clientCode=" + clientCode + ", clientDesc=" +clientDesc+ ", hqHdrId=" + hqHdrId + "]";
}
}
数据准备
List insurerClientDto = new ArrayList<>();
InsurerClientDto dto= newInsurerClientDto();
dto.setClientCode("KP03S");
insurerClientDto.add(dto);
dto= newInsurerClientDto();
dto.setClientCode("KP048");
insurerClientDto.add(dto);
dto= newInsurerClientDto();
dto.setClientCode("KP003");
insurerClientDto.add(dto);
3种方法
1、不能实现排序
Comparator clinentComparator = (v1, v2) ->v1.getClientCode().compareTo(v2.getClientCode());
insurerClientDto.stream().sorted(clinentComparator.reversed());
不能实现排序原因:
2、可以实现排序
Comparator clinentComparator = Comparator.comparing(InsurerClientDto :: getClientCode, String.CASE_INSENSITIVE_ORDER);
insurerClientDto.sort(clinentComparator);
3、可以实现排序
insurerClientDto.sort((d1, d2) -> d1.getClientCode().compareTo(d2.getClientCode()));
//或(用法相同,上面省略类名而已)
insurerClientDto.sort((InsurerClientDto d1, InsurerClientDto d2) -> d1.getClientCode().compareTo(d2.getClientCode()));
原文链接:
参考链接: