foreword
You and the excellent people just lacked some tools before. Sometimes you worked hard for several days and found that others can use the tools to get it done. With the rise of various AI tools, we need to pay more attention to those that can improve Our work efficiency tool, this article introduces an Idea plug-in, a tool that can greatly improve your editing efficiency.
installation method
After restarting, click on the one on the right side of Idea </>
, click and output the email address as required, and register after receiving the verification code. There is no need for a "code receiving platform", and you can register and log in like a fool. After the account is registered, you can enter the conversation. There is a question here. I asked which GPT version it uses, and it said it was GPT-3, which feels a bit strange, but it clearly says GPT4.
synthetic tree test
For example, we often write and synthesize various trees. We can use recursion or loop. People who don’t write often may make mistakes. Let’s test it to see if it can be done.
question:
public class Menu {
private Long id;
private Long parentId;
private String name;
private List<Menu> subMenus;
}
现在有个 List<Menu>menus对象,请帮我parentId是其父id,请将这些对象合成一个树,将子menus放入subMenus对象中
The generated code, with a little modification, will be through the
package com.test.jdk11.testunicode;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Description: TODO
* @Author 赵侠客
* @Date 2023/7/28
**/
public class Menu {
private Long id;
private Long parentId;
private String name;
private List<Menu> subMenus;
public static void main(String[] args) {
List<Menu> menus=new ArrayList<>();
menus.add(new Menu(1L,null,"一级"));
menus.add(new Menu(2L,1L,"一级"));
menus.add(new Menu(3L,1L,"一级"));
menus.add(new Menu(4L,3L,"一级"));
menus.add(new Menu(5L,4L,"一级"));
List<Menu> tree = buildTree(menus);
System.out.println(JSON.toJSONString(tree));
}
public static List<Menu> buildTree(List<Menu> menus) {
Map<Long, Menu> menuMap = new HashMap<>();
List<Menu> rootMenus = new ArrayList<>();
// 将所有的菜单对象放入 map 中,以便后续构建树形结构
for (Menu menu : menus) {
menuMap.put(menu.getId(), menu);
}
// 遍历菜单对象,将子菜单放入对应的父菜单的 subMenus 属性中
for (Menu menu : menus) {
Long parentId = menu.getParentId();
if (parentId == null) {
// 如果 parentId 为空,则将该菜单对象作为根节点
rootMenus.add(menu);
} else {
// 如果 parentId 不为空,则将该菜单对象放入对应的父菜单的 subMenus 中
Menu parentMenu = menuMap.get(parentId);
if (parentMenu != null) {
if (parentMenu.getSubMenus() == null) {
parentMenu.setSubMenus(new ArrayList<>());
}
parentMenu.getSubMenus().add(menu);
}
}
}
return rootMenus;
}
}
Test lambda expressions
Test questions:
public class TenantBucketSize {
private Long tenantId;
private String day;
private Long size;
}
现有List<TenantBucketSize >请根据tenantId分组后再根据day分组按day正序,最后将size求和,
The generated code is also passed
public class TenantBucketSize {
private Long tenantId;
private String day;
private Long size;
public static Map<Long, Map<String, Long>> calculateTotalSize(List<TenantBucketSize> tenantBucketSizes) {
return tenantBucketSizes.stream()
.collect(Collectors.groupingBy(TenantBucketSize::getTenantId,
Collectors.groupingBy(TenantBucketSize::getDay,
Collectors.summingLong(TenantBucketSize::getSize))));
}
public static void main(String[] args) {
List<TenantBucketSize> tenantBucketSizes=new ArrayList<>();
tenantBucketSizes.add(new TenantBucketSize(1L,"2023-07-08",1L));
tenantBucketSizes.add(new TenantBucketSize(1L,"2023-07-08",2L));
tenantBucketSizes.add(new TenantBucketSize(1L,"2023-07-09",3L));
tenantBucketSizes.add(new TenantBucketSize(2L,"2023-07-08",6L));
tenantBucketSizes.add(new TenantBucketSize(3L,"2023-07-09",8L));
System.out.println(JSON.toJSONString(calculateTotalSize(tenantBucketSizes)));
}
regular expression generation
Everyone should know a little about regular expressions, but when you really want to write them yourself, most of the more complex expressions still need to be searched
Use JAVA to write a method, receive String, return Boolen, use regular expressions to detect that the password length must be greater than 8, and contain numbers, letters, and special strings, and add comments to explain the regularity
Generate random numbers
Use JAVA to write a method to generate random numbers, receive an Int parameter to return the length of the random number, and return String, which requires numbers, letters, and special strings
It's a pity that this test case failed. The generated random number password is correct, but the code is wrong.
public static void main(String[] args) {
for(int i=9;i<15;i++){
for(int j=0;j<100;j++){
String password=generateRandomString(i);
System.out.println(password+":密码强:"+validatePassword(password));
}
}
}
private static final String CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()";
public static boolean validatePassword(String password) {
// 正则表达式解释:
// ^ 开始
// (?=.*[0-9]) 必须包含至少一个数字
// (?=.*[a-zA-Z]) 必须包含至少一个字母
// (?=.*[@#$%^&+=]) 必须包含至少一个特殊字符(可以根据需要添加或修改特殊字符)
// (?=\S+$) 不包含空格
// .{8,} 长度至少为8个字符
// $ 结束
String regex = "^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[@#$%^&+=]).{8,}$"; return Pattern.matches(regex, password);
}
public static String generateRandomString(int length) {
SecureRandom random = new SecureRandom();
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(CHARACTERS.length());
char randomChar = CHARACTERS.charAt(randomIndex);
sb.append(randomChar);
}
return sb.toString();
}