201521123103 《Java学习笔记》 第八周学习总结
一、本周学习总结
1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容。

集合部分:TreeMap实现类:对键值进行排序。
Map的entrySet Set<Map.Entry<K,V>> entrySet()常用于遍历Map的键值对。Entry是Map内的一个接口。
Collections 操作集合的工具类,常见方法:sort 排序;Shuffle 打乱集合中的元素。
1.2 选做:收集你认为有用的代码片段
public class MapTest
{
public static void main(String[] args)
{
Map<String, Employee> staff = new HashMap<String, Employee>();
staff.put("144-25-5464", new Employee("Amy Lee"));
staff.put("567-24-2546", new Employee("Harry Hacker"));
staff.put("157-62-7935", new Employee("Gary Cooper"));
staff.put("456-62-5527", new Employee("Francesca Cruz"));
// print all entries
System.out.println(staff);
// remove an entry
staff.remove("567-24-2546");
// replace an entry
staff.put("456-62-5527", new Employee("Francesca Miller"));
// look up a value
System.out.println(staff.get("157-62-7935"));
// iterate through all entries
for (Map.Entry<String, Employee> entry : staff.entrySet())
{
String key = entry.getKey();
Employee value = entry.getValue();
System.out.println("key=" + key + ", value=" + value);
}
}
}
二、书面作业
1、List中指定元素的删除(题目4-1)
1.1 实验总结

总结:学会使用泛型定义 List<String> List=new ArrayList();以及类型转换;remove方法,在remove的过程当中,删除当前下标为i的元素后,该元素后的所有元素将往前移一格,i--。
2、统计文字中的单词数量并按出现次数排序(题目5-3)
2.1 伪代码(简单写出大体步骤)
(1)创建一个HashMap对象 Map<String,Integer>map=new HashMap<String ,Integer>();这里我限定了键、值类型
(2)在集合中存入指定映射关系if(map.containsKey(x)) map.put(x, map.get(x)+1); if(map.get(x)==null) map.put(x, 1);
(3)输出集合的大小map.size()
(4)遍历集合调用map.entrySet()方法List<Entry<String,Integer>> list =new ArrayList<Entry<String,Integer>>(map.entrySet());看资料说是创建了一个Map集合的映射项集合list
(5)调用Collections.sort()排序
(6)通过Map.Entry接口提供的getValue()方法得到value值,将其进行比较,按题目要求排好if (o1.getValue() != o2.getValue()) {return o2.getValue() - o1.getValue();} else {return o1.getKey().compareTo(o2.getKey());}
(7)输出前十个
2.2 实验总结

总结:这个题目用到好多Map集合中的方法,写的我好心累,但最后写出来特别开心。这个说明老师上课的PPT十分重要。接下来就是把这些方法好好熟练一下。用到的知识点:
1)containsKey(Object key)如果此映射包含指定键的映射关系,则返回true。
2)put(K key,V value)向Map集合存入参数指定的映射关系。
3)get(Object key)根据参数所指定的键对象搜索对应的值对象。
4)通过映射项集合遍历Map集合:map.entrySet()方法将Map集合转换为一个映射集合,获取映射项集合的迭代对象,在每次迭代过程中将获取到的元素保存到Map.Entry类型变量中。
5)通过Map.Entry接口提供的getValue()方法得到value值。
6)Collections.sort()sort(List<T> list, Comparator<? super T> c)。
参考书籍:Java语言基础教程 李东明 张丽娟主编
3、倒排索引(题目5-4)
3.1 截图你的提交结果(出现学号)

3.2 伪代码(简单写出大体步骤)
写了两个循环:第一个是
while (sc.hasNextLine()) {
if ("!!!!!") {
break;
}
else {
存入单词和对应的行数;
}
row++;
}
第二个循环
while (sc.hasNextLine()) {
if (没找到) {
System.out.println("found 0 results");
}
else {
System.out.println(行数);
for each {
System.out.println(整行的单词);
}
}
3.3 实验总结
用到的方法和上题很像,但是不会编写行数的存储。我问的同学,又学到了新的东西。
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中调用,然后输出结果。
代码:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> list=new ArrayList<Student>();
Student she1=new Student(11,"zhang",21,Gender.female,true);
Student she2=new Student(9,"zhang",21,Gender.female,true);
Student she3=new Student(9,"zhang",19,Gender.female,true);
Student she4=new Student(9,"zhang",21,Gender.male,true);
list.add(she1);
list.add(she2);
list.add(she3);
list.add(she4);
for (Student student : list) {
System.out.println(student.find());
}
}
}
public class Student {
private int id;
private String name;
private int age;
private Gender gender;//枚举类型
private boolean joinsACM; //是否参加过ACM比赛
public Student(int id, String name, int age, Gender gender, boolean joinsACM) {
super();
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.joinsACM = joinsACM;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", gender=" + gender + ", joinsACM=" + joinsACM
+ "]";
}
public Student find()
{
if(this.id>10&&this.name.equals("zhang")&&this.age>20&&this.gender==Gender.female&&this.joinsACM)
{
Student she=new Student(this.id,this.name,this.age,this.gender,this.joinsACM);
return she;
}
else
return null;
}
}
运行结果:

4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的函数,并测试。
问同学写出来的
ArrayList<Student> Stu = (ArrayList<Student>) arrayList.parallelStream().filter(student -> (student.getId() > 10 && student.getName().equals("zhang")&& student.getAge() > 20 &&student.getGender().equals(Gender.female)&& student.isJoinsACM())).collect(Collectors.toList());
运行结果:

4.3 构建测试集合的时候,除了正常的Student对象,再往集合中添加一些null,然后重新改写4.2,使其不出现异常。
ArrayList<Student> Stu = (ArrayList<Student>) arrayList.parallelStream().filter(student -> student != null && (student.getId() > 10 && student.getName().equals("zhang")&& student.getAge() > 20 &&student.getGender().equals(Gender.female)&& student.isJoinsACM())).collect(Collectors.toList());
5、泛型类:GeneralStack(题目5-5)
5.1 截图你的提交结果(出现学号)
main函数不会写,运行不出来,每次只能输出Integer Test,然后没了

5.2 GeneralStack接口的代码
public interface GeneralStack<T> {
public T push(T item); //如item为null,则不入栈直接返回null。
public T pop(); //出栈,如为空,则返回null.
public T peek(); //获得栈顶元素,如为空,则返回null.
public boolean empty();//如为空返回true
public int size(); //返回栈中元素数量
}
5.3 结合本题,说明泛型有什么好处
答:泛型可以使我们定义的接口对任何引用类型的数据都适用,也就是说类型可以实现通一个接口若干次,只要每次使用不同的类型参数。
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 GenericMain {
public static <T extends Object & Comparable<T>> T max(List<T> list)
{
T max = list.get(0);
for (T a : list) {
if ( a.compareTo(max) > 0 ){
max = a;
}
}
return max;
}
public static void main(String[] args) {
List<String>strList=new ArrayList<String>();
List<Integer>intList=new ArrayList<Integer>();
strList.add("ab");
strList.add("abc");
strList.add("abcd");
intList.add(1);
intList.add(2);
intList.add(3);
String maxStr = max(strList);
Integer maxInt = max(intList);
System.out.println("String max="+maxStr+","+"Integer max="+maxInt);
}
}
运行结果:

三、码云上代码提交记录及PTA实验总结
题目集:jmu-Java-05-集合
3.1. 码云代码提交记录
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

3.2. PTA实验
函数(4-1),编程(5-3,5-4,5-5)
实验总结已经在作业中体现,不用写。
201521123103 《Java学习笔记》 第八周学习总结的更多相关文章
- 20145213《Java程序设计》第八周学习笔记
20145213<Java程序设计>第八周学习笔记 教材学习内容总结 "桃花春欲尽,谷雨夜来收"谷雨节气的到来意味着寒潮天气的基本结束,气温回升加快.刚出冬的我对于这种 ...
- 20145337《Java程序设计》第八周学习总结
20145337<Java程序设计>第八周学习总结 教材学习内容总结 15.1日志 15.1.1日志API简介 使用日志的起点是logger类,logger实例的创建有许多要处理的要素,必 ...
- 《Java程序设计》第八周学习总结
20145224 <Java程序设计>第八周学习总结 教材学习内容总结 第15章 通用API 15.1.1 日志API简介 ·java.util.logging包提供了日志功能相关类与接口 ...
- 20145236 《Java程序设计》第八周学习总结
20145236 <Java程序设计>第八周学习总结 教材学习内容总结 第十四章 NIO与NIO2 认识NIO NIO使用频道(Channel)来衔接数据节点,在处理数据时,NIO可以让你 ...
- 学号 20175329 2018-2019-3《Java程序设计》第八周学习总结
学号 20175329 2018-2019-3<Java程序设计>第八周学习总结 教材学习内容总结 第十五章 泛型 可以使用"class 名称"声明一个类,为了和普通的 ...
- 20175314 《Java程序设计》第八周学习总结
20175314 <Java程序设计>第八周学习总结 教材学习内容总结 安利一个非常实用的图片处理工具:图片工厂,它具有非常强大的图片批处理能力,比如加水印.降低画质.命名等,不仅如此它还 ...
- 20175126《Java程序设计》第八周学习总结
# 20175126 2016-2017-2 <Java程序设计>第八周学习总结 ## 教材学习内容总结 - 本周学习方式主要为手动敲代码并理解内容学习. - 学习内容为教材第十五章,本章 ...
- 20175234 2018-2019-2 《Java程序设计》第八周学习总结
目录 20175234 2018-2019-2 <Java程序设计>第八周学习总结 教材学习内容总结 15.1泛型 15.2链表 15.3堆栈 15.4散列映射 15.5树集 15.6树映 ...
- 20165235 祁瑛 2018-4 《Java程序设计》第八周学习总结
20165235 祁瑛 2018-4 <Java程序设计>第八周学习总结 教材学习内容总结 操作系统与进程 程序是一段静态的代码,它是应用软件执行的蓝本.进程是程序的一次动态执行过程,它对 ...
- 20172325 2018-2019-2 《Java程序设计》第八周学习总结
20172325 2018-2019-2 <Java程序设计>第八周学习总结 教材学习内容总结 一.堆 1.什么是堆? 具有两个附加属性的一个二叉树. 堆分为小顶堆和大顶堆. 最小堆:对每 ...
随机推荐
- 在Entity Framework 中用 Code First 创建新的数据库
在Entity Framework 中用 Code First 创建新的数据库 (原文链接) 本文将逐步介绍怎样用Code First 创建新数据库,使用在代码中定义类和API中提供的特性(Attri ...
- 【平板电脑模拟器】PC端-Chrome自带的功能
直接说使用方式吧, 启动方法:打开Chrome浏览器--〉F12--〉右下角的齿轮按钮(Settings)--〉选择"Overrides" 然后在"Overrides&q ...
- 剑指offer——矩阵覆盖(斐波那契变形)
****感觉都可以针对斐波那契写一个变形题目的集合了****** 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? cl ...
- python 符合Python风格的对象
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 25.0px Helvetica } 对象表示形式 每门面向对象的语言至少都有一种获取对象的字符串表示形式的 ...
- String、StringBuffer与StringBuilder
String.StringBuffer与StringBuilder之间区别 最近学习到StringBuffer,心中有好些疑问,搜索了一些关于String,StringBuffer,StringBui ...
- Session的引入以及Cookie的不足
一.为什么引入session > Cookie实际上就是一个头. > 服务器会创建Cookie,并且将Cookie以一个响应头的形式发送给浏览器 > 浏览器收到Cook ...
- windows10企业版怎么关闭自动更新
windows10企业版怎么关闭自动更新.. 我之所以选择关闭自动的更新的原因: 1.Windows版本是激活版的不是注册版的<其实我想说的是我用的是盗版的> 2.对于为什么禁止[系统更 ...
- 2_linux 常用基本命令
相信当你看到此帖子时,你已不再是当年那个颓废的你,你一定也在追梦的路上奔跑,那么请留下你的“梦”,让我们用心去交流,好吗? 废话不多说,直接说正事! 一.查看磁盘分区 1.fdisk -l 查看磁盘 ...
- Effective Java通俗理解(下)
Effective Java通俗理解(上) 第31条:用实例域代替序数 枚举类型有一个ordinal方法,它范围该常量的序数从0开始,不建议使用这个方法,因为这不能很好地对枚举进行维护,正确应该是利用 ...
- Servlet的执行流程、生命周期
下面这幅图的Request和Response的箭头方向反了,应该是客户端发出请求,然后web服务器返回响应. servlet生命周期阶段包括初始化.加载.实例化.服务和销毁. 编写Servlet的d ...