1、JSONObject
1.2、List<Map<String, String>> 和 Map<String, String> 类型返回的参数如何使用
2、string.split(“,”)(拆除string字符串转为list)
3、时间和string转换 时间转换为时间戳
4、string字符串截取 字符串位数不够前面补0
5、double类型转int类型
6、map<String,Object> Object转换
BigDecimal cannot be cast to java.lang.Double
java.math.BigDecimal cannot be cast to java.lang.String
7、包装类
8、随机数 应该是从1往后叠加的
9、随机id 32位
10、新增配置文件
11 数组和集合的添加
12、时间转换时间戳
13、生成二维码
14、forcher 循环 兰么大使用
16、 ipage 和 list 转换
17、判断是否为空 或者判断是否相同或者不同执行
18、json 前端传过来= 转换为 :
19 去掉指定字符串里面的东西
1、JSONObject
JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构)
JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象
将数据封装成json然后传递给前端
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
使用
JSONObject liyi = new JSONObject();
//添加
liyi.put("name", "李一");
liyi.put("age", 18);
其他方法生成
HashMap<String, Object> liyi = new HashMap<>();
liyi.put("name", "liyi");
liyi.put("age", 18);
System.out.println(new JSONObject(liyi).toString());
1.2、List<Map<String, String>> 和 Map<String, String> 类型返回的参数如何使用
Map<String,String>类型的实体使用:
xml文件里面使用:resultType=“java.util.Map”
sql里面:select equipment_no as equipmentNo
java里面获取字段参数:meters.get(“equipmentNo”)
List<Map<String, String>> 类型的实体:
xml文件里面使用:resultType=“java.util.Map”
list 就是多了一个循环 list.get(0).get(“equipmentNo”)
**2、string.split(“,”)
存储的String字符串 用,隔开的**
例如:
String string = "boo,and,foo";
List<String> EnclosureList = new ArrayList<>();
for (String temp : string.split(",")) {
//空字符串的话输出--“空字符串”
if (temp.equals("")) {
System.out.println("空字符串");
} else {
EnclosureList.add(temp);
}
}
2222222222
去掉最后一个,
String str = "hello,world,java,";
int lastCommaIndex = str.lastIndexOf(",");
str = str.substring(0, lastCommaIndex);
```1、JSONObject
1.2、List<Map<String, String>> 和 Map<String, String> 类型返回的参数如何使用
2、string.split(",")(拆除string字符串转为list)
3、时间和string转换 时间转换为时间戳
4、string字符串截取 字符串位数不够前面补0
5、double类型转int类型
6、map<String,Object> Object转换
BigDecimal cannot be cast to java.lang.Double
java.math.BigDecimal cannot be cast to java.lang.String
7、包装类
8、随机数 应该是从1往后叠加的
9、随机id 32位
10、新增配置文件
11 数组和集合的添加
12、时间转换时间戳
13、生成二维码
14、forcher 循环 兰么大使用
16、 ipage 和 list 转换
17、判断是否为空 或者判断是否相同或者不同执行
18、json 前端传过来= 转换为 :
19 去掉指定字符串里面的东西
1、JSONObject
JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构)
JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象
将数据封装成json然后传递给前端
依赖
com.alibaba fastjson 1.2.46
使用
JSONObject liyi = new JSONObject();
//添加
liyi.put(“name”, “李一”);
liyi.put(“age”, 18);
其他方法生成
HashMap<String, Object> liyi = new HashMap<>();
liyi.put("name", "liyi");
liyi.put("age", 18);
System.out.println(new JSONObject(liyi).toString());
## 1.2、List<Map<String, String>> 和 Map<String, String> 类型返回的参数如何使用
Map<String,String>类型的实体使用:
xml文件里面使用:resultType="java.util.Map"
> sql里面:select equipment_no as equipmentNo
> java里面获取字段参数:meters.get("equipmentNo")
List<Map<String, String>> 类型的实体:
xml文件里面使用:resultType="java.util.Map"
> list 就是多了一个循环 list.get(0).get("equipmentNo")
**2、string.split(",")
**存储的String字符串 用,隔开的****
例如:
String string = “boo,and,foo”;
List EnclosureList = new ArrayList<>();
for (String temp : string.split(“,”)) {
//空字符串的话输出–“空字符串”
if (temp.equals(“”)) {
System.out.println(“空字符串”);
} else {
EnclosureList.add(temp);
}
}
2222222222
去掉最后一个,
String str = “hello,world,java,”;
int lastCommaIndex = str.lastIndexOf(“,”);
str = str.substring(0, lastCommaIndex);
多个符号的:
public void Test3() {
string = “asd-sdf+sda+sda”;
//匹配-或者+
String[] split = string.split(“[-\+]”);
printSplit(split);
}
**3、时间和string转换**
时间转换成string
> SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
> SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
> Date date =new Date();
> **String newDate=sdf2.format(date)**;//当前年月
输出:
> newDate:"2023-01"
**string转换成date**
String newDate=“2023-01-02 00:00:00”
SimpleDateFormat sdf= new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date a=sdf.parse(newDate);
时间转换为时间戳
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date time = new Date(); // 获取当前时间
long timestamp = time.getTime() / 1000L;//当前时间的时间戳
**4、string字符串截取**
String newDate=“2023-01-02”;
String newMM=newDate.substring(5);//当前月 保留5位后面的
int newYY=Integer.valueOf(newDate.substring(0,4));//当前年 从0开始留四位
输出:
> newMM:"01-02"
> newYY:"2023"
>
**
# > 在数字前面补零
**
> i=1;
String.format("%03d", i)
4.3 创建时间
new Timestamp(System.currentTimeMillis())
new Date()
举例:
collect.setCreateTime(new Timestamp(System.currentTimeMillis()));
## 5、double类型转int类型
## java.lang.NumberFormatException: For input string: “0.0“异常解决
,得强转,Double.intValue(),而不是用Integer.parseInt()
6、Object转换成其他的类型
原因是将数据库中数值型取出保存到 map<String,Object>中,需要进行数值运算,转成double类型时抛出ava.math.BigDecimal cannot be cast to java.lang.Double。
或者ava.math.BigDecimal cannot be cast to java.lang.String的报错 或者其他类型报错
> 使用:
> 1.转成string String num_str=map.get("key").toString();
> 2.转成double,需承接上面的转成String Double num_double=Double.parseDouble(num_str)
>
> 错误代码 //code int num = (int)map.get(key); 1 解决方法 int num =
> Integer.parseInt(String.valueOf(map1.get(key)))
数字类型的String字符串转换为浮点数通常采用parseDouble()和valueOf()方法,两者主要是存在以下两点区别。
区别一:参数区别
Double.parseDouble(java.lang.String)的参数只能是String,如果参数改为double类型提示“The method parseDouble(String) in the type Double is not applicable for the arguments (double)”错误。
Double.valueOf()的参数类型可以是浮点型或者是字符串均可。
7、包装类
包装类(Wrapper Class): Java是一个面向对象的编程语言,
**Java中的八种基本数据类型却是不面向对象的,所以为了方便使用,在设计类时为每个基本数据类型设计了一个对应的类进行代表**,这样八种基本数据类型对应的类统称为包装类(Wrapper Class),包装类均位于java.lang包。
二、包装类和基本数据类型的转换
1、为了使用方便Java中将8种基本数据类型进行了封装:除了Integer和Character类以外,其它六个类的类名和基本数据类型一直,只是类名的第一个字母大写即可。
自动拆装箱
JDK自从1.5版本以后,就引入了自动拆装箱的语法,也就是在**进行基本数据类型和对应的包装类转换时,系统将自动进行**,这将大大方便程序员的代码书写。
**自动装箱:将 基本数据类型 封装为对象类型,来符合java的面向对象的思想。
自动拆箱:将对象重新转化为基本数据类型。**
**8、随机数 应该是从1往后叠加的**
long sj=Math.round(Math.random()*50);
**9、随机id 32位**
import cn.hutool.core.util.IdUtil;
IdUtil.simpleUUID()
UUID.randomUUID().toString().replace("-","");
**10、新增配置文件**
1需要新增配置文件的话 需要在application.yml 文件里面添加新增文件的后缀
2、使用@Value引用写的配置参数就行了
![在这里插入图片描述](https://img-blog.csdnimg.cn/61c2020a528d49738012eb95aedee31a.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/355116e6ea8346078009055f376f5661.png)
## 11 数组和集合的添加
集合:
public ProductType(Integer id, String name, Integer parentId, Integer status) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.status = status;
}
productTypes = new ArrayList<>();
//建立要输入的菜单信息
productTypes.add(new ProductType(1, "手机", 0, 1));
productTypes.add(new ProductType(2, "电脑", 0, 1));
productTypes.add(new ProductType(3, "电器", 0, 1));
productTypes.add(new ProductType(4, "游戏手机", 1, 1));
数组:
public Product(String description, double price) {
this.description = description;
this.price = price;
}
public Product[] getProductTypes() {
Product[] productTypes = {new Product("手机", 30),
new Product("电脑", 30),
new Product("电器", 80),
new Product("家电", 120)};
return productTypes;//更改
}
12、 获取当前时间
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date =new Date();
String newDate=sdf1.format(date);
List<Yuyue> list = yuyueService.selectYuyueList(yuyue);
List<Yuyue> list2=new ArrayList<>();
//循环记录 判断我的时间比开始时间早在30分钟之内 就提示快开始了
// 如果我的时间比开始时间晚了三十分钟之内 就提示已经开始了 30分钟 3600
// 如果我的时间比结束时间晚二十分钟之内 提示结束了
for (Yuyue y : list) {
try {
Long beginUseTime = sdf1.parse(y.getStartTime()).getTime();
Long beginendTime = sdf1.parse(y.getEndTime()).getTime();
Long newTime = sdf1.parse(newDate).getTime();
//开始时间比当前时间大十分钟之内 开课之前十分钟之内
if(beginendTime-newTime<1200000&&beginendTime-newTime>0){
y.setStatus("即将开始");
list2.add(y);
}
13、生成二维码
String content = "https://www.baidu.com/";
String name = "baidui";
boolean code = new com.pink.jucstudy.utils.CodeX().createCode(content, "D:\\XX\\" + name +".png");
if (code){
System.out.println("成功");
}else {
System.out.println("失败了");
}
14 例如 ids=123,456,789
把这个分成数组形式
List<String> idList = Arrays.asList(ids.split(","));
15、
![在这里插入图片描述](https://img-blog.csdnimg.cn/5842cd70b11b4e65bc00c2d09e9e9191.png)
16、 IPage<Contacts> contactsList1 = contactsMapper.selectUserContacts(page,criteria,struId);
Long t=contactsList1.getTotal();
List<Contacts> contactsList=contactsList1.getRecords();
contactsList1.setRecords(contactsList);
contactsList1.setTotal(t);
return contactsList1;
page转换list
Page --> List
page.getcontent(); 返回的是list<object>
Long total=page.getTotalElements();//总条数
int pages=page.getTotalPages();//总页数
list page
list名称为 subList //list名称
Pageable pageable = new PageRequest(page, size);//分页
Page<Pipeline> pipelinePage = new PageImpl<>(subList, pageable, count);//转换用new pageImpl
return new PageInfo<>(pipelinePage);
subList 是分页后的集合
pageable 是“页码”和“每页多少条数据”
count 是数据总条数
17、判断是否为空 或者判断是否相同或者不同执行
if(!userId.equals("1")){} //和1不相同的时候
if(userId.equals("1")){} //和1相同的时候
StringUtils.isNotNull(tVchiclePostApproverList)) //判断list是否为空的
Strings.isNotEmpty() //判断字符串是否为空的
18、
32位随机数
Long uuid = IdWorker.getId();
tVchiclePost.setId(uuid);
18、 String s = JSON.toJSONString(jsonObject.get("list"));
// 此处转一下防止引号问题带来的报错
List<TVchiclePost> lists = JSON.parseArray(s, TVchiclePost.class);
for (int i = 0; i < lists.size(); i++) {
tVchiclePostService.updateTVchiclePost(lists.get(i));
}
19
public String trimStr(String str, String indexStr){
if(str == null){
return null;
}
StringBuilder newStr = new StringBuilder(str);
if(newStr.indexOf(indexStr) == 0){
newStr = new StringBuilder(newStr.substring(indexStr.length()));
}else if(newStr.indexOf(indexStr) == newStr.length() - indexStr.length()){
newStr = new StringBuilder(newStr.substring(0,newStr.lastIndexOf(indexStr)));//在结尾
}else if(newStr.indexOf(indexStr) < (newStr.length() - indexStr.length())){
newStr = new StringBuilder(newStr.substring(0,newStr.indexOf(indexStr))
+newStr.substring(newStr.indexOf(indexStr)+indexStr.length(),newStr.length()));
}
return newStr.toString();
}
String str1="/ktms/user/find.jspx";
String str2="/ktms";
String str3="tms";
String str4=".jspx";
System.out.println(this.trimStr(str1, str2));
System.out.println(this.trimStr(str1, str3));
System.out.println(this.trimStr(str1, str4));
![在这里插入图片描述](https://img-blog.csdnimg.cn/fa030b4d8b4f46e8ba576833f8ce7500.png)
多个符号的:
public void Test3() {
string = “asd-sdf+sda+sda”;
//匹配-或者+
String[] split = string.split(“[-\+]”);
printSplit(split);
}
**3、时间和string转换**
时间转换成string
> SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
> SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
> Date date =new Date();
> **String newDate=sdf2.format(date)**;//当前年月
输出:
> newDate:"2023-01"
**string转换成date**
String newDate=“2023-01-02 00:00:00”
SimpleDateFormat sdf= new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date a=sdf.parse(newDate);
时间转换为时间戳
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Date time = new Date(); // 获取当前时间
long timestamp = time.getTime() / 1000L;//当前时间的时间戳
**4、string字符串截取**
String newDate=“2023-01-02”;
String newMM=newDate.substring(5);//当前月 保留5位后面的
int newYY=Integer.valueOf(newDate.substring(0,4));//当前年 从0开始留四位
输出:
> newMM:"01-02"
> newYY:"2023"
>
**
# > 在数字前面补零
**
> i=1;
String.format("%03d", i)
## 5、double类型转int类型
## java.lang.NumberFormatException: For input string: “0.0“异常解决
,得强转,Double.intValue(),而不是用Integer.parseInt()
6、Object转换成其他的类型
原因是将数据库中数值型取出保存到 map<String,Object>中,需要进行数值运算,转成double类型时抛出ava.math.BigDecimal cannot be cast to java.lang.Double。
或者ava.math.BigDecimal cannot be cast to java.lang.String的报错 或者其他类型报错
> 使用:
> 1.转成string String num_str=map.get("key").toString();
> 2.转成double,需承接上面的转成String Double num_double=Double.parseDouble(num_str)
>
> 错误代码 //code int num = (int)map.get(key); 1 解决方法 int num =
> Integer.parseInt(String.valueOf(map1.get(key)))
数字类型的String字符串转换为浮点数通常采用parseDouble()和valueOf()方法,两者主要是存在以下两点区别。
区别一:参数区别
Double.parseDouble(java.lang.String)的参数只能是String,如果参数改为double类型提示“The method parseDouble(String) in the type Double is not applicable for the arguments (double)”错误。
Double.valueOf()的参数类型可以是浮点型或者是字符串均可。
7、包装类
包装类(Wrapper Class): Java是一个面向对象的编程语言,
**Java中的八种基本数据类型却是不面向对象的,所以为了方便使用,在设计类时为每个基本数据类型设计了一个对应的类进行代表**,这样八种基本数据类型对应的类统称为包装类(Wrapper Class),包装类均位于java.lang包。
二、包装类和基本数据类型的转换
1、为了使用方便Java中将8种基本数据类型进行了封装:除了Integer和Character类以外,其它六个类的类名和基本数据类型一直,只是类名的第一个字母大写即可。
自动拆装箱
JDK自从1.5版本以后,就引入了自动拆装箱的语法,也就是在**进行基本数据类型和对应的包装类转换时,系统将自动进行**,这将大大方便程序员的代码书写。
**自动装箱:将 基本数据类型 封装为对象类型,来符合java的面向对象的思想。
自动拆箱:将对象重新转化为基本数据类型。**
**8、随机数 应该是从1往后叠加的**
long sj=Math.round(Math.random()*50);
**9、随机id 32位**
import cn.hutool.core.util.IdUtil;
IdUtil.simpleUUID()
UUID.randomUUID().toString().replace("-","");
IdWorker.getId(sysOrgan)
**10、新增配置文件**
1需要新增配置文件的话 需要在application.yml 文件里面添加新增文件的后缀
2、使用@Value引用写的配置参数就行了
![在这里插入图片描述](https://img-blog.csdnimg.cn/61c2020a528d49738012eb95aedee31a.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/355116e6ea8346078009055f376f5661.png)
## 11 数组和集合的添加
集合:
public ProductType(Integer id, String name, Integer parentId, Integer status) {
this.id = id;
this.name = name;
this.parentId = parentId;
this.status = status;
}
productTypes = new ArrayList<>();
//建立要输入的菜单信息
productTypes.add(new ProductType(1, "手机", 0, 1));
productTypes.add(new ProductType(2, "电脑", 0, 1));
productTypes.add(new ProductType(3, "电器", 0, 1));
productTypes.add(new ProductType(4, "游戏手机", 1, 1));
数组:
public Product(String description, double price) {
this.description = description;
this.price = price;
}
public Product[] getProductTypes() {
Product[] productTypes = {new Product("手机", 30),
new Product("电脑", 30),
new Product("电器", 80),
new Product("家电", 120)};
return productTypes;//更改
}
12、 获取当前时间
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date =new Date();
String newDate=sdf1.format(date);
List<Yuyue> list = yuyueService.selectYuyueList(yuyue);
List<Yuyue> list2=new ArrayList<>();
//循环记录 判断我的时间比开始时间早在30分钟之内 就提示快开始了
// 如果我的时间比开始时间晚了三十分钟之内 就提示已经开始了 30分钟 3600
// 如果我的时间比结束时间晚二十分钟之内 提示结束了
for (Yuyue y : list) {
try {
Long beginUseTime = sdf1.parse(y.getStartTime()).getTime();
Long beginendTime = sdf1.parse(y.getEndTime()).getTime();
Long newTime = sdf1.parse(newDate).getTime();
//开始时间比当前时间大十分钟之内 开课之前十分钟之内
if(beginendTime-newTime<1200000&&beginendTime-newTime>0){
y.setStatus("即将开始");
list2.add(y);
}
13、生成二维码
String content = "https://www.baidu.com/";
String name = "baidui";
boolean code = new com.pink.jucstudy.utils.CodeX().createCode(content, "D:\\XX\\" + name +".png");
if (code){
System.out.println("成功");
}else {
System.out.println("失败了");
}
14 例如 ids=123,456,789
把这个分成数组形式
List<String> idList = Arrays.asList(ids.split(","));
15、
![在这里插入图片描述](https://img-blog.csdnimg.cn/5842cd70b11b4e65bc00c2d09e9e9191.png)
16、 IPage<Contacts> contactsList1 = contactsMapper.selectUserContacts(page,criteria,struId);
Long t=contactsList1.getTotal();
List<Contacts> contactsList=contactsList1.getRecords();
contactsList1.setRecords(contactsList);
contactsList1.setTotal(t);
return contactsList1;
17、判断是否为空 或者判断是否相同或者不同执行
if(!userId.equals("1")){} //和1不相同的时候
if(userId.equals("1")){} //和1相同的时候
StringUtils.isNotNull(tVchiclePostApproverList)) //判断list是否为空的
Strings.isNotEmpty() //判断字符串是否为空的
18、
32位随机数
Long uuid = IdWorker.getId();
tVchiclePost.setId(uuid);
18、 String s = JSON.toJSONString(jsonObject.get("list"));
// 此处转一下防止引号问题带来的报错
List<TVchiclePost> lists = JSON.parseArray(s, TVchiclePost.class);
for (int i = 0; i < lists.size(); i++) {
tVchiclePostService.updateTVchiclePost(lists.get(i));
}
19
public String trimStr(String str, String indexStr){
if(str == null){
return null;
}
StringBuilder newStr = new StringBuilder(str);
if(newStr.indexOf(indexStr) == 0){
newStr = new StringBuilder(newStr.substring(indexStr.length()));
}else if(newStr.indexOf(indexStr) == newStr.length() - indexStr.length()){
newStr = new StringBuilder(newStr.substring(0,newStr.lastIndexOf(indexStr)));//在结尾
}else if(newStr.indexOf(indexStr) < (newStr.length() - indexStr.length())){
newStr = new StringBuilder(newStr.substring(0,newStr.indexOf(indexStr))
+newStr.substring(newStr.indexOf(indexStr)+indexStr.length(),newStr.length()));
}
return newStr.toString();
}
String str1="/ktms/user/find.jspx";
String str2="/ktms";
String str3="tms";
String str4=".jspx";
System.out.println(this.trimStr(str1, str2));
System.out.println(this.trimStr(str1, str3));
System.out.println(this.trimStr(str1, str4));
![在这里插入图片描述](https://img-blog.csdnimg.cn/fa030b4d8b4f46e8ba576833f8ce7500.png)
stream
Stream 流提供了一个 collect() 方法,可以收集流中的数据到【集合】或者【数组】中去。
//1.收集数据到list集合中
stream.collect(Collectors.toList())
//2.收集数据到set集合中
stream.collect(Collectors.toSet())
//3.收集数据到指定的集合中
stream.collect(Collectors.toCollection(Supplier<C> collectionFactory))
![这是转换的](https://img-blog.csdnimg.cn/4f9771583f1a4e98a4d7cb42c0c9605e.png)
![这是接收的](https://img-blog.csdnimg.cn/0434349ddaf14ecca402bcb94a0a6a88.png)
![这是查询的](https://img-blog.csdnimg.cn/d8133af16a414c769bd3d4431042137d.png)