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.什么是堆? 具有两个附加属性的一个二叉树. 堆分为小顶堆和大顶堆. 最小堆:对每 ...
随机推荐
- cobbler无人值守批量安装Linux系统
本文目录: 1.1 pxe安装系统 1.2 cobbler基本介绍 1.3 安装和配置cobbler 1.3.1 安装cobbler 1.3.2 配置dhcp和tftp 1.4 cobbler从本地光 ...
- LinkQueue(链队列)
关于Node.h,请参考LinkStack #include"Node.h" template<typename ElemType> class LinkQueue { ...
- 某安寿险HR核人面试问题总结,技术面波澜不惊,HR面反而有被暴击感
之前朋友推荐了某安寿险的技术类职位.经过IQ/EQ网上笔试,技术面之前的java基础笔试和技术面试,基本上没什么意外的.昨天晚上7点安排的HR核人面试(他们的人力部门的工作负荷比较大,目前这个部门处于 ...
- ETL作业调度工具TASKCTL的两个重大突破
在传统设计理念下,流程图的可视化.作业流的定义设计功能,随着作业量增加,越来越难用,越来越不适用是一个难以避免的问题.就这两个问题,我给大家分享一下TASKCTL是如何转变思路.如何突破.如何带来一些 ...
- SVN:Cleanup failed to process the following paths
频繁使用SVN,于是乎玩坏了.用了一下clearup,结果爆了如题错误.查了一下,是有文件被加锁了,位置在项目根目录 .svn下的wc.db 里,需用专门工具才能看到里面.就是个数据库,里面有很多表. ...
- 【转载】跟我一起学习VIM - vim插件
目录 写在前面:Life Changing Editor 什么是VIM 为什么选VIM 为什么选其它 为什么犹豫选择它们 VIM >= SUM(现代编辑器) 如何学习VIM 一秒钟变记事本 VI ...
- 【社交系统ThinkSNS+研发日记三】基于 Laravel Route 的 ThinkSNS+ Component
[社交系统ThinkSNS+研发日记系列] 一.<ThinkSNS+ 基于 Laravel master 分支,从 1 到 0,再到 0.1> 二.<基于 Laravel 开发 Th ...
- re模块和正则表达式
re模块 讲正题之前我们先来看一个例子:https://reg.jd.com/reg/person?ReturnUrl=https%3A//www.jd.com/ 这是京东的注册页面,打开页面我们就看 ...
- OpenStack(企业私有云)万里长征第六步——OpenStack网络及虚拟机存储位置
一.前言 昨天又装了一遍OpenStack.码农这项工作就如同人生,永远有你想不到的意外在等着你,时而是惊喜时而是悲伤.在装的过程中倒是很顺利,只是在安装完成之后碰到了两个之前没有碰到的问题,这里记录 ...
- ueditor单独调用上传附件和图片的功能
javascript富文本编辑器使我们添加.编辑网站中的文章更加方便和容易.这些富文本编辑器提供了所见即所得(What You See Is What You Get - WYSIWYG)的功能,可以 ...