Java常用笔记
Stream List常用方法
//list对象取某个字段
List<Integer> vehicleIds = list.stream().map(OcCareBasePO::getVehicleId).collect(Collectors.toList());
//list对象转map-重复取后者
Map<Integer, Integer> map = list.getResult().stream().collect(Collectors.toMap(Vehicle3VO::getVehicleID, Vehicle3VO::getVehicleBrand(), (a, b) -> b));
//list对象转map-重复合并为list
Map<Integer, List<OcCareRulePO>> map = list.stream().collect(Collectors.groupingBy(OcCareRulePO::getVehicleBrandDic));
// 根据name去重
List<Person> unique = persons.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new));
//过滤空值
List<OcDispatchVO> repairFactoryIds = list.stream().filter(l -> Objects.nonNull(l.getRepairFactoryId())).collect(Collectors.toList());
//累加器
BigDecimal actualUnloadingWeight = orderInfoDeE6.getResult().stream().map(OrderListResultVO::getActualLoadingWeight).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6);
//排序
Collections.sort(students, (first, second) -> first.getScore() - second.getScore());
// 取交集
List<String> list3= list1.stream().filter(item -> list2.contains(item)).collect(Collectors.toList());
// 取差集
List<String> list3= list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
同步,锁常用方法
版本一:
Interner<String> interner = Interners.newWeakInterner();
synchronized (interner.intern(mobile)){
//校验
mobileSet.contains(mobile);
//业务逻辑
do some business;
//业务逻辑处理成功
mobileSet.add(mobile)
}
版本二:
//定义锁对象
private final ReentrantLock lock = new ReentrantLock();
public void matching() throws Exception {
lock.lock();
try{
//业务处理
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
String常用方法
// 格式化指定位数 0为左补零,5为总长度,d位整数,结果为:00123
String.format("%05d", "123");