将List处理成Map - #225
Conversation
3stonelei
commented
Jun 4, 2022
- 这个PR解答了当前仓库中的题目(机器人会自动判题并合并当前PR)
- 这个PR修复了当前仓库中的一些代码缺陷(机器人不会判题,而是由管理员来处理当前PR)
|
🎉 感谢提交Pull Request!请稍等片刻,我们已经将其提交到CI进行检查,一旦有结果会立即通知您! |
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.*; |
There was a problem hiding this comment.
不应使用 '.*' 形式的导入 - java.util.* 。
| // 市场部 -> [{name=王五, department=市场部, age=40 }] | ||
| public static Map<String, List<User>> collect(List<User> users) {} | ||
| public static Map<String, List<User>> collect(List<User> users) { | ||
| Map<String,List<User>> userListByDepartment=new HashMap<>(); |
There was a problem hiding this comment.
',' 后应有空格。
'=' 后应有空格。
'=' 前应有空格。
| Map<String,List<User>> userListByDepartment=new HashMap<>(); | ||
| for (User user:users | ||
| ) { | ||
| if (!userListByDepartment.containsKey(user.getDepartment())){ |
| for (User user:users | ||
| ) { | ||
| if (!userListByDepartment.containsKey(user.getDepartment())){ | ||
| userListByDepartment.put(user.getDepartment(),new ArrayList<>()); |
| userListByDepartment.get(user.getDepartment()).sort(new Comparator<User>() { | ||
| @Override | ||
| public int compare(User o1, User o2) { | ||
| if (o1.getAge()==o2.getAge()){ |
There was a problem hiding this comment.
'==' 后应有空格。
'==' 前应有空格。
'{' 前应有空格。
|
|
||
| @Override | ||
| public String toString() { | ||
| return "User{" + |
| @Override | ||
| public String toString() { | ||
| return "User{" + | ||
| "id=" + id + |
| public String toString() { | ||
| return "User{" + | ||
| "id=" + id + | ||
| ", name='" + name + '\'' + |
| return "User{" + | ||
| "id=" + id + | ||
| ", name='" + name + '\'' + | ||
| ", age=" + age + |
| "id=" + id + | ||
| ", name='" + name + '\'' + | ||
| ", age=" + age + | ||
| ", department='" + department + '\'' + |
|
你的提交 b33827b ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 b33827b ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| for (User user:users | ||
| ) { | ||
| List<User> userList; | ||
| if (userListByDepartment.containsKey(user.getDepartment())){ |
| ) { | ||
| List<User> userList; | ||
| if (userListByDepartment.containsKey(user.getDepartment())){ | ||
| userList=userListByDepartment.get(user.getDepartment()); |
| List<User> userList; | ||
| if (userListByDepartment.containsKey(user.getDepartment())){ | ||
| userList=userListByDepartment.get(user.getDepartment()); | ||
| }else { |
| if (userListByDepartment.containsKey(user.getDepartment())){ | ||
| userList=userListByDepartment.get(user.getDepartment()); | ||
| }else { | ||
| userList=new ArrayList<>(); |
| return (o1.getAge()>o2.getAge())?1:-1; | ||
| } | ||
| }); | ||
| userListByDepartment.put(user.getDepartment(),userList); |
|
|
||
| @Override | ||
| public String toString() { | ||
| return "{" + |
|
你的提交 3e3191c ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 3e3191c ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| Map<String,List<User>> userListByDepartment=new HashMap<>(); | ||
| for (User user:users | ||
| ) { | ||
| String departmentKey=user.getDepartment(); |
| for (User user:users | ||
| ) { | ||
| String departmentKey=user.getDepartment(); | ||
| if (!userListByDepartment.containsKey(departmentKey)){ |
| ) { | ||
| String departmentKey=user.getDepartment(); | ||
| if (!userListByDepartment.containsKey(departmentKey)){ | ||
| userListByDepartment.put(departmentKey,new ArrayList<>()); |
| import java.util.Objects; | ||
|
|
||
| public class User { | ||
| public class User implements Comparable{ |
|
|
||
| @Override | ||
| public int compareTo(Object o) { | ||
| if (this.getAge()==((User) o).getAge()){ |
There was a problem hiding this comment.
'==' 后应有空格。
'==' 前应有空格。
'{' 前应有空格。
| if (this.getAge()==((User) o).getAge()){ | ||
| return 0; | ||
| } | ||
| return (this.getAge()>((User) o).getAge())?1:-1; |
There was a problem hiding this comment.
'>' 后应有空格。
'>' 前应有空格。
'?' 后应有空格。
'?' 前应有空格。
':' 后应有空格。
':' 前应有空格。
|
你的提交 aa1c60b ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 aa1c60b ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| import java.util.Objects; | ||
|
|
||
| public class User { | ||
| public class User{ |
|
你的提交 939ed00 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 939ed00 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
|
你的提交 7791049 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 7791049 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| } | ||
| userListByDepartment.get(departmentKey).add(user); | ||
| } | ||
| for (List<User> list:userListByDepartment.values()){ |
|
你的提交 1876694 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 1876694 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| import java.util.Objects; | ||
|
|
||
| public class User { | ||
| public class User implements Comparable<User>{ |
|
你的提交 2079a29 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 2079a29 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
|
|
||
| @Override | ||
| public String toString() { | ||
| return "{" + |
|
你的提交 1ac6949 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 1ac6949 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| public String toString() { | ||
| return "{" + | ||
| ", name='" + name + '\'' + | ||
| ", department='" + department + '\'' +", age=" + age + |
|
你的提交 fa8051f ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 fa8051f ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| @Override | ||
| public String toString() { | ||
| return "{" + | ||
| "name='" + name + '\'' + |
|
你的提交 c5bac36 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 c5bac36 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| ) { | ||
| String departmentKey=user.getDepartment(); | ||
| List userList; | ||
| if (userListByDepartment.containsKey(departmentKey)){ |
| String departmentKey=user.getDepartment(); | ||
| List userList; | ||
| if (userListByDepartment.containsKey(departmentKey)){ | ||
| userList=userListByDepartment.get(departmentKey); |
| return u1.getAge() - u2.getAge(); | ||
| } | ||
| }); | ||
| userListByDepartment.put(departmentKey,userList); |
|
你的提交 1992e24 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
1 similar comment
|
你的提交 1992e24 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |