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());
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章