Guava
Guava简单的操作
private static void baseTest() {
//natural() 对可排序类型做自然排序,如数字按大小,日期按先后排序
//usingToString() 按对象的字符串形式做字典排序[lexicographical ordering]
//from(Comparator) 把给定的Comparator转化为排序器
//reverse() 获取语义相反的排序器
//nullsFirst() 使用当前排序器,但额外把null值排到最前面。
//nullsLast() 使用当前排序器,但额外把null值排到最后面。
List<Integer> numbers = Lists.newArrayList(30, 20, 60, 80, 10);
List<Integer> result = Lists.newArrayList();
result = Ordering.natural().sortedCopy(numbers);
System.out.println(result);
// 10,20,30,60,80
result = Ordering.natural().sortedCopy(numbers);
System.out.println(result);
result = Ordering.natural().reverse().sortedCopy(numbers);
// 80,60,30,20,10
Integer min = Ordering.natural().min(numbers);
// 10
Integer max = Ordering.natural().max(numbers);
// 80
Lists.newArrayList(30, 20, 60, 80, null, 10);
Ordering.natural().nullsLast().sortedCopy(numbers);
// 10,20,30,60,80,null
Ordering.natural().nullsFirst().sortedCopy(numbers);
// null,10,20,30,60,80
}
Java8 排序
package com.gb.mq;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Author: wgs
* @Date 2021/8/26 08:50
* @Classname Student
* @Description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private Integer age;
private String name;
public static void main(String[] args) {
Student student1 = new Student(10, 18, "哈哈");
Student student2 = new Student(10, 11, "嘿嘿");
Student student3 = new Student(19, 22, "哒哒");
Student student4 = new Student(6, 54, "喔喔");
Student student5 = new Student(25, 25, "嘻嘻");
List<Student> studentList = Lists.newArrayList(student1, student2, student3, student4, student5);
List<Student> students = studentList;
System.out.println("排序前");
studentList.forEach(e -> System.out.println(JSONObject.toJSONString(e)));
// 正序
studentList = studentList.stream().sorted(Comparator.comparing(Student::getId)).collect(Collectors.toList());
System.out.println("-------");
studentList.forEach(e -> System.out.println(JSONObject.toJSONString(e)));
// 倒序
studentList = studentList.stream().sorted(Comparator.comparing(Student::getId).reversed()).collect(Collectors.toList());
System.out.println("~~~~~~");
studentList.forEach(e -> System.out.println(JSONObject.toJSONString(e)));
// 根据id倒序排,id相同根据年龄正序排
System.out.println("========排序输出前========");
students.forEach(e -> System.out.println(JSONObject.toJSONString(e)));
List<Student> studentsSortAge = students
.stream()
.sorted(Comparator.comparing(Student::getId)
.reversed()
.thenComparing(Student::getAge))
.collect(Collectors.toList());
System.out.println("========排序输出后========");
studentsSortAge.forEach(e -> System.out.println(JSONObject.toJSONString(e)));
}
}
四中方式实现根据第一个集合对第二个排序
/**
* 默认的排序器
* natural() 对可排序类型做自然排序,如数字按大小,日期按先后排序
* reverse() 对当前的比较器进行反转
* 自然排序,不能将汉字按首字母的顺序排序。
*/
private static void naturalTest() {
List<String> list = Lists.newArrayList("12", "2", "3", "33", "4", "44", "5", "55", "测试", "基线", "有钱");
Ordering<Comparable> natural = Ordering.natural();
list.sort(natural);
System.out.println(list.toString());
//反转
list.sort(natural.reverse());
System.out.println(list.toString());
List<Integer> ints = Lists.newArrayList(12, 2, 3, 33, 4, 44, 5, 55);
ints.sort(natural);
System.out.println(ints.toString());
ints.sort(natural.reverse());
System.out.println(ints.toString());
}
/**
* 根据指定顺序排序
*/
private static void javaSpecifyTest() {
List<String> orders = Stream.of("东部", "南部", "A", "西部", "北部", "中部").
collect(Collectors.toList());
List<String> source = Stream.of("北部", "南部", "西部", "东部", "中部", "A").
collect(Collectors.toList());
Collections.reverse(orders);
source.sort((o1, o2) -> Integer.compare(orders.indexOf(o2), orders.indexOf(o1)));
System.out.println(source);
Map<Integer, String> tree = new TreeMap<Integer, String>();
for (String code : source) {
if (orders.contains(code)) {
tree.put(orders.indexOf(code), code);
} else {
tree.put(source.size(), code);
}
}
}
/**
* 原理,就是获取了字符在排序集合中的位置,然后借助TreeMap实现排序。
*/
private static void javaSpecifyTreeTest() {
List<String> orders = Stream.of("东部", "南部", "A", "西部", "北部", "中部").
collect(Collectors.toList());
List<String> source = Stream.of("北部", "南部", "西部", "东部", "中部", "A").
collect(Collectors.toList());
Map<Integer, String> tree = new TreeMap<Integer, String>();
for (String code : source) {
if (orders.contains(code)) {
tree.put(orders.indexOf(code), code);
} else {
tree.put(source.size(), code);
}
}
System.out.println(source);
}
private static void guavaSpecifyTest() {
List<String> orders = Stream.of("东部", "南部", "A", "西部", "北部", "中部").
collect(Collectors.toList());
List<String> source = Stream.of("北部", "南部", "西部", "东部", "中部", "A").
collect(Collectors.toList());
Ordering ordering = Ordering.explicit(orders);
source.sort((o1, o2) -> {
if (orders.contains(o1) && orders.contains(o2)) {
return ordering.compare(o1, o2);
}
return 0;
});
System.out.println(source);
}
/**
* 不仅只是指定顺序,还可以设置指定缺少的字符 的排序。
*/
private static void guavaSpecifyComparatorTest() {
List<String> orders = Stream.of("东部", "南部", "A", "西部", "北部", "中部").
collect(Collectors.toList());
List<String> source = Stream.of("北部", "南部", "西部", "东部", "中部", "A").
collect(Collectors.toList());
FixedOrderComparator<String> fixedOrderComparator = new FixedOrderComparator<String>(orders);
fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.AFTER);
source.sort(fixedOrderComparator);
System.out.println(source);
}
对象操作
public static void guavaSort(){
List<String> keys = ImmutableList.of("A", "Z", "D", "E", "B", "H", "C");
List<Map<String, String>> items = ImmutableList.of(
ImmutableMap.of("id", "D", "num", "5"),
ImmutableMap.of("id", "A", "num", "5"),
ImmutableMap.of("id", "C", "num", "3"),
ImmutableMap.of("id", "Z", "num", "3")
);
Ordering<Map<String, String>> numOrdering =
Ordering.natural().reverse().onResultOf(item -> item.get("num"));
System.out.println(JSONObject.toJSONString(numOrdering.sortedCopy(items)));
Ordering<Map<String, String>> idOrdering = Ordering.explicit(keys).onResultOf(item -> item.get("id"));
Ordering<Map<String, String>> compoundOrdering = numOrdering.compound(idOrdering);
System.out.println(JSONObject.toJSONString(compoundOrdering.sortedCopy(items)));
List<Map<String, String>> result = compoundOrdering.sortedCopy(items);
System.out.println(result);
}
两个集合根据规则排序
package com.gb.mq;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @Author: wgs
* @Date 2021/8/26 08:50
* @Classname Student
* @Description
* https://stackoverflow.com/questions/45349293/sorting-list-based-on-another-lists-order
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentS {
private Integer id;
private Integer age;
private String name;
public static void main(String[] args) {
StudentS student1 = new StudentS(10, 18, "哈哈");
StudentS student2 = new StudentS(10, 11, "嘿嘿");
StudentS student3 = new StudentS(19, 22, "哒哒");
StudentS student4 = new StudentS(6, 54, "喔喔");
StudentS student5 = new StudentS(25, 25, "嘻嘻");
List<StudentS> studentList = Lists.newArrayList(student1, student2, student3, student4, student5);
List<StudentS> students = Lists.newArrayList(student5, student1, student3, student4, student2);
List<Integer> ids = Lists.newArrayList(11, 18, 25, 22, 54);
Ordering<StudentS> studentSOrdering = Ordering.natural().reverse().onResultOf(StudentS::getAge);
Ordering<StudentS> ageOrdering = Ordering.explicit(ids).onResultOf(StudentS::getAge);
List<StudentS> compoundOrdering = ageOrdering.sortedCopy(studentList);
compoundOrdering.forEach(System.out::println);
// 方式二
// FixedOrderComparator<StudentS> fixedOrderComparator = new FixedOrderComparator<StudentS>(students);
//
//
// fixedOrderComparator.setUnknownObjectBehavior(FixedOrderComparator.UnknownObjectBehavior.AFTER);
// studentList.sort(fixedOrderComparator);
//
// students.forEach(e -> System.out.println(JSONObject.toJSONString(e)));
// System.out.println("---------");
// studentList.forEach(e -> System.out.println(JSONObject.toJSONString(e)));
//
}
}