Ôn tập lập trình Java (MT2 Review Solutions) - Ching and Christines
正在生成预览...
Tài liệu ôn tập giữa kỳ về lập trình Java, bao gồm các bài tập thực hành về streams và iterators.
描述
Midterm 2 Review Solutions Streams Intermediate Operations to Know 1. stream(): Converts a collection into a stream 2. sorted(Comparator c): Sorts the stream using c, which can also be a lambda 3. map(Function f)(): Applies f to each element in the stream 4. filter(Predicate p): Tests each element with p to see if it should remain in the stream Terminal Operations to Know 1. reduce(BinaryOperator accumulator): Applies the accumulator and combines the elements in the stream, two at a time. Returns an Optional. 2. collect(Collector c): Turns the stream into a collection. To a List: Collectors.toList() To a Set: Collectors.toSet() To a Map: Collectors.grooupingBy(Function f) 3. forEach(Consumer action) Practice Problems Given the following classes, write methods to perform the desired operations. You may only use one semicolon per method. public class Student { public int getLabSection() { ... } public int getAge() {...} public Group getGroup {...} } public class Group { public int size() {...} public Group merge(Group other) { ... } } 1 1. Collect all the unique lab sections from a list of students. There should not be duplicates. public Collection<Integer> uniqueSections(List<Student> lst) { return lst.stream() .map(Student::getLabSection) .collect(Collectors.toSet()); } 2. Each TA submits a list of their students. Write a method that will map each lab section to the students in that section whose group is larger than size 4. Assume there is always at least one student in each section whose group size is larger than 4. public Map<Integer, List<Student>> grpMap(List<List<Student>> lst) { return lst.stream() .map(o -> o.stream() .filter(getGroup.size() > 4) .collect(Collectors.toList())) .collect(Collectors.groupingBy( o -> o.get(0).getLabSection())); } 3. Each TA submits a list of their students. Write a method that will get the number of students who are over age 20. public int over20 (List<List<Student>> lst) { return lst.stream() .map(o-> o.stream() .f
AI 摘要
- 文档名称
- Ôn tập lập trình Java (MT2 Review Solutions) - Ching and Christines
- 学校 / 课程
- University of California, Berkeley · Lập trình Java
- 内容
- Tài liệu ôn tập giữa kỳ về lập trình Java, bao gồm các bài tập thực hành về streams và iterators.
- 目录
- 此文档没有清晰的目录。
- 页数
- 11 页
- 上传者
- Uni24h
常见问题
此文档免费吗?
是的。“Ôn tập lập trình Java (MT2 Review Solutions) - Ching and Christines”是免费的 — 只需登录并点击“下载”即可获取原始文件。
这份文档有多少页?
该文档共有 11 页,适用于课程 Lập trình Java。您可以在下载前进行在线预览。
我可以在下载前预览吗?
是的。您可以通过在线阅读器直接在本页面预览此文档,然后再决定是否下载。
Ôn tập lập trình Java (MT2 Review Solutions) - Ching and Christines
正在生成预览...
Midterm 2 Review Solutions Streams Intermediate Operations to Know 1. stream(): Converts a collection into a stream 2. sorted(Comparator c): Sorts the stream using c, which can also be a lambda 3. map(Function f)(): Applies f to each element in the stream 4. filter(Predicate p): Tests each element with p to see if it should remain in the stream Terminal Operations to Know 1. reduce(BinaryOperator accumulator): Applies the accumulator and combines the elements in the stream, two at a time. Returns an Optional. 2. collect(Collector c): Turns the stream into a collection. To a List: Collectors.toList() To a Set: Collectors.toSet() To a Map: Collectors.grooupingBy(Function f) 3. forEach(Consumer action) Practice Problems Given the following classes, write methods to perform the desired operations. You may only use one semicolon per method. public class Student { public int getLabSection() { ... } public int getAge() {...} public Group getGroup {...} } public class Group { public int size() {...} public Group merge(Group other) { ... } } 1 1. Collect all the unique lab sections from a list of students. There should not be duplicates. public Collection<Integer> uniqueSections(List<Student> lst) { return lst.stream() .map(Student::getLabSection) .collect(Collectors.toSet()); } 2. Each TA submits a list of their students. Write a method that will map each lab section to the students in that section whose group is larger than size 4. Assume there is always at least one student in each section whose group size is larger than 4. public Map<Integer, List<Student>> grpMap(List<List<Student>> lst) { return lst.stream() .map(o -> o.stream() .filter(getGroup.size() > 4) .collect(Collectors.toList())) .collect(Collectors.groupingBy( o -> o.get(0).getLabSection())); } 3. Each TA submits a list of their students. Write a method that will get the number of students who are over age 20. public int over20 (List<List<Student>> lst) { return lst.stream() .map(o-> o.stream() .f
阅读全文
- 文档名称
- Ôn tập lập trình Java (MT2 Review Solutions) - Ching and Christines
- 学校 / 课程
- University of California, Berkeley · Lập trình Java
- 内容
- Tài liệu ôn tập giữa kỳ về lập trình Java, bao gồm các bài tập thực hành về streams và iterators.
- 目录
- 此文档没有清晰的目录。
- 页数
- 11 页
- 上传者
- Uni24h
评论 (0)
暂无评论。快来抢沙发吧!
Misc.onclusion (Discussion 14-END) (Ôn tập cấu trúc dữ liệu) - Christine Zhou
Final Review Solutions (Cấu trúc dữ liệu heap, hàng đợi ưu tiên và duyệt đồ thị) - Ching and Christines
Giáo trình Lập trình Java
Inheritance (Discussion 4) (Kế thừa trong Java) - Christine Zhou
Asymptotic Analysis (Discussion 7) (Phân tích tiệm cận) - Christine Zhou
Chương 7.Cơ học lượng tử - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 6.Quang học lượng tử - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 5.Thuyết tương đối - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 4. Tán xạ ánh sáng - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
Chương 3.Phân cực ánh sáng - Vật lý đại cương 3 - TS.Nguyễn Thị Trang
评论 (0)
暂无评论。快来抢沙发吧!