根据Java集合对象中的一个字段对其进行排序

我有以下集合:

Collection agentDtoList = new ArrayList(); 

AgentSummaryDTO如下:

 public class AgentSummaryDTO implements Serializable { private Long id; private String agentName; private String agentCode; private String status; private Date createdDate; private Integer customerCount; } 

现在我必须根据customerCount字段对集合agentDtoList进行排序,如何实现呢?

这是我的“1liner”:

 Collections.sort(agentDtoList, new Comparator(){ public int compare(AgentSummaryDTO o1, AgentSummaryDTO o2){ return o1.getCustomerCount() - o2.getCustomerCount(); } }); 

Java 8的UPDATE:对于int数据类型

  Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount()); 

对于String数据类型(如注释中所示)

 Collections.sort(list, (o1, o2) -> (o1.getAgentName().compareTo(o2.getAgentName()))); 

AgentSummaryDTO.getCustomerCount()期待getter AgentSummaryDTO.getCustomerCount()

看看Comparator和Collections类。

一种简单的方法是在AgentSummaryDTO实现Comparable接口,然后将列表传递给Collections.sort()

如果您无法编辑AgentSummaryDTO ,则需要一个Comparator,如下所示: 如何使用Object name字段按字母顺序对List 进行排序

Jiri Kremser的答案可以进一步简化,这实际上是完整的Java 8方法:

 Collections.sort(agentDtoList, Comparator.comparing(AgentSummaryDTO::getCustomerCount)); 

这只是通过整数字段进行比较,并且因为Integer实现了Comparable ,所以效果很好。

更清晰的解决方案可能是使用内置的comparingInt()方法:

 Collections.sort(agentDtoList, Comparator.comparingInt(AgentSummaryDTO::getCustomerCount)); 

当然,通过静态导入sort和comparisonInt可以表达更短的内容:

 sort(agentDtoList, comparingInt(AgentSummaryDTO::getCustomerCount)); 

看看下面的代码。

 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 agentSummary = new ArrayList(); 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 { 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 8它的工作

 Collections.sort(agentDtoList, (o1, o2) -> o1.getCustomerCount() - o2.getCustomerCount());