1. 本周学习总结
1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容。
1.2 选做:收集你认为有用的代码片段
对Map进行排序
List<Entry<String,Integer>> list =new ArrayList<Entry<String,Integer>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,Map.Entry<String, Integer> o2) {
if (o1.getValue() != o2.getValue()) {
return o2.getValue() - o1.getValue();
}
else {
return o1.getKey().compareTo(o2.getKey());
}
}
});
2. 书面作业
本次作业题集集合
1. List中指定元素的删除(题集题目)
1.1 实验总结。并回答:列举至少2种在List中删除元素的方法。
用String类spilt()方法判断空格并分开字符,删除元素后,被删除元素后面的元素需要前移一格。
在List中删除元素的方法:
1.使用循环和判断语句删除,删除后下标自减
2.通过Iterator进行遍历删除符合条件的多个元素
2. 统计文字中的单词数量并按出现次数排序(题集题目)
此题上周的实验总结2-6中已总结:http://www.cnblogs.com/Yelz/p/7809242.html
3. 倒排索引(题集题目)
本题较难,做不出来不要紧。但一定要有自己的思考过程,要有提交结果。
3.1 截图你的代码运行结果
3.2 伪代码(不得复制代码,否则扣分)
create a treemap to keep word in key and Set<line> in value;
add words and lines to TreeMap;
input words to search in TreeMap;
if(TreeMap.contains(word))
output line and String in line;
else
output "found 0 results";
3.3 实验总结
注意输出查找一个单词和一个语句的功能,处理输入时先用nextLine方法读取一行,再以空格作为分隔逐个读取单词
4.Stream与Lambda
编写一个Student类,属性为:
private Long id;
private String name;
private int age;
private Gender gender;//枚举类型
private boolean joinsACM; //是否参加过ACM比赛
创建一集合对象,如List,内有若干Student对象用于后面的测试。
4.1 使用传统方法编写一个搜索方法
List<Student> search(List<Student> stuList, Long id, String name, int age, Gender gender, boolean joinsACM)
,然后调用该方法将id>某个值,name为某个值, age>某个值, gender为某个值,参加过ACM比赛的学生筛选出来,放入新的集合。在main中调用,然后输出结果。(截图:出现学号、姓名)
方法体:
public static List<Student> search(List<Student> stuList, Long id, String name, int age, Gender gender, boolean joinsACM)
{
List<Student> students = new ArrayList<Student>();
for(Student i : stuList)
{
if(i == null)
continue;
if(i.getId() > id && i.getName().equals(name) && i.getAge() > age && i.getGender().equals(gender) && i.isJoinsACM() == joinsACM)
students.add(i);
}
return students;
}
测试数据:
Student stu1 = new Student(201621121234l,"学生1",15,Gender.MALE,true);
Student stu2 = new Student(201621121235l,"学生2",16,Gender.MALE,false);
Student stu3 = new Student(201621121236l,"学生1",17,Gender.FEMALE,true);
Student stu4 = new Student(201621121237l,"学生2",18,Gender.FEMALE,false);
Student stu5 = new Student(201621121238l,"学生1",19,Gender.MALE,true);
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
stuList.add(stu5);
调用方法:
searchStuListNormal = search(stuList,201621121233l,"学生1",16,Gender.MALE,true);
输出结果:
4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的代码,并测试(要出现测试数据)。构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,你编写的方法应该能处理这些null而不是抛出异常。(截图:出现学号)
测试数据加入null:
Student stu1 = new Student(201621121234l,"学生1",15,Gender.MALE,true);
Student stu2 = new Student(201621121235l,"学生2",16,Gender.MALE,false);
Student stu3 = new Student(201621121236l,"学生1",17,Gender.FEMALE,true);
Student stu4 = new Student(201621121237l,"学生2",18,Gender.FEMALE,false);
Student stu5 = new Student(201621121238l,"学生1",19,Gender.MALE,true);
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
stuList.add(stu5);
stuList.add(null);
search实现语句:
searchStuListByStream = stuList.stream()
.filter(Student ->
Student != null && Student.getId() > 201621121233l && Student.getName().equals("学生1")
&& Student.getAge() > 16 && Student.getGender().equals(Gender.MALE)&& Student.isJoinsACM() == true)
.collect(Collectors.toList());
输出结果:
5. 泛型类:GeneralStack
题集jmu-Java-05-集合之GeneralStack
5.1 GeneralStack接口的代码
interface GeneralStack<E>
{
E push(E item);
E pop();
E peek();
public boolean empty();
public int size();
}
5.2 结合本题与以前作业中的ArrayListIntegerStack相比,说明泛型有什么好处
以前的ArrayListIntegerStack只能存Integer类型元素,想要存其他类型元素就要把相应代码进行修改,十分麻烦且易出错。使用泛型可以实现代码复用而无视类型,大大提高效率减少代码量。
6. 选做:泛型方法
基础参考文件GenericMain,在此文件上进行修改。
6.1 编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List<String>类型
。也能使得Integer maxInt = max(intList);运行成功,其中intList为List类型。注意:不得直接调用Collections.max函数。
测试数据:
List<String> strList = new ArrayList<String>();
List<Integer> intList = new ArrayList<Integer>();
strList.add("alkdshfb");
strList.add("hkfhwhd");
strList.add("ohjagd");
intList.add(123);
intList.add(456);
intList.add(789);
max方法:
public static <T extends Comparable<T>> T max(List<T> list) {
T max = list.get(0);
for (T i : list) {
if (i.compareTo(max) > 0) {
max = i;
}
}
return max;
}
运行结果:
6.2 选做:现有User类,其子类为StuUser,且均实现了Comparable接口。编写方法max1,基本功能同6.1,使得User user = max1(stuList);可以运行成功,其中stuList为List类型。也可使得Object user = max(stuList)运行成功。
测试数据:
List<StuUser> stuList = new ArrayList<StuUser>();
StuUser stu1 = new StuUser(18,"201621123063");
StuUser stu2 = new StuUser(19,"201621123062");
StuUser stu3 = new StuUser(20,"201621123065");
StuUser stu4 = new StuUser(21,"201621123061");
stuList.add(stu1);
stuList.add(stu2);
stuList.add(stu3);
stuList.add(stu4);
User user = max1(stuList);
Object user1 = max(stuList);
System.out.println(user.toString());
System.out.println(user1.toString());
max1方法:
public static <T extends Comparable<? super T>> T max1(List<? extends T> list )
{
T max = list.get(0);
for(T i : list)
{
if(i.compareTo(max) > 0)
{
max = i;
}
}
return max;
}
输出:
6.3 选做:编写int myCompare(T o1, T o2, Comparator c)方法,该方法可以比较两个User对象,也可以比较两个StuUser对象,传入的比较器c既可以是Comparator,也可以是Comparator。注意:该方法声明未写全,请自行补全。
测试数据:
StuUser stu1 = new StuUser(18,"201621123063");
StuUser stu2 = new StuUser(19,"201621123062");
User user1 = new User(20);
System.out.println(myCompare(stu1,stu2,new StuUserComparator()));
System.out.println(myCompare(stu1,user1,new UserReverseComparator()));
myCompare方法:
public static <T> int myCompare (T o1, T o2, Comparator<T> c){
return c.compare(o1, o2);
}
输出结果:
7. 选做:逆向最大匹配分词算法
集合实验文件中的第07次实验(集合).doc文件,里面的题目6.
7.1 写出伪代码(不得直接复制代码)
input dictionary
add each word in dictionary to a HashSet
for(right=string.end)
for(current length from n-- to 1)
substring the longest word from right to left
add words to list
ouput list
7.2 截图你的代码运行结果。
3.码云及PTA
题目集:jmu-Java-05-集合
3.1. 码云代码提交记录
3.2 截图PTA题集完成情况图
3.3 统计本周完成的代码量
周次 | 行数 | 新增行数 | 文件数 | 新增文件数 |
---|---|---|---|---|
1 | 226 | 226 | 45 | 45 |
2 | 377 | 377 | 7 | 7 |
3 | 712 | 281 | 42 | 9 |
4 | 166 | 166 | 2 | 2 |
5 | 253 | 253 | 4 | 4 |
6 | 484 | 484 | 9 | 9 |
7 | 269 | 269 | 1 | 1 |
8 | 301 | 32 | 5 | 4 |
9 | 447 | 447 | 7 | 7 |
4. 评估自己对Java的理解程度
维度 | 程度 |
---|---|
语法 | PTA上的题目基本没问题,常用语法基本无障碍使用,有些较偏的语法仍需要通过百度得知 |
面向对象设计能力 | 这方面能力还比较弱 |
应用能力 | 还未尝试编写小工具 |
至今为止代码行数 | 3235 |
选做:5.使用Java解决实际问题
有n门课程,每个学生对每门课程都有几个不懂的问题(每题都有标号)。教师期望对所有学生的问题进行归类,首先对问题按课程分类,在某类中又将同一个学生的题目归类在一起。现有的操作流程,是每个学生把自己的各科目中不懂得题目按课程分类号后发给学习委员,学习委员进行统一汇总。现在希望编写一个程序,帮助学习委员分类,并统计每门课程中哪些题目不懂率最高。尝试写出解决该问题的大概步骤?每个学生发给学习委员的文件内容应遵循一定规范方便程序处理,尝试写出该规范。
输入规范:
姓名
课程名 若干题号(以空格隔开)
课程名 若干题号(以空格隔开)
课程名 若干题号(以空格隔开)
大概步骤:
①读取输入,每个学生之间的信息用"end"
隔开,所有输入结束的标识为"ENDINPUT"
②使用Map<int,List<String>>
存放信息,每门课使用不同的map
保存,题目编号存入key
,学生姓名存入Value
③比较Value
中List
的size
大小可得此题不懂的人数,从而算出不懂率
运行截图: