当前位置: 首页 > news >正文

【Java】常用方法合集

以 DemoVo 为实体

import lombok.Data;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;@Data
@ExcelIgnoreUnannotated
public class ExportPromoteUnitResult {private String id;@ExcelProperty(value = "学号")private String stuNo;@ExcelProperty(value = "姓名")private String stuName;
}
  1. 将一个实体的属性名取出来,整合为一个 List<String[]>格式
    private static List<String[]> getData() {// new一个实体DemoVoDemoVo demo = new DemoVo();List<String[]> propertyNames = getPropertyNames(demo);return propertyNames;}public static List<String[]> getPropertyNames(Object obj) {List<String[]> properties = new ArrayList<>();Class<?> clazz = obj.getClass(); // 获取对象的Class对象Field[] fields = clazz.getDeclaredFields(); // 获取所有字段for (Field field : fields) {if (field.isAccessible()) { // 如果字段可访问,则添加其名称到列表中(对于私有字段可能需要设置可访问)properties.add(new String[]{field.getName()}); // 将属性名添加到List<String[]>中,每个String[]包含一个属性名} else {field.setAccessible(true); // 对于私有字段,需要设置可访问才能获取其名称(注意:这可能会破坏封装性)properties.add(new String[]{field.getName()}); // 添加属性名到List<String[]>}}return properties; // 返回包含所有属性名的List<String[]>}
  1. 将一个实体的属性名整合为 List< String > 格式
	// new一个实体DemoVo,取出属性名DemoVo demo = new DemoVo();List<String[]> propertyNames = getPropertyNames(demo.getClass());System.out.print("propertyNames的输出结果为:" + propertyNames); // propertyNames的输出结果为:[id,stuNo,stuName]// 获取实体类的所有字段名public List<String> getPropertyNames(Class<?> clazz) {Field[] fields = clazz.getDeclaredFields(); // 获取所有字段List<String> propertyNames = new ArrayList<>(); // 新建一个数组propertyNamesfor (Field field : fields) {propertyNames.add(field.getName()); // 将所有字段放于数组propertyNames中}return propertyNames;}
  1. 根据属性名/字段名获取对应的值
    List<Map<String, Object>> maps = combinFixedLists(stuClassId);System.out.print("maps的输出结果为:" + maps);// maps的输出结果为:[{id=1,stuNo="2024001",stuName="李四",stuSex="1"},{id=2,stuNo="2024002",stuName="张三",stuSex="1"}]private List<Map<String, Object>> combinFixedLists(Long stuClassId) throws IllegalAccessException, NoSuchFieldException {// 先根据stuClassId查询数据,此处不过多写sql语句了List<StudentClassResult> listObjects = this.queryStuResult(stuClassId);System.out.print("假如得出的结果为:" + listObjects);// 假如得出的结果为:[{id:1,stuNo:"2024001",stuName:"李四",stuSex:"1"},{id:2,stuNo:"2024002",stuName:"张三",stuSex:"1"}]List<Map<String, Object>> combinedList = new ArrayList<>();for (StudentClassResult object : listObjects) {Map<String, Object> fieldMap = new HashMap<>();Field[] fields = object.getClass().getDeclaredFields();for (Field field : fields) {String name = field.getName();field.setAccessible(true);fieldMap.put(field.getName(), field.get(object));}combinedList.add(fieldMap);}return combinedList;}
  1. 去掉List< String >中指定元素
	removePropertyFromString(propertyNames, "id");// 去掉指定属性名private static void removePropertyFromString(List<String> fixList, String fixElement) {Iterator<String> iterator = fixList.iterator();while (iterator.hasNext()) {String next = iterator.next();if (next == fixElement) {iterator.remove();}}}
  1. 取出JSON中的某属性名对应的属性值
	System.out.print(stuData)// stuData={"stuData":{"stuId":57,"photoList":{"fileName":["照片.png"],"filePath":["/qcbucket/2024/10/22/01.照片.png"]},"stuClassId":"3","stuName":"张三"}}Object stuId = null;Object photoList = null;Object stuClassId = null;Object stuName = null;JSONObject jsonObject = JSONObject.parseObject(stuData.toString());stuId = jsonObject.getJSONObject("stuData").get("stuId ");// 57photoList = jsonObject.getJSONObject("stuData").get("photoList");// "fileName":["照片.png"],"filePath":["/qcbucket/2024/10/22/01.照片.png"]stuClassId = jsonObject.getJSONObject("stuData").get("stuClassId");// "3"stuName = jsonObject.getJSONObject("stuData").get("stuName");// "张三"
  1. 保留两位数

(1)setScale

String num = "123.4567";
BigDecimal val = new BigDecimal(num);
BigDecimal roundedVal = val.setScale(2, RoundingMode.HALF_UP);
System.out.println(roundedVal); // 输出: 123.46

(2)sql语句

在这里插入图片描述

  1. 原数组 String[] originalArray,在指定元素后添加n个空字符串 “”
    // 原数组originalArray = new String[]{"apple","banana"}// 查找指定字符串String[] target = {"apple"}// 在指定字符串后追加num个 空字符串int num = 2;String[] array = modifyArray(originalArray, target, num);System.out("array的结果为:" + array);// array的结果为:["apple","","","banana"]private static String[] modifyArray(String[] originalArray, String[] target, int n) {List<String> resultList = new ArrayList<>();for (String element : originalArray) {resultList.add(element);for (String add : target) {if (element.equals(add)) {for (int i = 0; i < n; i++) {resultList.add("");}}}}return resultList.toArray(new String[0]);}
  1. 获取 resources 下 指定文件的路径
    在这里插入图片描述
URL resource = getClass().getClassLoader().getResource("ip2region.xdb");
String XDB_PATH = resource.getPath();
http://www.lryc.cn/news/466614.html

相关文章:

  • 深入了解Vue Router:基本用法、重定向、动态路由与路由守卫的性能优化
  • 深入理解InnoDB底层原理:从数据结构到逻辑架构
  • Linux介绍及操作命令
  • JS | 详解图片懒加载的6种实现方案
  • Java | Leetcode Java题解之第502题IPO
  • JavaWeb学习(3)
  • 【含开题报告+文档+PPT+源码】基于SpringBoot的百货商城管理系统的设计与实现
  • Elasticsearch 实战应用与优化策略研究
  • 植物大战僵尸杂交版游戏分享
  • ProteinMPNN中DecLayer类介绍
  • Flux.all 使用说明书
  • DORA 机器人中间件学习教程(6)——激光点云预处理
  • 搜维尔科技:TechViz将您的协同项目评审提升到一个全新的高度
  • Dinky 字段模式演变 PIPELINE 同步MySQL到Doris
  • 【Docker】Harbor 私有仓库和管理
  • 《重置MobaXterm密码并连接Linux虚拟机的完整操作指南》
  • 每天五分钟深度学习:逻辑回归和神经网络
  • 深度学习——线性神经网络(五、图像分类数据集——Fashion-MNIST数据集)
  • 音频声音怎么调大?将音频声音调大的几个简单方法
  • C#的委托
  • 软考(网工)——局域网和城域网
  • MySQL 9从入门到性能优化-通用查询日志
  • 解码专业术语——应用系统开发项目中的专业词汇解读
  • 高级java每日一道面试题-2024年10月18日-JVM篇-说下你对G1垃圾收集器的理解?
  • 2024系统架构师---湖仓一体架构论文知识点
  • Unity性能优化
  • MyHdfs代码分享
  • Java网络编程-简单的API调用
  • 论文笔记(五十)Segmentation-driven 6D Object Pose Estimation
  • 微服务的一些基本概念