201521123102 《Java程序设计》第8周学习总结

1. 本周学习总结

1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容。

2. 书面作业

本次作业题集集合

1.List中指定元素的删除(题目4-1)
1.1 实验总结

在实验中,Scanner的不止是只有System.in(键盘输入之类的),还可以是一个已赋值变量,如:
Scanner in=new Scanner(nextLine);

2.统计文字中的单词数量并按出现次数排序(题目5-3)
2.1 伪代码(简单写出大体步骤)

/先是常规main函数的写法/
用while(!str.equals("!!!!!"))判断输出条件。
建立一个ArrayList数组列表
Collections.sort(arrayList, new Comparator<Map.Entry<String, Integer>>() //用collections.sort方法来实现降序输出
关键代码为if(o2.getValue() - o1.getValue()!=0)
return o2.getValue()- o1.getValue();
return o1.getKey().compareTo(o2.getKey());
Map.Entry<String, Integer> entry :arrayList //用map.entry来实现迭代输出每个key得到的每个value

3.倒排索引(题目5-4)
3.2 伪代码(简单写出大体步骤)

//定义map(存放单词+出现该单词的行号),set(单词出现的所有行号)和ArrayList(每一行字符串)
Map<String, Set> map = new TreeMap<String, Set>();
ArrayList lines = new ArrayList();
Set index = new TreeSet();
lines.add(strline1);
//将每一行根据空格把单词提取出来放到字符串数组中
String[] words = strline1.split(" ");
//保存行号和字符串的过程
for (String word : words)
if (!map.containsKey(word)) {
if (!map.containsKey(word)) {
index.add(row);
map.put(word, index);
} else {
index = map.get(word);
if (!index.contains(row)) {
index.add(row);
map.put(word, index);
}
}
//输出所有单词及出现的所有行号
for (Map.Entry<String, Set> e : map.entrySet()) {
System.out.println(e.getKey() + "=" + e.getValue());
}

**4.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中调用,然后输出结果。
for(i=0;i<list.size();i++){
if((list.get(i).getId()>10)&&(list.get(i).getName().equals("zhang"))&&(list.get(i).getAge()>20)&&(list.get(i).getGender().equals(getGender(2)))&&(list.get(i).isJoinsACM()==true)){
list2.add(list.get(i));//id>10,name为zhang, age>20, gender为女,参加过ACM比赛的学生加入list2
}
}
输出结果:

4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的函数,并测试。
list2=list.stream().filter(a->10<a.getId()).collect(Collectors.toList());//id>10
list2=list2.stream().filter(a->"zhang".equals(a.getName())).collect(Collectors.toList());//name为zhang
list2=list2.stream().filter(a->20<a.getAge()).collect(Collectors.toList());//age>20
list2=list2.stream().filter(a->getGender(2).equals(a.getGender())).collect(Collectors.toList());//gender为女
list2=list2.stream().filter(a->a.isJoinsACM()==true).collect(Collectors.toList());//参加过ACM比赛

4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,然后重新改写4.2,使其不出现异常。
list.add(null);//增加null
list.add(new Student(10L,"zheng",15,getGender(2),true));
list.add(new Student(20L,"ling",25,getGender(1),false));
list.add(new Student(1L,"wang",20,getGender(1),true));
list.add(new Student(25L,"zhang",25,getGender(2),true));
list.add(new Student(20L,"zhang",20,getGender(1),false));
list=list.stream().filter(a->a!=null).collect(Collectors.toList());//排除null

5.泛型类:GeneralStack(题目5-5)
5.2 GeneralStack接口的代码

interface GeneralStack {
public T push(T item);

public T pop();

public T peek();

public boolean empty();

public int size();

}
5.3 结合本题,说明泛型有什么好处
不需要使用有风险的强制类型转换
泛型允许指定集合中元素的类型(T->Integer,Double,Car),即使是自定义的类型也适用,比如Car
错误在编译阶段就能发现,而不用等到运行时才发现出错,跟强制转换有关,对比集合跟加有优势
6.泛型方法
基础参考文件GenericMain,在此文件上进行修改。
6.1 编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List类型。也能使得Integer maxInt = max(intList);运行成功,其中intList为List类型。

import java.util.ArrayList;
import java.util.List;

public class Main {
public static <T extends Comparable> T max(List list)
{
T max = list.get(0);
for (T t : list) {
if ( t.compareTo( max ) > 0 ){
max = t;
}
}
return max;
}
public static void main(String[] args) {
ListstrList=new ArrayList();
strList.add("q");
strList.add("z");
strList.add("w");
String max = max(strList);
System.out.println("String max="+max);
}
}

原文地址:https://www.cnblogs.com/hyy786030686/p/6715432.html