- Object转数组工具类
public static List<Object> objToList(Object obj) {
// 不判断Object类型 直接转换成数组
List<Object> list = new ArrayList<Object>();
for (Object o : (List<?>) obj) {
list.add(o);
}
return list;
}
- 复杂JavaBean转换工具类
-
首先导入依赖 // dozer的工具包 <dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.4.0</version> </dependency> // 添加配置类 @Configuration public class DozerBeanMapperConfigure { @Bean public DozerBeanMapper mapper() { DozerBeanMapper mapper = new DozerBeanMapper(); return mapper; } } //自定义工具类 public class DozerUtils { static DozerBeanMapper dozerBeanMapper = new DozerBeanMapper(); public static <T> List<T> mapList(Collection sourceList, Class<T> destinationClass){ List destinationList = Lists.newArrayList(); for (Iterator i$ = sourceList.iterator(); i$.hasNext();){ Object sourceObject = i$.next(); Object destinationObject = dozerBeanMapper.map(sourceObject, destinationClass); destinationList.add(destinationObject); } return destinationList; } } 如果是List对象 使用 DozerUtils.mapList(userList,user.class); userList 是你要转换的List user是你要转换后的对象类型 如果不是List对象 使用 // bean转换工具 @Autowired private Mapper dozerMapper; 然后调用 dozerMapper.map(user,userdto.class);方法
-
每三个逗号分隔字符串
public static List<List<String>> wayPointsUtils(String wayPointSet1) {
// 这里是我前端传的JSON字符串有\ 所以做了处理
wayPointSet1 = wayPointSet1.replaceAll("\"", "");
// 去掉左右花括号
String wayPointSet = wayPointSet1.substring(1, wayPointSet1.length() - 1);
// 此处才是开始分隔字符串的操作 wayPointSet是要处理的字符串
String[] array = wayPointSet.split(",");
List<String> start = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
start.add(array[i]);
}
List<List<String>> result = new ArrayList();
int size = start.size();
if (size > 0) {
int segments = size / 3;
segments = size % 3 == 0 ? segments : segments + 1; // 段数
List<String> cutList = null;
for (int i = 0; i < segments; i++) {
if (i == segments - 1) {
cutList = start.subList(3 * i, size);
} else {
cutList = start.subList(3 * i, 3 * (i + 1));
}
result.add(cutList);
}
}
return result;
}