一个List列表数据按照另外一个List列表顺序进行重排序
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.google.common.collect.Multimap;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import org.apache.commons.collections4.CollectionUtils;
public final class ListSortUtils {
private ListSortUtils() {
super();
}
public static List sortByList(List sortFieldList,
List dataList,
Function function) {
if (CollectionUtils.isEmpty(sortFieldList) || CollectionUtils.isEmpty(dataList)) {
return dataList;
}
int maxSize = sortFieldList.size() + dataList.size() + 1;
Map sortIndexMap = new HashMap<>();
for (int sortField : sortFieldList) {
sortIndexMap.put(sortField, maxSize--);
}
Map resultMap = new TreeMap<>(Comparator.reverseOrder());
for (T data : dataList) {
Integer fieldName = function.apply(data);
int index = sortIndexMap.getOrDefault(fieldName, 0);
if (index > 0) {
resultMap.put(index, data);
} else {
resultMap.put(maxSize--, data);
}
}
return resultMap.values().stream().collect(Collectors.toList());
}
// 支持dataList数据重复
public static List sortByMultiList(List sortFieldList,
List dataList,
Function function) {
if (CollectionUtils.isEmpty(sortFieldList) || CollectionUtils.isEmpty(dataList)) {
return dataList;
}
int maxSize = sortFieldList.size() + dataList.size() + 1;
Map sortIndexMap = new HashMap<>();
for (int sortField : sortFieldList) {
sortIndexMap.put(sortField, maxSize--);
}
Multimap resultMap = TreeMultimap.create(Ordering.natural().reversed(), Ordering.usingToString());
for (T data : dataList) {
Integer fieldName = function.apply(data);
int index = sortIndexMap.getOrDefault(fieldName, 0);
if (index > 0) {
resultMap.put(index, data);
} else {
resultMap.put(maxSize--, data);
}
}
return resultMap.values().stream().collect(Collectors.toList());
}
public static void swap(List list, int index1, int index2) {
if (CollectionUtils.isEmpty(list)) {
return;
}
if (index1 >= list.size() || index2 >= list.size()) {
return;
}
T data = list.get(index1);
list.set(index1, list.get(index2));
list.set(index2, data);
}
public static void move(List list, T data, int index) {
if (CollectionUtils.isEmpty(list)) {
return;
}
boolean isRemove = list.remove(data);
if (!isRemove) {
return;
}
list.add(index, data);
}
/**
* 集合按照groupNum个数,集合内的function值尽量不重复
*/
public static List groupDistinctSortList(int groupNum,
List dataList,
Function function) {
List sortList = new ArrayList<>();
while (true) {
List groupList = new ArrayList<>();
Set groupIds = new HashSet<>();
for (T data : dataList) {
if (groupList.size() >= groupNum) {
break;
}
Integer groupId = function.apply(data);
if (groupIds.contains(groupId)) {
continue;
}
groupList.add(data);
groupIds.add(groupId);
}
if (groupList.size() < groupNum) {
break;
}
sortList.addAll(groupList);
dataList.removeAll(groupList);
}
sortList.addAll(dataList);
return sortList;
}
}
使用示例
import java.util.ArrayList;
import java.util.List;
import cn.com.autohome.tuan.framework.utils.ListSortUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public class TestSort {
static class Article {
private int id;
private String title;
private String content;
public Article(int id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Article article = (Article) o;
return new EqualsBuilder()
.append(id, article.id)
.append(title, article.title)
.append(content, article.content)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.append(id)
.append(title)
.append(content)
.toHashCode();
}
@Override
public String toString() {
return String.valueOf(id);
}
}
public static void main(String[] args) {
// 文章默认的顺序是1,2,3,4,5
List articleList = new ArrayList<>();
Article article1 = new Article(1, "XXX1", "XXXXXXX1");
articleList.add(article1);
Article article2 = new Article(2, "XXX2", "XXXXXXX2");
articleList.add(article2);
Article article3 = new Article(3, "XXX3", "XXXXXXX3");
articleList.add(article3);
Article article4 = new Article(4, "XXX4", "XXXXXXX4");
articleList.add(article4);
Article article5 = new Article(5, "XXX5", "XXXXXXX5");
articleList.add(article5);
System.out.println(articleList);
// 现在需要将2、5两篇文章置顶,其他顺序不变
List hotArticleIds = new ArrayList<>();
hotArticleIds.add(2);
hotArticleIds.add(5);
// 顺序为2,5,1,3,4
List articles = ListSortUtils.sortByMultiList(hotArticleIds, articleList, Article::getId);
System.out.println(articles);
}
}
git地址:https://github.com/shawntime/shawn-common-utils/blob/master/src/main/java/com/shawntime/common/utils/ListSortUtils.java