Java中Collections.sort排序用法
第一种是list中的对象实现Comparable接口
/**
* 根据年龄对User进行排序
*/
public class User implements Comparable<User>{
private Long id;
private String name;
private Integer age;
public User() {
}
public User(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public int compareTo(User arg0) {
//排序条件
return this.getAge().compareTo(arg0.getAge());
}
}
测试1
public class Test{
public static void main(String[] args) {
User yxs1 = new User((long) 1, "yxs1", 21);
User yxs2 = new User((long) 2, "yxs2", 22);
User yxs3 = new User((long) 3, "yxs3", 23);
User yxs4 = new User((long) 4, "yxs4", 24);
User yxs5 = new User((long) 5, "yxs5", 25);
User yxs6 = new User((long) 6, "yxs6", 26);
List<User> list = new ArrayList<User>();
list.add(yxs1);
list.add(yxs2);
list.add(yxs5);
list.add(yxs3);
list.add(yxs4);
list.add(yxs6);
Collections.sort(list);
for(User u : list){
System.out.println(u.getName());
}
}
}
第二种方法是根据Collections.sort重载方法来实现
public class User { //此处无需实现Comparable接口
private Long id;
private String name;
private Integer age;
public User() {
}
public User(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
主类中这样写即可:
public class Test{
public static void main(String[] args) {
User yxs1 = new User((long) 1, "yxs1", 21);
User yxs2 = new User((long) 2, "yxs2", 22);
User yxs3 = new User((long) 3, "yxs3", 23);
User yxs4 = new User((long) 4, "yxs4", 24);
User yxs5 = new User((long) 5, "yxs5", 25);
User yxs6 = new User((long) 6, "yxs6", 26);
List<User> list = new ArrayList<User>();
list.add(yxs1);
list.add(yxs2);
list.add(yxs5);
list.add(yxs3);
list.add(yxs4);
list.add(yxs6);
Collections.sort(list, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
/**
* 升序排的话就是第一个参数.compareTo(第二个参数);
* 降序排的话就是第二个参数.compareTo(第一个参数);
*/
return o1.getAge().compareTo(o2.getAge());
}
});
for (User user : list) {
System.out.println(user);
//System.out.println(user.getName());
}
System.out.println(list);
}
}
附加Map集合遍历方法:
//遍历map集合并排序的方法
public static Object mapSort(Map<String,String> resultMap){
Set<Map.Entry<String, String>> entries = resultMap.entrySet();
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(entries);
LinkedHashMap<String, Long> result = new LinkedHashMap<String, Long>(list.size());
long total = 0;
try {
Collections.sort(list, new Comparator<Map.Entry<String,String>>(){
public int compare(Map.Entry<String,String> map1,Map.Entry<String,String> map2){
//降序排列
//return Long.valueOf(map2.getValue()).compareTo(Long.valueOf(map1.getValue()));
//升序排列
return Long.valueOf(map1.getValue()).compareTo(Long.valueOf(map2.getValue()));
}
});
for(Map.Entry<String,String> entry : list){
result.put(entry.getKey(), Long.valueOf(entry.getValue()));
total= total + Long.valueOf(entry.getValue());
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}