合併兩個list集合並且排序
阿新 • • 發佈:2018-12-25
集合合成的話
List<Article> list1 = new ArrayList<Article>();
/*.....新增資料.......*/
List<Article> list2 = new ArrayList<Article>();
/*.....新增資料.......*/
list1.addAll(list2);
一、被比較物件需實現Comparable介面中的compareTo方法
/** * 根據order對User排序 */ public class User implements Comparable<User>{ private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } public int compareTo(User arg0) { return this.getOrder().compareTo(arg0.getOrder()); } }
測試程式碼
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test{ public static void main(String[] args) { User user1 = new User(); user1.setName("a"); user1.setOrder(1); User user2 = new User(); user2.setName("b"); user2.setOrder(2); List <User> list = new ArrayList<User>(); //此處add user2再add user1 list.add(user2); list.add(user1); for(User u : list){ System.out.println(u.getName()); } Collections.sort(list); for(User u : list){ System.out.println(u.getName()); } } }
輸出結果
b
a
a
b
二、自定義一個比較器Comparator實現compare方法(這裡採用匿名內部類的形式)
/** * 根據order對User排序 */ public class User {//此處無需實現Comparable介面 private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } }
傳入比較器,測試程式碼
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test{
public static void main(String[] args) {
User user1 = new User();
user1.setName("a");
user1.setOrder(1);
User user2 = new User();
user2.setName("b");
user2.setOrder(2);
List<User> list = new ArrayList<User> ();
list.add(user2);
list.add(user1);
Collections.sort(list,new Comparator<User>(){
public int compare(User arg0, User arg1) {
return arg0.getOrder().compareTo(arg1.getOrder());
}
});
for(User u : list){
System.out.println(u.getName());
}
}
}
三、多屬性比較
新增一個id屬性
/**
* 多屬性排序
*/
public class User {
private Integer id;
private String name;
private Integer order;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
}
測試程式碼
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test{
public static void main(String[] args) {
User user1 = new User();
user1.setId(1);
user1.setName("a");
user1.setOrder(1);
User user2 = new User();
user2.setId(2);
user2.setName("a");
user2.setOrder(1);
List<User> list = new ArrayList<User> ();
list.add(user2);
list.add(user1);
Collections.sort(list,new Comparator<User>(){
public int compare(User arg0, User arg1) {
//第一次比較order
int i = arg0.getOrder().compareTo(arg1.getOrder());
//如果order相同則比較name
if(i==0){
int j= arg0.getName().compareTo(arg1.getName());
//如果name相同則比較id
if (j==0){
return arg0.getId().compareTo(arg1.getId());
}
return j;
}
return i;
}
});
for(User u : list){
System.out.println(u.getId());
}
}
}
參考文章: