1.将一个对象的属性值复制给另一个对象
BeanUtils.copyProperties(productInfo,productInfoVO);
2.jdk1.8特性 获取类目type列表
//获取某一属性集合
List<Integer> categoryTypeList = productInfoList.stream()
.map(ProductInfo::getCategoryType)
.collect(Collectors.toList());
//获取对象集合
public List<ProductInfoOutput> findList(List<String> productIdList) {
return productinforepository.findByProductIdIn(productIdList).stream()
.map(e ->{
ProductInfoOutput output = new ProductInfoOutput();
BeanUtils.copyProperties(e,output);
return output;
}).collect(Collectors.toList());
}
3. split 用法注意
// \\会转义成反斜杠,反斜杠本身就是转义符,所有就成了“\.”,在进行转义就是.,所以\\.实际上是“.”
String[] aa = "a.ab.abc".split("\\.");
String cc = "a=1 and b =2 or c=3";
String[] bb =cc.split("and|or"); // |作为连接符
for (int i=0;i<bb.length;i++){
System.out.println(bb[i].replace(" ",""));
}
String[] dd ="2|33|4".split("\\|");
//2 33 4
for (int i=0;i<dd.length;i++){
System.out.println(dd[i].replace(" ",""));
}
4.json 串转换List
参考:https://www.cnblogs.com/zh-1721342390/p/10337428.html
import com.google.gson.Gson;
/**
* 2019/10/23 22:30
*/
@Slf4j
//方法一
public class OrderForm2OrderDTOConverter {
public static OrderDTO convert(OrderForm orderForm){
Gson gson = new Gson();
OrderDTO orderDTO = new OrderDTO();
orderDTO.setBuyerName(orderForm.getName());
orderDTO.setBuyerPhone(orderForm.getAddress());
orderDTO.setBuyerAddress(orderForm.getAddress());
orderDTO.setBuyerOpenid(orderForm.getOpenid());
List<OrderDetail> orderDetails = new ArrayList<>();
try {
//json串编程List
orderDetails =gson.fromJson(orderForm.getItems(),
new TypeToken<List<OrderDetail>>(){}.getType());
}catch (Exception e){
log.error("[json转换] 错误,String={}",orderForm.getItems());
throw new OrderException(ResultEnum.PARAM_ERROR);
}
orderDTO.setOrderDetailList(orderDetails);
return orderDTO;
}
//方法二
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
public static void main(String[] args) {
// 转换方法1
JSONArray array = JSONArray.fromObject("[{'name':'hehe','age':22}]");
List<Person> list = JSONArray.toList(array, Person.class);// 方法已过时
System.out.println(list.get(0).getName());
// 转换方法2
List<?> list2 = JSONArray.toList(array, new Person(), new JsonConfig());
//参数1为 要转换的JSONArray数据,参数2为要转换的目标数据,即List盛装的数据
Person person = (Person) list2.get(0);
System.out.println(person.getAge());
}
}
/**
* 对象转json
* //将java对象转换为json对象
*/
net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(reply);
String str = json.toString();//将json对象转换为字符串
5. 判断List 是否为空
CollectionUtils.isEmpty(list)
public static boolean isEmpty(@Nullable Collection<?> collection) {
return collection == null || collection.isEmpty();
}
public interface List<E> extends Collection<E> {
}
6.生成唯一主键
/**
* 生成唯一的主键
* 格式:时间+随机数
*/
public static synchronized String genUniquekey(){
Random random = new Random();
Integer number = random.nextInt(90000)+100000;
return System.currentTimeMillis()+String.valueOf(number);
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
res.append(String.format("Array:size=%d,capacity =%d\n",size,data.length));
res.append('[');
for (int i=0;i<size;i++){
res.append(data[i]);
if(i!=size-1){
res.append(",");
}
}
res.append("]");
return res.toString();
}
// format,格式化后,输出如下
//Array:size=10,capacity =20
//[0,1,2,3,4,5,6,7,8,9]
//list 转换String (json形式)
net.sf.json.JSONArray json = net.sf.json.JSONArray.fromObject(questionVOS);
String json_str = json.toString();