1.练习1:输出一句话中每个单词或汉字的出现的次数:(采用分拣的思路)
package HashMapPractice; public class Letter { private int count; public Letter () { } public Letter(int count) { super(); this.count = count; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } }
package HashMapPractice; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * 统计这句话中单词出现的次数 * The early birds catch the worm Twenty years old is not the years to be happy * * 存储到Map中 * key:String * value:自定义类型 * * 分拣思路: * 方法1.为所有的key(不同的单词)创建容器 * 之后容器中存放对应的Value * * 方法2:第一次为所有的单词创建容器并存放值 * 第二次直接使用容器存放值 * * @author Wang * */ public class Practice01 { public static void main(String[] args) { String str = "The early birds catch the worm Twenty years old is not the years to be happy"; String[] strArray = str.split(" ");//将字符串以空格为界限 进行分割; /* for(String temp : strArray) {//这个for-each或者叫增强for 是直接遍历数组里面的元素 而不是通过index遍历的 跟迭代器的原理类似 System.out.println(temp); }//检测一下是否分割了 */ /* Map <String,Letter> map = new HashMap <String,Letter> ();//创建一个map for(String temp : strArray){ if(!map.containsKey(temp)) {//如果map里面没有包含这个单词的的容器Letter就创建一个 map.put(temp,new Letter()); } } for(String temp : strArray) { Letter let = map.get(temp);//把创建好的容器Letter拿出来; let.setCount(let.getCount() + 1);//对存放单词的数组进行遍历 出现相同的单词就会拿出相同的容器 让容器里面的count加一 } Set <String> keys = map.keySet();//把Map所有的key都拿出来放到keys里面 那么我们遍历map就只需要遍历keys这个数组就行了 for(String key : keys) { System.out.println(key + "出现了" + map.get(key).getCount() + "次"); } */ //我们可以把第一个for 和第二个for进行合并简化一下 看下面的method01 method02(); } public static void method01() { String str = "The early birds catch the worm Twenty years old is not the years to be happy"; String[] strArray = str.split(" ");//将字符串以空格为界限 进行分割 Map <String,Letter> map = new HashMap <String,Letter> ();//创建一个map for(String temp : strArray){ if(!map.containsKey(temp)) {//如果map里面没有包含这个单词的的容器Letter就创建一个 map.put(temp,new Letter()); } Letter let = map.get(temp);//然后把创建好的容器Letter拿出来; let.setCount(let.getCount() + 1);//对存放单词的数组进行遍历 出现相同的单词就会拿出相同的容器 让容器里面的count加一 } Set <String> keys = map.keySet();//把Map所有的key都拿出来放到keys里面 那么我们遍历map就只需要遍历keys这个数组就行了 for(String key : keys) { System.out.println(key + "出现了" + map.get(key).getCount() + "次"); } } public static void method02() { String str = "人 生 是 一 场 没 有 彩 排 的 现 场 直 播"; String[] strArray = str.split(" ");//将字符串以空格为界限 进行分割 Map <String,Letter> map = new HashMap <String,Letter> ();//创建一个map for(String temp : strArray) { if(!map.containsKey(temp)) {//如果没有这个单词的容器就创建一个新容器 Letter a = new Letter(); map.put(temp, a);//并将元素存进去 数加一 a.setCount(a.getCount()+1); }else {//说明传见过容器了 Letter b = map.get(temp);//将这个的容器拿出来 数加一 b.setCount(b.getCount() +1); } } Set <String> keys = map.keySet(); for(String key : keys) { System.out.println(key + " 出现了 " + map.get(key).getCount() + " 次"); } } }
2.练习2:
题目:定义一个Student类,属性:name 姓名,classNumber 班号,score 成绩
现在将若干Student对象放入List,请统计出每个班级的总分和平均分,
分别打印,并打印出来每个班的学生都是谁;这里我们用面向对象方法来写
package HashMapPractice; public class Student { private int classRoomNum; private String name; private double score; public Student(int classRoomNum, String name, double score) { super(); this.classRoomNum = classRoomNum; this.name = name; this.score = score; } public int getClassRoomNum() { return classRoomNum; } public void setClassRoomNum(int classRoomNum) { this.classRoomNum = classRoomNum; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getScore() { return score; } public void setScore(double score) { this.score = score; } }
package HashMapPractice; import java.util.ArrayList; import java.util.List; public class ClassRoom { private int classRoomNum; private List <Student> studentList; private double total; public ClassRoom(){ studentList = new ArrayList();//先在这里建一个储存student的 容器 } public ClassRoom(int classRoomNum) { this();//调用空构造器 这里会生成一个student的容器 this.classRoomNum = classRoomNum; } public int getClassRoomNum() { return classRoomNum; } public void setClassRoomNum(int classRoomNum) { this.classRoomNum = classRoomNum; } public List<Student> getStudentList() { return studentList; } public void setStudentList(List<Student> studentList) { this.studentList = studentList; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } }
package HashMapPractice; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; /** * * HashMap练习2 * * 题目:定义一个Student类,属性:name 姓名,classNumber 班号,score 成绩 * 现在将若干Student对象放入List,请统计出每个班级的总分和平均分, * 分别打印,并打印出来每个班的学生都是谁;这里我们用面向对象方法来写 * * * @author Wang * */ public class Practice02 { public static void main(String[] args) { List <Student> list = new ArrayList();//创建一个储存student的容器 inputInformation(list);//添加学生信息 Map <Integer,ClassRoom> mapRoom = new HashMap(); handle(list,mapRoom); print03(mapRoom); } public static void inputInformation(List <Student> list) {//添加信息 list.add(new Student(1,"张三",102)); list.add(new Student(1,"王亚杰",149)); list.add(new Student(1,"李双",133)); list.add(new Student(2,"李四",77)); list.add(new Student(2,"王二",111)); list.add(new Student(2,"李大双",112)); list.add(new Student(3,"栋宝宝",150)); } public static void handle(List <Student> list,Map <Integer,ClassRoom> mapRoom) {//处理信息 for(Student stu : list) { int a = stu.getClassRoomNum(); String b = stu.getName(); double c = stu.getScore(); ClassRoom room = mapRoom.get(a); //将这个学生的班级通过他的班级号拿出来 if(room == null) { //检测mapRoom中有没有这个这个班级 如果没有的话 room = new ClassRoom(); //就新建一个班级 mapRoom.put(a, room); //并且吧这个班级放到Map里面 } room.setClassRoomNum(a); //把班级号加进去 room.setTotal(room.getTotal()+c); //已经有这个班级了 那么就把这个学生的分数加到总分里 room.getStudentList().add(stu); //把这个学生加到这个班级里 } } public static void print01(Map <Integer,ClassRoom> mapRoom) {//打印总分,平均分,各班的学生名单 用的是mapRoom.keySet() Set <Integer> keys = mapRoom.keySet(); for(Integer key : keys) { double totalScore = mapRoom.get(key).getTotal(); double averageScore = totalScore/ mapRoom.get(key).getStudentList().size(); System.out.println("班级号为: " + key +" " + "班级总分为 " + totalScore +" "+ "班级的平均分为 " + averageScore); List <Student> list1 = mapRoom.get(key).getStudentList(); System.out.print("班级成员有: "); for(int i = 0; i<list1.size();i++) { System.out.print(list1.get(i).getName() + " "); } System.out.println(); } } public static void print02(Map <Integer,ClassRoom> mapRoom) {//写这个方法的主要目的是 让自己熟悉一下迭代器 用的是mapRoom.entrySet() Set <Map.Entry<Integer,ClassRoom> >entrySet = mapRoom.entrySet(); Iterator <Map.Entry<Integer,ClassRoom> > it = entrySet.iterator(); while(it.hasNext()) { Map.Entry<Integer,ClassRoom> entry =it.next(); ClassRoom room = entry.getValue(); double avg = room.getTotal()/room.getStudentList().size(); System.out.println("班号为:"+room.getClassRoomNum()+",总分"+room.getTotal()+",平均分"+avg); } } public static void print03(Map <Integer,ClassRoom> mapRoom) {//写这个方法的主要目的是 让自己熟悉一下迭代器 用的是mapRoom.keySet() Set <Integer> keys = mapRoom.keySet(); Iterator <Integer> iter = keys.iterator(); while(iter.hasNext()) { ClassRoom room = mapRoom.get(iter.next()); double totalScore = room.getTotal(); double averageScore = totalScore/room.getStudentList().size(); System.out.println(totalScore+ " " + averageScore); } } }