Java
一、 Object转换实体类以及list实体类
object转 实体
Object updateheader = map.get("updateheader");
ObjectMapper objectMapper = new ObjectMapper();
Header updateHeader = objectMapper.convertValue(updateheader,Header.class);
object转list实体
二、 异常 try cath
案例
@Test
public void trycathDemo(){
try {
int i=1/0;
} catch (Exception e) {
System.out.println("-------------"+e.getMessage());
e.printStackTrace();
}
throw new ServiceException("分母不能为0");
}
public void trycathDemo2(){
int i=20;
int j=0;
if(j==0){
throw new Exception("分母不能为0!");
}
}
三、 List
差集、交集、并集、去重并集
public class DemoTestList {
public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("5");
list1.add("6");
List<String> list2 = new ArrayList<String>();
list2.add("2");
list2.add("3");
list2.add("7");
list2.add("8");
// 交集
List<String> intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out :: println);
// 差集 (list1 - list2)
List<String> reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out :: println);
// 差集 (list2 - list1)
List<String> reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out :: println);
// 并集
List<String> listAll = list1.parallelStream().collect(toList());
List<String> listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out :: println);
// 去重并集
List<String> listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out :: println);
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out :: println);
}
筛选
List<Person> collect = personLists.stream().filter(person ->
person.getSalary() > 40 && person.getAge() > 0
).collect(Collectors.toList());
修改值
// 新建对象不会改变原来list的值,不新建对象会改变原来list的值
List<DeliveryCompleteMachineDetail> list = list(Condition.getQueryWrapper(detail));
//确认 和差异 数据 新增
List<DeliveryCompleteMachineDetail> confirmList = list.stream().map(details -> {
DeliveryCompleteMachineDetail detail1Data = new DeliveryCompleteMachineDetail();
BeanUtil.copy(details, detail1Data);
detail1Data.setActive(CompletePlanConstant.Q);
detail1Data.setId(null);
return detail1Data;
}).collect(Collectors.toList());
转数组
long[] longs = deleteList.stream().mapToLong(x -> x.getId()).toArray();
对象某个属性转 list
List collect = listDelete.stream().map(AccountDataConfiguration::getId).collect(Collectors.toList());
List collect2 = listDelete.stream().filter(x-x.getId()!=null &&
x.getType().equals(“2”)).map(AccountDataConfiguration::getId).collect(Collectors.toList());
求和
Integer count = personLists.stream().collect(Collectors.summingInt(Person::getAge));
double countsum = personLists.stream().collect(Collectors.summingDouble((person -> Double.parseDouble(person.getDateStr()))));
深拷贝
list深拷贝复制
public class ListDeepCopy {
public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(src);
ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
ObjectInputStream in = new ObjectInputStream(byteIn);
@SuppressWarnings("unchecked")
List<T> dest = (List<T>) in.readObject();
return dest;
}
}
Orika依赖
<dependency>
<groupId>net.rakugakibox.spring.boot</groupId>
<artifactId>orika-spring-boot-starter</artifactId>
<version>1.9.0</version>
</dependency>
@Autowired
private MapperFacade mapperFacade;
//list和单个对象都可以 注入方式 有缓存不建议使用
List<StorePack> listData= mapperFacade.mapAsList(detailList, StorePack.class);
// //对象不同属性复制 不能用注入的方式,注入的方式有缓存同一个类设置不同属性的复制会有问题 改用初始化的方式
MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(FrBudgetData.class, BudgetElementDataVO.class)
.field("residueAmount", "usedAmount") //不同属性名的映射 本预算剩余额度
.byDefault() //剩余字段(相同属性名)的映射
.register();
List<BudgetElementDataVO> budgetElementDataList = mapperFactory.getMapperFacade().mapAsList(list, BudgetElementDataVO.class);
list分段截取数据
/**
* list 按 多少个数据为一组 分段
* @param sourList
* @param batchCount 个数(按几个一组)
*/
public static void dealBySubList(List<?> sourList, int batchCount){
int sourListSize = sourList.size();
int subCount = sourListSize%batchCount==0 ? sourListSize/batchCount : sourListSize/batchCount+1;
int startIndext = 0;
int stopIndext = 0;
for(int i=0;i<subCount;i++){
stopIndext = (i==subCount-1) ? sourListSize : stopIndext + batchCount;
//分段截取完的数据
List<?> tempList = new ArrayList<>(sourList.subList(startIndext, stopIndext));
startIndext = stopIndext;
}
}
四、Lambda
基础
1.Lambda表达式需要“函数式接口”的支持
2.函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。
3.Lambda表达式的基础语法:Java8中引入了一个新的操作符"->”该操作符称为箭头操作符或Lambda 操作符*
4.左侧:Lambda表达式的参数列表,右侧:Lambda表达式中所需执行的功能,即Lambda体*
5. 语法格式一:无参数,无返回值 :() -> System.out.println( "Hello Lambda! " );
6. 语法格式二:有一个参数,并且无返回值: () -> System.out.println(x)
7. 语法格式三:若只有一个参数,小括号可以省略不写: ×-> System.out.println(x)
8. 语法格式四:有两个以上的参数,有返回值,并且 Lambda体中有多条语句*
最全写法:
Comparator com = (x, y) -> {System.out. print1n(“函数式接口”);return Integer.compare(x, y);}
9.语法格式五:若Lambda 体中只有一条语句,return和大括号都可以省略不写 Comparator com = (x,y) ->Integer. compare(x, y);
10.语法格式六:Lambda表达式的参数列表的数据类型可以省略不写,因为JVN编译器通过上下文推断出,数据类型,即类型推断”* (Integer x,Integer y) -> Integer.compare(x, y);
代码
//实体类可以直接new 所以 lambda 实体类的时候会报错
//接口类里面只有一个方法: 函数式接口
//Lambda 不需要去实现这个接口直接用接口(只能函数式接口 )
public class LamdaDemo {
public static void main(String[] args) {
Music music=(xx,yy)->{
return (xx+yy)+"";
};
String str=music.chui(55,88);
System.out.println(str);// 求和143
}
}
interface Music{
String chui(int a,int b );
}
五、Stream
大佬的文章你们可以看看
https://blog.csdn.net/mu_wind/article/details/109516995
list去重 查重
1.--------------------去重
List<AccountDataConfigurationVO> distinctClass = item.stream().collect(Collectors.collectingAndThen
(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing
(o -> o.getAccountNumber() + ";" + o.getType()))), ArrayList::new));
2.------------------查重(针对批量处理)
//item赋值给itemOrginal(为了不改变传过来的list )
ArrayList<AccountDataConfigurationVO> itemOrginal = new ArrayList<>(item);
//查询数据库所有数据用来做对比
List<AccountDataConfiguration> list = this.list();
//item 是 VO类 list是实体类没有VO, list转换成VO类型
List<AccountDataConfigurationVO> copy = BeanUtil.copy(list, AccountDataConfigurationVO.class);
//转换完的item 是copy 添加到 新的itemOrginal中
itemOrginal.addAll(copy);
//查重复值
List<String> listCheck =itemOrginal.stream().
collect(Collectors.groupingBy(a->"类型:"+a.getType()+",账号"+a.getAccountNumber()+",组织代码"+a.getOrganizationCode(),Collectors.counting()))
.entrySet().stream()
.filter(a->a.getValue()>1)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
if(listCheck.size()>0){
String str="";
for (String s : listCheck) {
str+=s+";";
}
throw new ServiceException(str+"已存在");
}
某个字段内容‘,’的拼接
String collect = accountDataConfigurationsList.stream().map(p -> p.getOrganizationCode()).collect(Collectors.joining(","));
List<String> stringList = Arrays.asList(collect.split(","));
运行时间
long startTime = System.currentTimeMillis(); //获取开始时间
//业务方法
long endTime = System.currentTimeMillis(); //获取结束时间
System.out.println("处理时间:" + (endTime - startTime) + "ms");