cn.woodwhales.common.example.business.DataToolExample Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of woodwhales-common Show documentation
Show all versions of woodwhales-common Show documentation
https://github.com/woodwhales
package cn.woodwhales.common.example.business;
import cn.woodwhales.common.business.DataTool;
import cn.woodwhales.common.business.DeduplicateResult;
import cn.woodwhales.common.example.model.business.*;
import cn.woodwhales.common.example.model.business.example.*;
import com.google.gson.Gson;
import java.util.*;
/**
* @author woodwhales on 2021-07-25 12:34
* DataTool 使用示例
*/
public class DataToolExample {
public static void main(String[] args) {
// deduplicate();
// enumMap1();
// enumMap2();
// getDataFromList();
// groupingBy();
// handleMap();
// testMapToList();
// testToList();
// toList();
// toMap1();
// toMap2();
// testGetListFromBaseList();
testGetListFromBaseList2();
}
public static void enumMap1() {
Map map = DataTool.enumMap(DataToolTempEnum.class, DataToolTempEnum::getCode);
assertEquals(3, map.size());
printMap(map);
boolean containsKey = DataTool.enumContainsKey(1, DataToolTempEnum.class, DataToolTempEnum::getCode);
assertEquals(true, containsKey);
DataToolTempEnum DataToolTempEnum = DataTool.enumGetValue(2, DataToolTempEnum.class, cn.woodwhales.common.example.model.business.DataToolTempEnum::getCode);
assertEquals(cn.woodwhales.common.example.model.business.DataToolTempEnum.YELLOW, DataToolTempEnum);
DataToolTempEnum DataToolTempEnum2 = DataTool.enumGetValue(4, DataToolTempEnum.class, cn.woodwhales.common.example.model.business.DataToolTempEnum::getCode);
assertNull(DataToolTempEnum2);
}
public static void enumMap2() {
Map map = DataTool.enumMap(DataToolTempEnum.class);
assertEquals(3, map.size());
printMap(map);
}
public static void toMap2() {
List list = new ArrayList<>();
list.add(new DataToolTempData(1, "张三", "描述-张三"));
DataToolTempData DataToolTempData1 = new DataToolTempData(2, "李四", "描述-李四");
list.add(DataToolTempData1);
DataToolTempData DataToolTempData2 = new DataToolTempData(2, "李四2", "描述-李四2");
list.add(DataToolTempData2);
list.add(new DataToolTempData(3, "王五", "描述-王五"));
list.add(new DataToolTempData(4, "宋八", "描述-宋八"));
Map map1 = DataTool.toMapForSaveOld(list, DataToolTempData::getId);
assertEquals(list.size() - 1, map1.size());
assertEquals(DataToolTempData1, map1.get(2));
printMap(map1);
System.out.println();
Map map2 = DataTool.toMapForSaveNew(list, DataToolTempData::getId);
assertEquals(list.size() - 1, map2.size());
assertEquals(DataToolTempData2, map2.get(2));
printMap(map2);
}
public static void toMap1() {
List list = new ArrayList<>();
list.add(new DataToolTempData(1, "张三", "描述-张三"));
list.add(new DataToolTempData(2, "李四", "描述-李四"));
list.add(new DataToolTempData(3, "王五", "描述-王五"));
list.add(new DataToolTempData(4, "宋八", "描述-宋八"));
Map integerDataToolTempDataMap = DataTool.toMap(list, DataToolTempData::getId);
assertEquals(list.size(), integerDataToolTempDataMap.size());
printMap(integerDataToolTempDataMap);
}
public static void toList() {
List list = new ArrayList<>();
list.add(new DataToolTempData(1, "张三", "描述-张三"));
list.add(new DataToolTempData(2, "李四", "描述-李四"));
list.add(new DataToolTempData(3, "王五", "描述-王五"));
list.add(new DataToolTempData(4, "宋八", "描述-宋八"));
List resultList = DataTool.toList(list, DataToolTempData::getName);
assertEquals(list.get(0).getName(), resultList.get(0));
assertEquals(list.get(2).getName(), resultList.get(2));
}
public static void testToList() {
HashMap map = new HashMap<>();
map.put(1, new DataToolTempMapData1(1, new DataToolTempMapData2(1, "map1")));
map.put(2, new DataToolTempMapData1(2, new DataToolTempMapData2(2, "map2")));
map.put(3, new DataToolTempMapData1(3, new DataToolTempMapData2(3, "map3")));
List list = new ArrayList<>();
list.add(new DataToolTempListData3(1, "A1"));
list.add(new DataToolTempListData3(2, "A2"));
list.add(new DataToolTempListData3(3, "A3"));
list.add(new DataToolTempListData3(4, "A4"));
List DataToolTempListResults = DataTool.toListWithMap(list, map, DataToolTempListData3::getKey, (DataToolTempListData3, DataToolTempMapData1) -> {
Integer key = DataToolTempListData3.getKey();
DataToolTempMapData2 DataToolTempMapData2 = DataToolTempMapData1.getDataToolTempMapData2();
return new DataToolTempListResult(key, DataToolTempMapData2);
});
DataToolTempListResults.forEach(System.out::println);
System.out.println("=========");
List DataToolTempListResults2 = DataTool.toListWithMap(list, map, DataToolTempListData3::getKey, (DataToolTempListData3, DataToolTempMapData1) -> {
Integer key = DataToolTempListData3.getKey();
DataToolTempMapData2 DataToolTempMapData2 = DataToolTempMapData1.getDataToolTempMapData2();
return new DataToolTempListResult(key, DataToolTempMapData2);
}, DataToolTempListData3 -> {
Integer key = DataToolTempListData3.getKey();
return new DataToolTempListResult(key, null);
});
DataToolTempListResults2.forEach(System.out::println);
}
public static void groupingBy() {
List list = new ArrayList<>();
list.add(new DataToolTempData(1, "张三", "描述-张三"));
list.add(new DataToolTempData(2, "李四", "描述-李四"));
list.add(new DataToolTempData(2, "李四2", "描述-李四2"));
list.add(new DataToolTempData(3, "王五", "描述-王五"));
list.add(new DataToolTempData(4, "宋八", "描述-宋八"));
Map> map = DataTool.groupingBy(list, DataToolTempData::getId);
assertEquals(list.size() - 1, map.size());
assertEquals(2, map.get(2).size());
printMap(map);
}
public static void deduplicate() {
List list = new ArrayList<>();
DataToolTempData data1 = new DataToolTempData(1, "张三", "描述-张三");
DataToolTempData data2 = new DataToolTempData(2, "李四", "描述-李四");
DataToolTempData data3 = new DataToolTempData(3, "王五", "描述-王五");
DataToolTempData data4 = new DataToolTempData(null, "赵六", "描述-赵六");
DataToolTempData data5 = new DataToolTempData(3, "朱七", "描述-朱七");
DataToolTempData data6 = new DataToolTempData(4, "宋八", "描述-宋八");
list.add(data1);
list.add(data2);
list.add(data3);
list.add(data4);
list.add(data5);
list.add(data6);
DeduplicateResult deduplicateResult1 = DataTool.deduplicate(list,
data -> Objects.nonNull(data.getId()),
DataToolTempData::getId, true);
List deduplicatedList = deduplicateResult1.getDeduplicatedList();
List repetitiveList = deduplicateResult1.getRepetitiveList();
List invalidList = deduplicateResult1.getInvalidList();
List deduplicatedKeyList = deduplicateResult1.getDeduplicatedKeyList();
System.out.println("deduplicatedList");
deduplicatedList.stream().forEach(System.out::println);
System.out.println("repetitiveList");
repetitiveList.stream().forEach(System.out::println);
System.out.println("invalidList");
invalidList.stream().forEach(System.out::println);
System.out.println("deduplicatedKeyList");
System.out.println(deduplicatedKeyList);
// 无效数据
assertEquals(1, invalidList.size());
assertEquals(data4, invalidList.get(0));
// 已去重数据
assertEquals(4, deduplicatedList.size());
// 重复数据
assertEquals(1, repetitiveList.size());
assertEquals(data5, repetitiveList.get(0));
DeduplicateResult deduplicateResult2 = DataTool.deduplicate(list,
data -> Objects.nonNull(data.getId()),
DataToolTempData::getId, false);
List deduplicatedList2 = deduplicateResult2.getDeduplicatedList();
List repetitiveList2 = deduplicateResult2.getRepetitiveList();
// 重复数据
assertEquals(4, deduplicatedList2.size());
assertEquals(data3, repetitiveList2.get(0));
DeduplicateResult deduplicateResult3 = DataTool.deduplicate(list, DataToolTempData::getId);
List deduplicatedList1 = deduplicateResult3.getDeduplicatedList();
assertEquals(5, deduplicatedList1.size());
}
public static void getDataFromList() {
DataToolTempDataDTO demoDataDTO = new DataToolTempDataDTO(3, "上海");
List list = new ArrayList<>();
list.add(new DataToolTempData(1, "北京", "北京"));
list.add(new DataToolTempData(2, "南京", "南京"));
list.add(new DataToolTempData(3, "上海", "上海"));
DataToolTempData result = DataTool.getDataFromList(demoDataDTO, DataToolTempDataDTO::getId, list, DataToolTempData::getId);
System.out.println("result = " + result);
assertEquals(list.get(2), result);
}
public static void handleMap() {
Map map = new HashMap<>();
map.put(1, new DataToolTempHandleMapDTO(1, new DataToolTempDataDTO(1, "AA")));
map.put(2, new DataToolTempHandleMapDTO(2, new DataToolTempDataDTO(2, "BB")));
map.put(2, new DataToolTempHandleMapDTO(3, new DataToolTempDataDTO(3, "CC")));
System.out.println("new Gson().toJson(map) = " + new Gson().toJson(map));
DataTool.handleMap(map, (k, v) -> {
if("AA".equals(k)) {
new DataToolTempDataDTO(4, "DD");
};
});
System.out.println("new Gson().toJson(map) = " + new Gson().toJson(map));
}
public static void testMapToList() {
Map map = new HashMap<>();
map.put("A", new DataToolTempDataDTO(1, "AA"));
map.put("B", new DataToolTempDataDTO(2, "BB"));
map.put("C", new DataToolTempDataDTO(3, "CC"));
List keyList = DataTool.mapToList(map, (key, value) -> key);
List valueList = DataTool.mapToList(map, (key, value) -> value.toString());
System.out.println("keyList = " + keyList);
System.out.println("valueList = " + valueList);
List filteredList = DataTool.mapToList(map, (key, value) -> key.equals("A"), (key, value) -> value.toString());
System.out.println("filteredList = " + filteredList);
List valueList2 = DataTool.mapValueToList(map, DataToolTempDataDTO::getNameForVO);
System.out.println("valueList2 = " + valueList2);
}
public static void testGetListFromBaseList() {
List userInfoDTOList = new ArrayList<>();
userInfoDTOList.add(new UserInfoDTO(1, "张三"));
userInfoDTOList.add(new UserInfoDTO(2, "李四"));
userInfoDTOList.add(new UserInfoDTO(3, "王五"));
userInfoDTOList.add(new UserInfoDTO(4, "赵六"));
userInfoDTOList.add(new UserInfoDTO(5, "宋七"));
List userExtraInfoDTOList = new ArrayList<>();
userExtraInfoDTOList.add(new UserExtraInfoDTO(2, 30, "北京"));
userExtraInfoDTOList.add(new UserExtraInfoDTO(4, 40, "上海"));
userExtraInfoDTOList.add(new UserExtraInfoDTO(5, 50, "杭州"));
List result = DataTool.getListFromBaseList(userInfoDTOList, UserInfoDTO::getId,
userExtraInfoDTOList, UserExtraInfoDTO::getUserId,
UserDetailDTO::new);
result.stream().forEach(System.out::println);
}
public static void testGetListFromBaseList2() {
List userInfoDTOList = new ArrayList<>();
userInfoDTOList.add(new UserInfoDTO(1, "张三"));
userInfoDTOList.add(new UserInfoDTO(2, "李四"));
userInfoDTOList.add(new UserInfoDTO(3, "王五"));
userInfoDTOList.add(new UserInfoDTO(4, "赵六"));
userInfoDTOList.add(new UserInfoDTO(5, "宋七"));
List userExtraInfoDTO2List = new ArrayList<>();
userExtraInfoDTO2List.add(new UserExtraInfoDTO2(1, "Java 开发手册", 40));
userExtraInfoDTO2List.add(new UserExtraInfoDTO2(1, "Python 开发手册", 8));
userExtraInfoDTO2List.add(new UserExtraInfoDTO2(2, "Java 开发手册", 26));
userExtraInfoDTO2List.add(new UserExtraInfoDTO2(2, "Python 开发手册", 24));
userExtraInfoDTO2List.add(new UserExtraInfoDTO2(3, "算法", 400));
userExtraInfoDTO2List.add(new UserExtraInfoDTO2(5, "设计模式", 2));
List result = DataTool.getListFromBaseList(userInfoDTOList,
UserDetailDTO2::new,
UserDetailDTO2::getUserId,
userExtraInfoDTO2List,
UserExtraInfoDTO2::getUserId,
UserDetailDTO2::addReadBookInfo);
System.out.println("result = " + new Gson().toJson(result));
}
private static void printMap(Map map) {
Map
© 2015 - 2025 Weber Informatics LLC | Privacy Policy