标题比较对象简单用法
package com.devfastmybatis.comporator;
import java.util.*;
public class a {
public static void main(String[] args) {
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map = new HashMap<>();
Map<String, String> map1 = new HashMap<>();
Map<String, String> map2 = new HashMap<>();
Map<String, String> map3 = new HashMap<>();
map.put("name", "zhangsan");
map.put("sortRule", "d");
map1.put("name", "lisi");
map1.put("sortRule", "a");
map2.put("name", "wangwu");
map2.put("sortRule", "b");
map3.put("name", "wangwu");
map3.put("sortRule", "a");
list.add(map);
list.add(map1);
list.add(map2);
list.add(map3);
System.out.println("排序前:"+list);
Collections.sort(list, new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> m1, Map<String, String> m2) {
String m1Key = m1.get("sortRule");
String m2Key = m2.get("sortRule");
int i = m1Key.compareTo(m2Key);
if(i>0){
return 1;
}else if(i<0){
return -1;
}else{
return 0;
}
}
});
System.out.println("排序后:"+list);
}
}
package com.devfastmybatis.comporator;
public class GeRenInfo implements Comparable<GeRenInfo>{
private Integer sxny;//所属年月
@Override
public String toString() {
return "sxny=" + sxny ;
}
public GeRenInfo() {
super();
// TODO Auto-generated constructor stub
}
public int getSxny() {
return sxny;
}
public void setSxny(int sxny) {
this.sxny = sxny;
}
public GeRenInfo(Integer sxny) {
super();
this.sxny = sxny;
}
@Override
public int compareTo(GeRenInfo o) {
int sxny01=this.sxny;
int sxny02=o.sxny;
int i=0;
if(sxny01> sxny02){
i=1;
}else if(sxny01<sxny02){
i=-1;
}
return i;
}
}
package com.devfastmybatis.comporator;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class UnitShow {
@Test
public void unitShow() {
//比较器
Comparator comparator = new Comparator<GeRenInfo>() {
@Override
public int compare(GeRenInfo o1, GeRenInfo o2) {
int sxny01 = o1.getSxny();
int sxny02 = o2.getSxny();
int i = 0;
if (sxny01 > sxny02) {
i = 1;
} else if (sxny01 < sxny02) {
i = -1;
}
return i;
}
};
// GeRenInfo[] gryf = new GeRenInfo[]{
// new GeRenInfo(201407),
// new GeRenInfo(201410),
// new GeRenInfo(201406),
// };
List<GeRenInfo> list = new ArrayList<GeRenInfo>();
list.add(new GeRenInfo(201407));
list.add(new GeRenInfo(201410));
list.add(new GeRenInfo(201406));
Collections.sort(list, comparator);//用比较器进行排序
System.out.println(list);
}
}
2.1
package com.devfastmybatis.comporator1;
import java.util.Objects;
public class Student implements Comparable<Student>{
private int code;
private String name;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Student(int code, String name) {
super();
this.code = code;
this.name = name;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public int compareTo(Student o) {
return (this.code>o.getCode())?1:-1;
}
@Override
public String toString() {
return "Student [code=" + code + ", name=" + name + "]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return code == student.code &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(code, name);
}
// @Override
// public int compareTo(Object o) {
// return 0;
// }
}
2.2
package com.devfastmybatis.comporator1;
import java.util.*;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;
public class sortByName {
/**
* 使用自然排序
* Student必须实现Comparable接口,否则会抛出ClassCastException
*/
@Test
public void testSortedSet3() {
Student stu1 = new Student(1, "Little");
Student stu2 = new Student(2, "Cyntin");
Student stu3 = new Student(3, "Tony");
Student stu4 = new Student(4, "Gemini");
// System.out.println(Arrays.toString(new int[]{1,2,3}));
SortedSet set = new TreeSet();
// set.add(stu1);
set.add(stu3); // 若Student没有实现Comparable接口,抛出ClassCastException
set.add(stu4);
set.add(stu2);
set.add(stu4);
set.add(new Student(12, "Little"));
System.out.println(set.size());
System.out.println(set);
}
/**
* 使用比较器排序
* Student可以只是个简单的Java类,不用实现Comparable接口
*/
@Test
public void testSortedSet4() {
Student stu1 = new Student(1, "Little");
Student stu2 = new Student(2, "Cyntin");
Student stu3 = new Student(3, "Tony");
Student stu4 = new Student(4, "Gemini");
SortedSet set = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Student
&& o2 instanceof Student) {
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return s1.getName().compareTo(s2.getName());
}
return 0;
}
});
set.add(stu1);
set.add(stu3);
set.add(stu4);
set.add(stu2);
set.add(stu4);
set.add(new Student(12, "Little"));
System.out.println(set);
}
}