工作使用到的方法
1:将集合数组根据指定符合拼接
1 2 3 | StringUtils.join(array,"-");< br >2,List< String > strings = Arrays.asList("foo", "bar", "baz"); String result = strings.stream().collect(Collectors.joining(",")); System.out.println(result); // 输出 "foo,bar,baz" |
2:将集合中的某个字段获取成新的集合;
String[] CompanyName = all1.stream().map(e -> e.getCompanyName()).toArray(String[]::new);
3,将json转换成map
//不可用map接受,json转换的likedHashMap
LinkedHashMap<String, String> stringStringLinkedHashMap = new LinkedHashMap<>(); Map<String, String> map = JSON.parseObject(put, stringStringLinkedHashMap.getClass());
Map<String, String> stringStringMap = JSON.parseObject(JsonUtils.toJsonString(Class), new TypeReference<Map<String, String>>() {
});
});
3.1 将json转为map字典
String str = JsonUtils.toJsonString(o);
Dict dict = JsonUtils.parseMap(str);
4,将entity转换map
Map<String, Object> archiveAttachmentMap = JSON.parseObject(JSON.toJSONString(entity), new TypeReference<Map<String, Object>>(){});
5,获取bean
类 implement ApplicationContextAware 会有 private ApplicationContext applicationContext; applicationContext.applicationContext.getBean(clazz);
6,optional的使用
Optional.of(entity);
7.时间转换的注解,
JSONFileId("yyyy-hh-mm")
使用阿里的注解,可以前后端都转换
8map转数组,不用带<>类型
Map<String, String>[] mapsArr=maps.toArray(new HashMap[maps.size()]);
9,if()else 如果数据一样,使用三元运算符来处理,不用写那么多,显得low
10,stream中map和filter的使用说明
filter中的内容解释下
filter是Stream类的一个helper函数,包含一个Predicate(断言)函数作为参数,对一个Stream对象内的对象进行过滤,如果Predicate函数返回true,则保留此对象,否则被过滤掉,只有在符合条件的情况下,才会被保留。
在解释下map中内容及返回
map是Stream中的一个帮助函数,map会接收一个函数作为参数,然后将这个函数应用到Stream中的每一个元素上,并将其映射成一个新的Stream对象,并返回这个新的Stream对象。函数中会调用添加到result中的数据来进行处理,并最终返回新的一个Stream对象。
那种情况不用写map中不用写return
在使用map函数进行处理时,如果不需要对数据进行改变处理的话,不用写return语句,这个map函数的功能就是将原stream对象的元素进行处理,然后生成一个新的stream对象,并返回这个新的stream对象
1. filter():允许您提供一个predicate以过滤stream中的元素。
2. map():它允许您将一个对象映射成另一个对象。
3. sorted():允许您按照自定义Comparator排序。
4. parallelStream():可以将stream加上parallel()方法处理,以实现多线程处理。
5. collect():将stream中的元素收集到集合中。
6. reduce():在集合上进行聚集操作,如求和,求积等。
7. forEach():它是终止操作,可以对streams中的每个元素执行操作。
8. limit():可以限制从流的源头获取的数量。
9. skip():跳过stream中的指定数量的数据。
10. count():用于计算stream中元素的数量。
10:git 指定某些文件不提交
git update - index -- assume - unchanged url.文件名
11:给两个实体类互相转换赋值
BeanUtils.copyProperties(source, target,“省略的字段”,source.get省略字段);
12:将对象转换为jsonString 时候可以使用,序列程序的枚举将empty转为null
String requestBody = JSONObject.toJSONString(oaContractInfoVO, SerializerFeature.WriteNullStringAsEmpty);
13:使用SpringUtils.getBean(Environment.class)启动包空指针
调用的类上班加注解@DependsOn("springUtils")
14:给数组添加元素
//1,转换为list。进行操作
String[] arr = {"foo", "bar", "baz"}; List<String> list = Stream.of(arr).collect(Collectors.toList());
//2.生成一个新的,将久的copy
String [] newstrings=new String[oldString.length + 1];
System.arraycopy(oldString, 0, newstrings, 0,oldString.length);
newstrings[newstrings.length - 1] = "goods";
15:将Instant 时间格式转换为string时间格式
Instant planDate = paymentPlan.getPlanDate(); DateTimeFormatter yyyyMMdd_hHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String format = yyyyMMdd_hHmmss.format(planDate); // paymentPlan.setPlanDate();
16:进行所有路径,查询等信息,越详细性能好,类似不要select * 这种
17: enum如果只是0/1可以使用enum.property.ordinal()方法获取索引位置,也就是0和1
18:String.trim()去掉字符前后的空格
19:获取项目路径
//获取Class根目录在target中 String resourcePath = getClass().getClassLoader().getResource("").getPath(); //获取项目根目录 String resourcePath = System.getProperty("user.dir");
20,忽略实体属性与mysql字段映射关系,实体多一个实体
@TableField(exist = false) private List<ImpEquipmentInfo> children = new ArrayList<>();
21,使用stream去重
// 根据name去重 List<PointfeaturesVo> unique = pointfeaturesVos.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(PointfeaturesVo::getCode))), ArrayList::new) );
22使用ObjectMapper
ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(json); String runStage = jsonNode.path("runStage").asText();
23不要使用equips对比,如果其中一个为空会空指针异常,建议使用objects的equals
Objects.equals(eq.getFkEquipmentPrototype(), e.getPkEquipmentPrototypes())
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏