Map实现分拣思路存储数据+自定义迭代器实现

本文介绍两种统计字符串中单词出现次数的方法,同时演示了如何利用面向对象思想和分拣思路统计班级的总人数及平均分。

1、分拣思路统计字符串出现次数

package cn.cjy.collection;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 用分拣思路统计字符串出现次数(2种思路)
 * 1、为所有Key创建容器,之后容器中直接存放value
 * 2、第一次创建容器并存放值,第二次之后直接使用容器存放值
 *
 */
public class MapApply01 {
	//第一种思路:为所有Key创建容器,之后容器中直接存放value
	public static void test1(){
		String str = "this-is-my-first-dog-but-i-like-cat-and-cat-is-nice-and-dog-is-friendly-this-why-i-like-cat-more";
		//分割字符串
		String[] strArray = str.split("-");
		//存储到Map中
		Map<String,Letter> map = new HashMap<String,Letter>();
		for(String temp : strArray){
			//1、为所有key创建容器
			if(!map.containsKey(temp)){
				map.put(temp, new Letter());
			}
			//2、直接使用容器存放value
			Letter letter =  map.get(temp);
			letter.setCount(letter.getCount()+1);
		}
		//输出Map值
		Set<String> keys = map.keySet();
		for(String temp:keys){
			Letter key = map.get(temp);
			System.out.println(temp+" 出现的次数为:"+key.getCount());
		}
	}
	
	//第二种思路:第一次创建容器并存放值,第二次之后直接使用容器存放值
	public static void test2(){
		String str = "this-is-my-first-dog-but-i-like-cat-and-cat-is-nice-and-dog-is-friendly-this-why-i-like-cat-more";
		//分割字符串
		String[] strArray = str.split("-");
		//存储到Map中
		Map<String,Letter> map = new HashMap<String,Letter>();
		
		Letter letter = null;
		for(String temp : strArray){
			if(null==(letter=map.get(temp))){
				letter = new Letter(); //第一次创建容器并放入value
				letter.setCount(1);  
				map.put(temp, letter);
			}else{
				letter =  map.get(temp);  //第二次之后直接放入value
				letter.setCount(letter.getCount()+1);
			}
		}
		//输出Map值
		Set<String> keys = map.keySet();
		for(String temp:keys){
			Letter key = map.get(temp);
			System.out.println(temp+" 出现的次数为:"+key.getCount());
		}
	}

	public static void main(String[] args) {
		test1();
		test2();
	}

}

class Letter{
	private String name;
	private int count;
	
	public void Letter(){
		
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
}

2、用面向对象思想+分拣思路统计班级总人数和平均分

package cn.cjy.collection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 用面向对象思想+分拣思路统计班级总人数和平均分
 * 将若干Student放入List
 *
 */
public class MapApply02 {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		exam(list);
		Map<String,ClassRoom> map = new HashMap<String,ClassRoom>();
		count(map,list);
		printMap(map);
		
	}
	//打印班级总人数和平均分
	public static void printMap(Map<String,ClassRoom> map){
		Set<Entry<String, ClassRoom>> entrySets = map.entrySet();
		Iterator<Entry<String, ClassRoom>> itera = entrySets.iterator();
		while(itera.hasNext()){
			Entry<String, ClassRoom> entry = itera.next();
			ClassRoom room = entry.getValue();
			double totols = room.getTotal();
			int num = room.getStus().size();
			System.out.println(room.getCode()+"班级的学生人数是:"+num+"  总分是:"+totols);
			
		}
	}
	//统计分数
	public static void count(Map<String,ClassRoom> map,List<Student> list){
		for(Student temp:list){
			String classNo = temp.getClassNo();
			double score = temp.getScore();
			
			//根据班级编号查看Map是否存在该班级,分拣思路
			ClassRoom room = map.get(classNo);
			if(null==room){
				room = new ClassRoom();
				map.put(classNo, room);
			}
			
			room.setCode(classNo);//存储班级号
			room.setTotal(room.getTotal()+score);//存储总分
			room.getStus().add(temp);//加入学生
		}
	}
	
	//将若干Student放入list
	public static void exam(List<Student> list){
		list.add(new Student("张三","001",80));
		list.add(new Student("李四","001",90));
		list.add(new Student("王二","002",92));
		list.add(new Student("赵五","002",88));
		list.add(new Student("钱多","002",87));
		list.add(new Student("孙六","003",70));
	}
}

//面向对象,定义一个Class班级类
class ClassRoom{
	private String code; //班级
	private List<Student> stus; //学生列表
	private double total; //总分
	
	public ClassRoom(){
		stus = new ArrayList<Student>();
	}
	
	public ClassRoom(String code) {
		this();
		this.code = code;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public List<Student> getStus() {
		return stus;
	}
	public void setStus(List<Student> stus) {
		this.stus = stus;
	}
	public double getTotal() {
		return total;
	}
	public void setTotal(double total) {
		this.total = total;
	}
	
}

//面向对象,定义一个Student类
class Student{
	private String name;
	private String classNo;
	private double score;
	
	public Student(){
		
	}
	
	public Student(String name, String classNo, double score) {
		super();
		this.name = name;
		this.classNo = classNo;
		this.score = score;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getClassNo() {
		return classNo;
	}
	public void setClassNo(String classNo) {
		this.classNo = classNo;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	
}

3、匿名内部类自定义实现迭代器

package cn.cjy.collection;

import java.util.Iterator;

/**
 * 内部类实现迭代器:简化迭代器原理加入接口提供方法
 * hasNext()
 * next()
 * remove()
 * 
 */
public class MyIterator {
	private String[] element = new String[]{"a","b","c","d","e","f"};
	private int size = element.length;
	
	/**
	 * 匿名内部类
	 */
	private Iterator<String> iterator(){
		return new Iterator<String>(){
			private int cursor = -1;
			//判断是否存在下一个元素
			public boolean hasNext(){
				return cursor+1 < size;
			}
			//获取下一个元素
			public String next(){
				cursor++;
				return element[cursor];
			}
			//移除元素
			public void remove(){
			}
		};
	}
	
	public static void main(String[] args) {
		MyIterator myItera = new MyIterator();
		Iterator<String> itera = myItera.iterator();
		while(itera.hasNext()){
			System.out.println(itera.next());
		}
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值