[英]How to sort an arraylist of objects?
我上課了
public class SMS
{
public String addr;
public String body;
public String type;
public String timestamp;
}
現在我已經創建了一個對象的數組列表
ArrayList<SMS> temp = new ArrayList<SMS>();
我添加了值。 現在我想根據時間戳對arrayList進行排序。
如何根據時間戳以升序/降序對arrayList進行排序?
Collections.sort(temp);
將對Comparable
對象的集合進行排序,因此類SMS
必須實現Comparable<SMS>
:
public class SMS implementes Comparable<SMS>
{
public String addr;
public String body;
public String type;
public String timestamp;
@Override
public int compareTo(SMS other) {
//for instance
return addr.compareTo( other.addr );
}
}
這通常是一種很好的做法,當實現Comparable
時也實現equals()
和hashcode()
以便對象之間的相等性與它們的比較一致。 此外,您應該添加一些針對空值的保護:
public class SMS implements Comparable<SMS>
{
public String addr;
public String body;
public String type;
public String timestamp;
@Override
public int compareTo(SMS other) {
//for instance
if( addr == null ) {
return Integer.MAX_VALUE;
}
return addr.compareTo( other.addr );
}
@Override
public boolean equals(Object other) {
//for instance
if( other instanceof SMS ) {
if( addr == null && ((SMS) other) != null ) {
return false;
}
return addr.equals(((SMS) other).addr);
} else {
return false;
}
return addr.compareTo( other.addr );
}
@Override
public int hashcode() {
//for instance
if( addr == null ) {
return 0;
}
return addr.hashcode();
}
}
要比較包含時間戳的字符串,您需要先將它們解析為long Long.parseLong(timestamp)
,然后使用Long.compare(x,y)
比較數值。
所以嘗試使用Collections.sort(yorList, yourOwnComparator)
就好了
Collections.sort(temp, new Comparator<SMS>() {
@Override
public int compare(SMS o1, SMS o2) {
return Long.compare(Long.parseLong(o1.timestamp),
Long.parseLong(o2.timestamp));
}
});
如果您可以將時間戳的類型更改為long
此代碼可能看起來像
Collections.sort(temp, new Comparator<SMS>() {
@Override
public int compare(SMS o1, SMS o2) {
return Long.compare(o1.timestamp, o2.timestamp);
}
});
在Java8中,您甚至可以使用lambdas來進一步縮短代碼
Collections.sort(temp, (SMS o1, SMS o2) -> Long.compare(o1.timestamp, o2.timestamp));
甚至
Collections.sort(temp, (o1, o2) -> Long.compare(o1.timestamp, o2.timestamp));
使用Comparable
(參見此處的示例 ),它應該是這樣的:
public class SMS implements Comparable<SMS>{
....
public int compareTo(SMS compareSms) {
return this.timestamp.compareTo(compareSms.timestamp);
}
}
這是一般測試代碼:
ArrayList<SMS> temp = new ArrayList<SMS>();
// testing values
SMS a = new SMS();
a.setTime("a time");
SMS b = new SMS();
b.setTime("b time");
SMS c = new SMS();
c.setTime("c time");
temp.add(a);
temp.add(b);
temp.add(c);
// descending sort
Collections.sort(temp, new Comparator<SMS>() {
public int compare(SMS text1, SMS text2) {
// compare timestamps from the SMSs
return text2.getTime().compareTo(text1.getTime());
}
});
// print it out to test
for (SMS sms : temp) {
System.out.println(sms.getTime());
}
如果我想將它從升序改為降序,我只需要切換比較文本消息的方式。 看這個:
return text2.getTime().compareTo(text1.getTime()); // descending
return text1.getTime().compareTo(text2.getTime()); // ascending
注意:我假設getTime()
返回文本消息的時間戳,您需要將此import語句添加到您的類: import java.util.Collections;
或者只是import java.util.*;
要使用getTime()
和setTime(String timestamp)
方法,您的類可能如下所示:
public class SMS
{
public String addr;
public String body;
public String type;
public String timestamp;
public void setTime(String timestamp) {
this.timestamp = timestamp;
}
public String getTime() {
return timestamp;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.