201521145048《Java程序设计》第8周学习总结
1. 本周学习总结
1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容。
1.2 选做:收集你认为有用的代码片段
1.2
List<Map.Entry<String, Integer>> entryList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
// TODO Auto-generated method stub
return (o2.getValue() - o1.getValue());
}
});
2. 书面作业
Q1、List中指定元素的删除(题目4-1)
1.1 实验总结
答:该试验要求我们编写两个函数convertStringToList与remove。convertStringToList中是实现的功能是让输入值转换为List<String>,我用的方法是建立个Scanner扫描器,将输入值加入到List<String>即可。remove函数中中容易出错,会使实际删除的值不是我们想要的,位置i的不同,为了避免这种情况,可以使用迭代器依次访问集合中的元素。
Q2、统计文字中的单词数量并按出现次数排序(题目5-3)
2.1 伪代码(简单写出大体步骤)
2.2 实验总结
2.1
(1)建立Map(key,value),key类型String,value类型Integer
(2)输入文本, str=in.next()
(3)比较,若是出现过单词(即key),次数+1(value+1),否则加入单词,次数=1
(4)将map对象转化为list对象;
(5)用Collections.sort对list进行排序,输出;
2.2
答:将map转换为list是之前遇到的瓶颈,需要用entrySet()同时取得map的键和值,然后用Collections.sort的匿名内部类进行对list的排序。
Q3、倒排索引(题目5-4)
**3.1 截图你的提交结果(出现学号)
3.2 伪代码(简单写出大体步骤)
(1)建立TreeMap<String, Set<Integer>>索引
(2)用while判断,存放数据
(3)whlie()循环解决匹配查找问题
3.3 实验总结**
答:难度好大,参考同学代码后完成,对map的用法不熟悉,不知如何存放,如题中开始建立索引列表就出现问题。还会遇到单词查找不到的情况。
Q4、Stream与Lambda
编写一个Student类,属性为:
private Long id;
private String name;
private int age;
private Gender gender;//枚举类型
private boolean joinsACM; //是否参加过ACM比赛
创建一集合对象,如List,内有若干Student对象用于后面的测试。
4.1 使用传统方法编写一个方法,将id>10,name为zhang, age>20, gender为女,参加过ACM比赛的学生筛选出来,放入新的集合。在main中调用,然后输出结果。
4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的函数,并测试。
4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,然后重新改写4.2,使其不出现异常。
4.1
public static ArrayList<Student> choose(ArrayList<Student> arrayList) {
ArrayList<Student> aList = new ArrayList<Student>();
for (Student student : arrayList) {
if (student.getId() > 10L && student.getName().equals("zhang")
&& student.getAge() > 20 &&
student.getGender().equals(Gender.女)
&& student.isJoinsACM()) {
aList.add(student);
}
}
return aList2、;
}
输出:
4.2
ArrayList<Student> aList = (ArrayList<Student>) arrayList.parallelStream()
.filter(student -> (student.getId() > 10L && student.getName().equals("zhang")
&& student.getAge() > 20 &&
student.getGender().equals(Gender.女)
&& student.isJoinsACM()))
.collect(Collectors.toList());
输出:
4.3
ArrayList<Student> arrayList2 = (ArrayList<Student>) arrayList.parallelStream()
.filter(student -> student != null && (student.getId() > 10L && student.getName().equals("zhang")
&& student.getAge() > 20 &&
student.getGender().equals(Gender.female)
&& student.isJoinsACM()))
.collect(Collectors.toList());
输出:
Q5、泛型类:GeneralStack(题目5-5)
5.1 截图你的提交结果(出现学号)
5.2 GeneralStack接口的代码
5.3 结合本题,说明泛型有什么好处
5.1
编译错误
5.2
interface GeneralStack<E> {
E push(E e);
E pop();
E peek();
boolean empty();
int size();
}
5.3
答:定义类时,不必直接就决定类的类型,在main中调用时,可以创建多个不同类型的类。就拿5-5题目来说,对于Integer, Double, Car三个引用类型的栈,它们的基本操作都是push(item),pop(),peek(),public boolean empty(),public int size()。如果没有使用泛型接口就要写三个不同类型的栈,代码就会很长。同时消除代码中的很多强制转换,减少出错,安全性提高
Q6、泛型方法
基础参考文件GenericMain,在此文件上进行修改。
6.1 编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List
6.2 选做:现有User类,其子类为StuUser,且均实现了Comparable接口。编写方法max1,基本功能同6.1,并使得max1(stuList);可以运行成功,其中stuList为List
6.3 选做:编写int myCompare(T o1, T o2, Comparator c)方法,该方法可以比较User对象及其子对象,传入的比较器c既可以是Comparator
6.1
class Max {
public static <T extends Comparable> T max(List<T> strList){
T maxest =strList.get(0);
for(int i = 1;i < strList.size(); i++){
if ((maxest.compareTo(strList.get(i)) < 0))
maxest = strList.get(i);
}
return maxest;
}
}//该方法可以返回List中所有元素的最大值
public static void main(String[] args) {
List<String>strList=new ArrayList<String>();
List<Integer>intList=new ArrayList<Integer>();
strList.add("a");
strList.add("b");
strList.add("c");
intList.add(1);
intList.add(2);
intList.add(3);
String max = max(strList);
Integer maxInt = max(intList);
System.out.println("String max="+max+"; "+"Integer max="+maxInt);
}
输出结果为:
String max=c;Integer max=3
3. 码云上代码提交记录及PTA实验总结
3.1. 码云代码提交记录
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图
3.2. PTA实验
函数(4-1),编程(5-3,5-4,5-5)
实验总结已经在作业中体现,不用写。