Java-根据atribute在arraylist中排序
您好,我有一个arraylist,让我们称它为“callcenterList”,在该arraylist中,我将添加“call”类的对象 “呼叫”对象具有不同的属性,其中一些属性是“状态”、“月份”和“日期”(收到呼叫的月份和日期及其状态) 状态可以是“已完成”或“待定” 因此,“调用”对象类似于:Java-根据atribute在arraylist中排序,java,sorting,arraylist,Java,Sorting,Arraylist,您好,我有一个arraylist,让我们称它为“callcenterList”,在该arraylist中,我将添加“call”类的对象 “呼叫”对象具有不同的属性,其中一些属性是“状态”、“月份”和“日期”(收到呼叫的月份和日期及其状态) 状态可以是“已完成”或“待定” 因此,“调用”对象类似于: call1: Atribute1 = whatever. Atribute2 = whatever. Month = 3 Day = 25 Status = Pending 下面是我被要求做的: 我
call1:
Atribute1 = whatever.
Atribute2 = whatever.
Month = 3
Day = 25
Status = Pending
下面是我被要求做的:
我需要显示所有状态为挂起并按日期排序的呼叫。
我该怎么做?如果您需要更多信息或更详细的信息,请告诉我。
谢谢 您可以使用
Comparator
类执行此操作,该类的compare(调用c1,调用c2)
方法比较两个参数的相关属性,然后根据比较结果返回1、0或-1。然后可以使用Collections.sort(myList,myComparator)
根据比较器对集合进行排序
public class Call {
private int value1;
private int value2;
public Call(int value1, int value2) {
this.value1 = value1;
this.value2 = value2;
}
public int getValue1() {
return value1;
}
public void setValue1(int value1) {
this.value1 = value1;
}
public int getValue2() {
return value2;
}
public void setValue2(int value2) {
this.value2 = value2;
}
}
公共类MyComparator实现Comparator{
@凌驾
公共整数比较(调用c1,调用c2){
int firstCompare=Integer.compare(c1.getValue1(),c2.getValue1());
if(firstCompare!=0){
返回第一比较;
}否则{
返回整数.compare(c1.getValue2(),c2.getValue2());
}
}
}
在ArrayList上调用sort(new CallComparator()),并向其传递一个。然后将根据比较器给出的orering对列表进行排序。如果调用#14发生在调用#7之前,则新的CallComparator().compare(call14,call7)将返回小于0的值
因此,对于您提供给我们的数据,比较器可以这样实现:
public class CallComparator implements Comparator<Call> {
@Override
public int compare(Call c1, Call c2) {
if (c1.getMonth() == c2.getMonth()
return c1.getDay() - c2.getDay();
else
return c1.getMonth() - c2.getMonth();
}
}
这将实现同样的效果。听起来你想过滤结果,而不是排序结果。虽然您可以通过首先对其进行排序,然后使用
迭代器来删除您不关心的元素来完成此操作,但与仅对其进行过滤相比,这需要做更多的工作
请注意,如果这是来自数据库,则在数据库端对其进行过滤。根据数据量的不同,在后端执行此类操作的成本要高于在服务器上执行此类操作的成本
使用,这很简单
- 使用创建一个iterable,然后可以将操作链接到该iterable
- 创建仅接受符合您所关心的参数的项的
- 按照您希望的顺序,使用。请记住,这不同于“可比较的”
,在“可比较的””中,您可以标记特定对象<代码>比较器允许您随意指定不同于自然排序的新排序
这里有一个例子。假设Call
有方法getStatus()
和getDate()
,并且getDate()
是可比的
(java.util.Date
类是)
列表调用=。。。
final List filteredCalls=fluenterable.from(calls).filter(new Predicate()){
@凌驾
公共布尔应用(最终调用输入){
返回“pending”.equals(input.getStatus());
}
}).toSortedList(新比较器(){
@凌驾
public int compare(final Call firstCall,final Call secondCall){
返回firstCall.getDate().compareTo(secondCall.getDate());
}
});
在Java8中,这个表达式变得更加简洁,并且不需要使用第三方库
final List<Call> filteredCalls = calls.stream()
.filter(c -> "pending".equals(c.getStatus()))
.sorted((left, right) -> left.getDate().compareTo(right.getDate()))
.collect(Collectors.toList());
final List filteredCalls=calls.stream()
.filter(c->“挂起”.equals(c.getStatus()))
.sorted((左,右)->left.getDate().compareTo(right.getDate()))
.collect(Collectors.toList());
仅存储通话的月份和日期(不包括年份)会使按日期订购变得不明确。简单地考虑使用一个代码>日期>代码>变量。尝试<代码>比较器< /代码>接口。创建一个实现比较器的新类。然后编写一个方法,将您的对象与您喜欢的方法进行比较。然后调用Collections.sort(yourList,yourComparator)代码>它看起来像来自数据库的数据,如果是这种情况,那么让数据库完成这项工作(纯排序和筛选时间)=>SQL将是最简单、最有效的方法。
return c1.getDate().compareTo(c2.getDate());
List<Call> calls = ...
final List<Call> filteredCalls = FluentIterable.from(calls).filter(new Predicate<Call>() {
@Override
public boolean apply(final Call input) {
return "pending".equals(input.getStatus());
}
}).toSortedList(new Comparator<Call>() {
@Override
public int compare(final Call firstCall, final Call secondCall) {
return firstCall.getDate().compareTo(secondCall.getDate());
}
});
final List<Call> filteredCalls = calls.stream()
.filter(c -> "pending".equals(c.getStatus()))
.sorted((left, right) -> left.getDate().compareTo(right.getDate()))
.collect(Collectors.toList());