我们在平时开发中,会遇到很多转换数据的操作,通常会将数据转换成List集合会方便很多,因为在Java_1.8提供了很多针对List的Stream方法;将一些平时比较容易实用的转换整理了下,希望对大家有帮助:
/**
* <b>Function: </b> todo
*
* @program: ListUtils
* @Package: com.kingbal.king.dmp.utils
* @author: dingcho
* @date: 2024/06/04
* @version: 1.0
* @Copyright: 2024 www.kingbal.com Inc. All rights reserved.
*/
public class ListsUtils {
/**
* 将一组数据平均分成n组
*
* @param source 要分组的数据源
* @param n 平均分成n组
* @param <T>
* @return
*/
public static <T> List<List<T>> averageAssign(List<T> source, int n) {
List<List<T>> result = new ArrayList<>();
int remainder = source.size() % n; //(先计算出余数)
int number = source.size() / n; //然后是商
int offset = 0;//偏移量
for (int i = 0; i < n; i++) {
List<T> value = null;
if (remainder > 0) {
value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
remainder--;
offset++;
} else {
value = source.subList(i * number + offset, (i + 1) * number + offset);
}
result.add(value);
}
return result;
}
/**
* 将一组数据固定分组,每组n个元素
*
* @param source 要分组的数据源
* @param n 每组n个元素
* @param <T>
* @return
*/
public static <T> List<List<T>> fixedGrouping(List<T> source, int n) {
if (null == source || source.size() == 0 || n <= 0)
return null;
List<List<T>> result = new ArrayList<>();
int sourceSize = source.size();
int size = (source.size() / n) + 1;
for (int i = 0; i < size; i++) {
List<T> subset = new ArrayList<T>();
for (int j = i * n; j < (i + 1) * n; j++) {
if (j < sourceSize) {
subset.add(source.get(j));
}
}
result.add(subset);
}
return result;
}
/**
* 将一组数据固定分组,每组n个元素
*
* @param source 要分组的数据源
* @param n 每组n个元素
* @param <T>
* @return
*/
public static <T> List<List<T>> fixedGrouping2(List<T> source, int n) {
if (null == source || source.size() == 0 || n <= 0)
return null;
List<List<T>> result = new ArrayList<>();
int remainder = source.size() % n;
int size = (source.size() / n);
for (int i = 0; i < size; i++) {
List<T> subset = source.subList(i * n, (i + 1) * n);
result.add(subset);
}
if (remainder > 0) {
List<T> subset = source.subList(size * n, size * n + remainder);
result.add(subset);
}
return result;
}
/**
* 指定数字转换为集合
*
* @param number
* @return
*/
public static List<Integer> fixedList(int number) {
List<Integer> list = Lists.newArrayList();
for (int i = 1; i <= number; i++) {
list.add(i);
}
return list;
}
/**
* 指定数字转换为集合
* @param minInt
* @param maxInt
* @return
*/
public static List<Integer> fixedMinMaxList(int minInt, int maxInt) {
List<Integer> list = Lists.newArrayList();
for (int i = minInt; i <= maxInt; i++) {
list.add(i);
}
return list;
}
/**
* 二维集合
*
* @param number
* @param number2
* @return
*/
public static List<String> fixedList(int number, int number2) {
List<String> list = Lists.newArrayList();
for (int i = 1; i <= number; i++) {
for (int k = 1; k <= number2; k++) {
list.add(i + "-" + k);
}
}
return list;
}
/**
* 获取时间分组
*
* @return
*/
public static Map<Integer, Integer> getTimesGroup() {
Map<Integer, Integer> map = Maps.newHashMap();
int table = 10;
int year = 1990;
for (int i = 1; i <= table; i++) {
if (i > 1 && i <= 8) {
for (int k = 0; k < 3; k++) {
map.put(year++, 100 + i);
}
} else if (i > 8) {
for (int k = 0; k < 2; k++) {
if (i > 9) {
map.put(year++, 1000 + i);
} else {
map.put(year++, 100 + i);
}
}
} else {
for (int k = 0; k < 10; k++) {
map.put(year++, 100 + i);
}
}
}
return map;
}
/**
* 根据已有数据进行数字分组
* @param list
* @return
*/
public static Map<String, Integer> getNumberGroup(List<Long> list) {
Map<String, Integer> map = Maps.newHashMap();
list = list.stream().distinct().collect(Collectors.toList());
Collections.sort(list);
List<Long> list2 = Lists.newArrayList();
List<Long> list3 = Lists.newArrayList();
for (int i = 0; i < list.size(); i++) {
StringBuffer key = new StringBuffer();
if (i % 2 == 0) {
list2.add(list.get(i));
} else {
list3.add(list.get(i));
}
}
if (list.size() % 2 != 0) {
list3.add(Collections.max(list));
}
for (int i = 0; i < list2.size(); i++) {
map.put(list2.get(i) + "~" + list3.get(i), 0);
}
return map;
}
/**
* 获取多个list最大的size
* @param lists
* @return
*/
public static Integer getMaxSize(List<?>... lists) {
if (ObjectUtils.isEmpty(lists)) {
return 0;
}
List<Integer> maxSizeList = new ArrayList<>();
for (List<?> list : lists) {
maxSizeList.add(ObjectUtils.isEmpty(list) ? 0 : list.size());
}
return ObjectUtils.isEmpty(maxSizeList) ? 0 : Collections.max(maxSizeList);
}
/**
* 填充空值
* @param list
* @param maxSize
* @param <T>
* @return
*/
public static <T> List<T> fillValue(List<T> list, int maxSize) {
if(list.size() < maxSize){
for (int i = 0; i < maxSize-list.size() +1; i++) {
list.add(null);
}
}
return list;
}
}
根据自己的项目需求选择对应方法,如果有其他方法欢迎留言~~~~~~