根据Java集合对象中的一个字段对其排序
我有以下收藏:根据Java集合对象中的一个字段对其排序,java,sorting,collections,Java,Sorting,Collections,我有以下收藏: Collection<AgentSummaryDTO> agentDtoList = new ArrayList<AgentSummaryDTO>(); 现在,我必须根据customerCount字段对集合agentdolist进行排序,如何实现这一点?查看和类 一种简单的方法是在agentsmarydto中实现接口,然后将列表传递给Collections.sort() 如果您无法将代理汇总数据编辑为,则需要一个比较器,如下所示:查看下面的代码 pack
Collection<AgentSummaryDTO> agentDtoList = new ArrayList<AgentSummaryDTO>();
现在,我必须根据customerCount
字段对集合agentdolist
进行排序,如何实现这一点?查看和类
一种简单的方法是在agentsmarydto
中实现接口,然后将列表传递给Collections.sort()
如果您无法将
代理汇总数据编辑为,则需要一个比较器,如下所示:查看下面的代码
package test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class AgentSummary {
private Long id;
private String agentName;
private String agentCode;
private String status;
private Date createdDate;
private Integer customerCount;
/**
* @param args
*/
public static void main(String[] args) {
new AgentSummary().addObjects();
}
public void addObjects(){
List<AgentSummaryDTO> agentSummary = new ArrayList<AgentSummaryDTO>();
for (int j = 0; j < 10; j++) {
agentSummary.add(new AgentSummaryDTO(j));
}
Collections.sort(agentSummary);
for (AgentSummaryDTO obj : agentSummary) {
System.out.println("File " + obj.getCustomerCount());
}
}
}
class AgentSummaryDTO implements Serializable, Comparable<AgentSummaryDTO> {
private Long id;
private String agentName;
private String agentCode;
private String status;
private Date createdDate;
private Integer customerCount;
AgentSummaryDTO() {
customerCount = null;
}
AgentSummaryDTO(int customerCount) {
this.customerCount = customerCount;
}
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id
* the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the agentName
*/
public String getAgentName() {
return agentName;
}
/**
* @param agentName
* the agentName to set
*/
public void setAgentName(String agentName) {
this.agentName = agentName;
}
/**
* @return the agentCode
*/
public String getAgentCode() {
return agentCode;
}
/**
* @param agentCode
* the agentCode to set
*/
public void setAgentCode(String agentCode) {
this.agentCode = agentCode;
}
/**
* @return the status
*/
public String getStatus() {
return status;
}
/**
* @param status
* the status to set
*/
public void setStatus(String status) {
this.status = status;
}
/**
* @return the createdDate
*/
public Date getCreatedDate() {
return createdDate;
}
/**
* @param createdDate
* the createdDate to set
*/
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
/**
* @return the customerCount
*/
public Integer getCustomerCount() {
return customerCount;
}
/**
* @param customerCount
* the customerCount to set
*/
public void setCustomerCount(Integer customerCount) {
this.customerCount = customerCount;
}
@Override
public int compareTo(AgentSummaryDTO arg0) {
if (this.customerCount > arg0.customerCount)
return 0;
else
return 1;
}
}
封装测试;
导入java.io.Serializable;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Date;
导入java.util.List;
公共类代理{
私人长id;
私有字符串代理名;
私有字符串代理代码;
私有字符串状态;
私人日期创建日期;
私有整数客户计数;
/**
*@param args
*/
公共静态void main(字符串[]args){
新建AgentCommand().addObjects();
}
public void addObjects(){
List AgentCommand=new ArrayList();
对于(int j=0;j<10;j++){
agentSummary.添加(新的AgentSummaryDTO(j));
}
集合。排序(代理汇总);
对于(代理汇总到obj:代理汇总){
System.out.println(“文件”+obj.getCustomerCount());
}
}
}
类AgentCommand实现可序列化、可比较的{
私人长id;
私有字符串代理名;
私有字符串代理代码;
私有字符串状态;
私人日期创建日期;
私有整数客户计数;
AgentSummaryDTO(){
customerCount=null;
}
代理汇总(国际客户账户){
this.customerCount=customerCount;
}
/**
*@返回id
*/
公共长getId(){
返回id;
}
/**
*@param-id
*要设置的id
*/
公共无效集合id(长id){
this.id=id;
}
/**
*@返回代理名称
*/
公共字符串getAgentName(){
退货代理名称;
}
/**
*@param-agentName
*要设置的代理名称
*/
public void setAgentName(字符串agentName){
this.agentName=agentName;
}
/**
*@返回代理代码
*/
公共字符串getAgentCode(){
返回代理代码;
}
/**
*@param-agentCode
*要设置的代理代码
*/
public void setAgentCode(字符串agentCode){
this.agentCode=agentCode;
}
/**
*@返回状态
*/
公共字符串getStatus(){
返回状态;
}
/**
*@param状态
*要设置的状态
*/
公共无效设置状态(字符串状态){
这个状态=状态;
}
/**
*@returnthecreateddate
*/
公共日期getCreatedDate(){
返回createdDate;
}
/**
*@param createdDate
*要设置的createdDate
*/
public void setCreatedDate(日期createdDate){
this.createdDate=createdDate;
}
/**
*@返回客户计数
*/
公共整数getCustomerCount(){
返回客户计数;
}
/**
*@param customerCount
*要设置的客户计数
*/
public void setCustomerCount(整数customerCount){
this.customerCount=customerCount;
}
@凌驾
公共int比较(代理汇总到arg0){
如果(this.customerCount>arg0.customerCount)
返回0;
其他的
返回1;
}
}
这是我的“1行”:
甚至:
Collections.sort(agentDtoList, Comparator.comparing(AgentSummaryDTO::getCustomerCount));
对于字符串数据类型(如注释中所示)
…它希望getterAgentSummaryDTO.getCustomerCount()
可以进一步简化,这实际上是完整的Java 8实现方法:
Collections.sort(agentDtoList, Comparator.comparing(AgentSummaryDTO::getCustomerCount));
这只是通过整数字段进行比较,效果很好,因为integer
实现了Comparable
更干净的解决方案可能是使用内置方法:
当然,通过静态导入sort
和comparingit
,这一点可以表达得更短:
sort(agentDtoList, comparingInt(AgentSummaryDTO::getCustomerCount));
Java8的更新。它的工作原理是:
Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount());
对于那些还没有找到答案的人:
您还可以使用JAVA-8流API对列表进行排序
List<AgentSummaryDTO> sortedList = agentDtoList.stream()
.sorted(Comparator.comparing(AgentSummaryDTO::getCustomerCount).reversed())
.collect(Collectors.toList());
List sortedList=agentToList.stream()
.sorted(Comparator.comparing(AgentSummaryDTO::getCustomerCount).reversed())
.collect(Collectors.toList());
如果您使用这些标记在javadoc中搜索,您可能会遇到以下问题:,java.util.Comparator)我使用返回代理mmary1.getCustomerCount().compareTo(代理mmary2.getCustomerCount());如果我需要使用agentname而不是customercount对同一个示例进行排序,该怎么办?我需要在字符串字段上排序。请suggest@TanuGarg而不是返回o1.getCustomerCount()-o2.getCustomerCount()
,根据的结果,您将拥有o1.getAgentName().compareTo(o2.getAgentName())
。compareTo
可以返回0
(如果比较的字符串相同),如果第一个字符串较大则返回负值,否则返回正值。它对字符串使用字典顺序。然后,Java中的排序算法(恰好是Merge sort)@karthik_varma_kCollections.sort(AgentToList,Comparator.comparing(AgentSmaryDTO::getCustomerCount).reversed()使用该结果代码>
Collections.sort(agentDtoList, Comparator.comparing(AgentSummaryDTO::getCustomerCount));
Collections.sort(agentDtoList, Comparator.comparingInt(AgentSummaryDTO::getCustomerCount));
sort(agentDtoList, comparingInt(AgentSummaryDTO::getCustomerCount));
Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount());
List<AgentSummaryDTO> sortedList = agentDtoList.stream()
.sorted(Comparator.comparing(AgentSummaryDTO::getCustomerCount).reversed())
.collect(Collectors.toList());