diff --git "a/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/1.\346\223\215\344\275\234\345\210\206\347\273\204\347\232\204\345\205\203\347\264\240/config.json" "b/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/1.\346\223\215\344\275\234\345\210\206\347\273\204\347\232\204\345\205\203\347\264\240/config.json" deleted file mode 100644 index 2a3402038df3f94e1383b4a032368400cecdd2a5..0000000000000000000000000000000000000000 --- "a/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/1.\346\223\215\344\275\234\345\210\206\347\273\204\347\232\204\345\205\203\347\264\240/config.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "node_id": "java-4cc01469b31d42ac80854c4024c1b15a", - "keywords": [], - "children": [], - "export": [], - "title": "操作分组的元素" -} \ No newline at end of file diff --git "a/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/2.\345\244\232\347\272\247\345\210\206\347\273\204/config.json" "b/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/2.\345\244\232\347\272\247\345\210\206\347\273\204/config.json" deleted file mode 100644 index 587e05eebc42a319cd0d48a8dd92858c53bbba03..0000000000000000000000000000000000000000 --- "a/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/2.\345\244\232\347\272\247\345\210\206\347\273\204/config.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "node_id": "java-873636226f0e44ad9269f2a13fe2adb9", - "keywords": [], - "children": [], - "export": [], - "title": "多级分组" -} \ No newline at end of file diff --git "a/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/3.\346\214\211\345\255\220\347\273\204\346\224\266\351\233\206\346\225\260\346\215\256/config.json" "b/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/3.\346\214\211\345\255\220\347\273\204\346\224\266\351\233\206\346\225\260\346\215\256/config.json" deleted file mode 100644 index 9633c11f5cd54d9896dc6a4ae78589ab3fd4dd33..0000000000000000000000000000000000000000 --- "a/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/3.\346\214\211\345\255\220\347\273\204\346\224\266\351\233\206\346\225\260\346\215\256/config.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "node_id": "java-1223956aa7ae47a9a2e517dc23c3f371", - "keywords": [], - "children": [], - "export": [], - "title": "按子组收集数据" -} \ No newline at end of file diff --git "a/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/group_by.json" "b/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/group_by.json" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/group_by.md" "b/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/group_by.md" new file mode 100644 index 0000000000000000000000000000000000000000..3f3970a5194044b6663698899ff4247a42f24761 --- /dev/null +++ "b/data/3.Java\351\253\230\351\230\266/5.\345\210\206\347\273\204/group_by.md" @@ -0,0 +1,51 @@ +# 分组 + +已知类型 + +```java +public class Employee{ + int id; + String name; + String department; +} +``` + +将 `List employee` 中的员工信息按部门分组,其中不正确的是: + +## 答案 + +```java +// List employee = ...; +Map result = employee.stream().collect(toMap(Employee::getDepartment, Function.identity())); +``` + +## 选项 + +### 标准方法 + +```java +// List employee = ...; +Map> result = employee.stream().collect(groupingBy(Employee::getDepartment)); +``` + +### 朴素方法 + +```java +// List employee = ...; +Map> result = new HashMap<>(); +for(var emp: employee){ + var list = result.getOrDefault(emp.department, new ArrayList<>()); + list.add(emp); +} +``` + +### 朴素方法 + +```java +Map> result = new HashMap<>(); +for(int index=0; index < employee.size(); index++){ + var emp = employee.get(index); + var list = result.getOrDefault(emp.department, new ArrayList<>()); + list.add(emp); +} +```