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

1. 本周学习总结

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

2. 书面作业

1.List中指定元素的删除(题目4-1)

1.1 实验总结:

1.通过equals方法以及list.remove的方法连用实现list中移除掉以与str内容相同的元素的函数。
2.java.util.与java.awt.都是java的标准库包,其中表示java.util和java.awt包里的所有类。其不同之处在于java.util.包含的是一些工具类,如集合类中List、Map、HashMap、Set等,而java.awt.*则封装的是和图形绘制相关的类,如点Point、线Line等。用到相应包里的类时,就会相应import相应的包。
3.list.remove的理解以及掌握。

2.统计文字中的单词数量并按出现次数排序(题目5-3)

2.1 伪代码(简单写出大体步骤)

1.while()循环对每个单词在map中建立键值对应;
2.将map对象转化为list对象;
3.用Collections.sort对list进行排序;
4.输出。

2.2 实验总结

建立键值对应时注意输入单词输入是否为空,若为空则值置为1,若不为空则值累加;

将map对象转变为list对象,用entrySet()同时取得map的键和值;

Collections.sort使用匿名内部类编写。

3.倒排索引(题目5-4)

3.1 截图你的提交结果(出现学号)

3.2 伪代码(简单写出大体步骤)

存放:

while (!lineword.equals("!!!!!")) {  
 String[] words = temp.split(" ");  
    for (String word : words) {  
    if(index.containsKey(word)){
      subIndex=index.get(word); 
    }
    index.put(word, subIndex.add(line););
    }  line++;
   }

查找:

if (temp1.length() == 0) {
            System.out.println("found 0 results");
        }

     if (!index.containsKey(s)) {
           System.out.println("found 0 results");
         } 
         subIndex1 = index.get(s);
     if (subIndex1.size()==0) {
                System.out.println("found 0 results");
            } 
    else {
        System.out.println(subIndex1);
            }

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中调用,然后输出结果。

  public Student bang1()
    {
      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 yes;
        }
        else
        return null;
        }
 }

4.2 使用java8中的stream(), filter(), collect()编写功能同4.1的函数,并测试。

ArrayList<Student> arrayList2 = (ArrayList<Student>) arrayList.parallelStream()
    .filter(student -> (student.getId() > 10L && 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> list = (ArrayList<Student>) stulist.stream().filter(e -> e != null)
                .filter(e -> e.getId() > 10).filter(e -> e.getAge() > 20).filter(e -> e.getGender().equals(Gender.女))
                .filter(e -> e.isJoinsACM()).collect(Collectors.toList());

        list.forEach(System.out::println);

5.泛型类:GeneralStack(题目5-5)

5.1 截图你的提交结果(出现学号)

5.2 GeneralStack接口的代码

    interface GeneralStack <T>
    {
        public  T push(T item);          
        public  T pop();              
        public  T peek();                
        public boolean empty();
        public int size();     
    }

5.3 结合本题,说明泛型有什么好处

1.无需使用有风险的强制转换
2.错误在编译阶段就能发现
3.泛型可以指定集合中元素的类型,得到强类型

6.泛型方法

6.1编写方法max,该方法可以返回List中所有元素的最大值。List中的元素必须实现Comparable接口。编写的max方法需使得String max = max(strList)可以运行成功,其中strList为List

方法代码如下

public class big{
public static <T extends Comparable<T>> T max(List<T>)
{                     
  T max = list.get;
  for (Tt : list) {
    if ( t.compareTo(max) > 0 ){
     max = t; 
  }
 }
  return max; 
} 

posted on 2017-04-15 20:08  呼吸一下广阔的天气  阅读(216)  评论(1编辑  收藏  举报